Temporary exposed download cleaner into the gui.
This commit is contained in:
@@ -10,22 +10,20 @@ use std::path::Path;
|
||||
use std::fs;
|
||||
|
||||
// TODO: Write unit test.
|
||||
pub fn download_checker(db: Database) -> Result<()> {
|
||||
fn download_checker(db: &Database) -> Result<()> {
|
||||
let mut episodes = {
|
||||
let tempdb = db.lock().unwrap();
|
||||
dbqueries::get_downloaded_episodes(&tempdb)?
|
||||
};
|
||||
|
||||
episodes.par_iter_mut().for_each(|ep| {
|
||||
if ep.local_uri().is_some() {
|
||||
if !Path::new(ep.local_uri().unwrap()).exists() {
|
||||
ep.set_local_uri(None);
|
||||
let res = ep.save(&db.clone());
|
||||
if let Err(err) = res {
|
||||
error!("Error while trying to update episode: {:#?}", ep);
|
||||
error!("Error: {}", err);
|
||||
};
|
||||
}
|
||||
if !Path::new(ep.local_uri().unwrap()).exists() {
|
||||
ep.set_local_uri(None);
|
||||
let res = ep.save(&db.clone());
|
||||
if let Err(err) = res {
|
||||
error!("Error while trying to update episode: {:#?}", ep);
|
||||
error!("Error: {}", err);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -33,7 +31,7 @@ pub fn download_checker(db: Database) -> Result<()> {
|
||||
}
|
||||
|
||||
// TODO: Write unit test.
|
||||
pub fn watched_cleaner(db: &Database) -> Result<()> {
|
||||
fn watched_cleaner(db: &Database) -> Result<()> {
|
||||
let mut episodes = {
|
||||
let tempdb = db.lock().unwrap();
|
||||
dbqueries::get_watched_episodes(&tempdb)?
|
||||
@@ -46,7 +44,7 @@ pub fn watched_cleaner(db: &Database) -> Result<()> {
|
||||
// TODO: expose a config and a user set option.
|
||||
let limit = watched + 172_800; // add 2days in seconds
|
||||
if now_utc > limit {
|
||||
let e = delete_local_content(db, &mut ep);
|
||||
let e = delete_local_content(&db.clone(), &mut ep);
|
||||
if let Err(err) = e {
|
||||
error!("Error while trying to delete file: {:?}", ep.local_uri());
|
||||
error!("Error: {}", err);
|
||||
@@ -79,3 +77,16 @@ pub fn delete_local_content(db: &Database, ep: &mut Episode) -> Result<()> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_watched(db: &Database, ep: &mut Episode) -> Result<()> {
|
||||
let epoch = Utc::now().timestamp() as i32;
|
||||
ep.set_watched(Some(epoch));
|
||||
ep.save(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(db: &Database) -> Result<()> {
|
||||
download_checker(db)?;
|
||||
watched_cleaner(db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use errors::*;
|
||||
|
||||
#[derive(Queryable, Identifiable, AsChangeset, Associations)]
|
||||
#[table_name = "episode"]
|
||||
#[changeset_options(treat_none_as_null = "true")]
|
||||
#[belongs_to(Podcast, foreign_key = "podcast_id")]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Episode {
|
||||
@@ -113,6 +114,7 @@ impl Episode {
|
||||
|
||||
#[derive(Queryable, Identifiable, AsChangeset, Associations)]
|
||||
#[belongs_to(Source, foreign_key = "source_id")]
|
||||
#[changeset_options(treat_none_as_null = "true")]
|
||||
#[table_name = "podcast"]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Podcast {
|
||||
@@ -170,6 +172,7 @@ impl Podcast {
|
||||
|
||||
#[derive(Queryable, Identifiable, AsChangeset)]
|
||||
#[table_name = "source"]
|
||||
#[changeset_options(treat_none_as_null = "true")]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Source {
|
||||
id: i32,
|
||||
|
||||
Reference in New Issue
Block a user