Commit 074284d2 authored by Jordan Petridis's avatar Jordan Petridis 🌱

Move unsub show logic and get_download_folder func to hammond-data::utils.

parent e290ae22
......@@ -8,11 +8,13 @@ use itertools::Itertools;
use errors::*;
use dbqueries;
use models::queryables::EpisodeCleanerQuery;
use models::queryables::{EpisodeCleanerQuery, Podcast};
use xdg_dirs::DL_DIR;
use std::path::Path;
use std::fs;
/// Scan downloaded `episode` entries that might have broken `local_uri`s and set them to `None`.
fn download_checker() -> Result<()> {
let episodes = dbqueries::get_downloaded_episodes()?;
......@@ -30,6 +32,7 @@ fn download_checker() -> Result<()> {
Ok(())
}
/// Delete watched `episodes` that have exceded their liftime after played.
fn played_cleaner() -> Result<()> {
let mut episodes = dbqueries::get_played_cleaner_episodes()?;
......@@ -54,7 +57,7 @@ fn played_cleaner() -> Result<()> {
}
/// Check `ep.local_uri` field and delete the file it points to.
pub fn delete_local_content(ep: &mut EpisodeCleanerQuery) -> Result<()> {
fn delete_local_content(ep: &mut EpisodeCleanerQuery) -> Result<()> {
if ep.local_uri().is_some() {
let uri = ep.local_uri().unwrap().to_owned();
if Path::new(&uri).exists() {
......@@ -119,6 +122,38 @@ pub fn replace_extra_spaces(s: &str) -> String {
.collect::<String>()
}
/// Returns the URI of a Podcast Downloads given it's title.
pub fn get_download_folder(pd_title: &str) -> Result<String> {
// It might be better to make it a hash of the title or the podcast rowid
let download_fold = format!("{}/{}", DL_DIR.to_str().unwrap(), pd_title);
// Create the folder
fs::DirBuilder::new()
.recursive(true)
.create(&download_fold)?;
Ok(download_fold)
}
/// Removes all the entries associated with the given show from the database,
/// and deletes all of the downloaded content.
/// TODO: Write Tests
/// TODO: Return Result instead
pub fn delete_show(pd: &Podcast) {
let res = dbqueries::remove_feed(&pd);
if res.is_ok() {
info!("{} was removed succesfully.", pd.title());
let dl_fold = get_download_folder(pd.title());
if let Ok(fold) = dl_fold {
let res3 = fs::remove_dir_all(&fold);
// TODO: Show errors?
if res3.is_ok() {
info!("All the content at, {} was removed succesfully", &fold);
}
};
}
}
#[cfg(test)]
mod tests {
extern crate tempdir;
......@@ -277,4 +312,11 @@ mod tests {
assert_eq!(replace_extra_spaces(&bad_txt), valid_txt);
}
#[test]
fn test_get_dl_folder() {
let foo_ = format!("{}/{}", DL_DIR.to_str().unwrap(), "foo");
assert_eq!(get_download_folder("foo").unwrap(), foo_);
let _ = fs::remove_dir_all(foo_);
}
}
......@@ -10,7 +10,7 @@ use std::fs;
use errors::*;
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery};
use hammond_data::xdg_dirs::{DL_DIR, HAMMOND_CACHE};
use hammond_data::xdg_dirs::HAMMOND_CACHE;
// TODO: Replace path that are of type &str with std::path.
// TODO: Have a convention/document absolute/relative paths, if they should end with / or not.
......@@ -97,15 +97,6 @@ fn save_io(file: &str, resp: &mut reqwest::Response, content_lenght: Option<u64>
Ok(())
}
pub fn get_download_folder(pd_title: &str) -> Result<String> {
// It might be better to make it a hash of the title
let download_fold = format!("{}/{}", DL_DIR.to_str().unwrap(), pd_title);
// Create the folder
DirBuilder::new().recursive(true).create(&download_fold)?;
Ok(download_fold)
}
// TODO: Refactor
pub fn get_episode(ep: &mut EpisodeWidgetQuery, download_folder: &str) -> Result<()> {
// Check if its alrdy downloaded
......@@ -192,15 +183,6 @@ mod tests {
use hammond_data::dbqueries;
use diesel::associations::Identifiable;
use std::fs;
#[test]
fn test_get_dl_folder() {
let foo_ = format!("{}/{}", DL_DIR.to_str().unwrap(), "foo");
assert_eq!(get_download_folder("foo").unwrap(), foo_);
let _ = fs::remove_dir_all(foo_);
}
#[test]
// This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit
// to run it.
......
......@@ -9,6 +9,7 @@ use humansize::{file_size_opts as size_opts, FileSize};
use hammond_data::dbqueries;
use hammond_data::{EpisodeWidgetQuery, Podcast};
use hammond_data::utils::get_download_folder;
use hammond_data::errors::*;
use hammond_downloader::downloader;
......@@ -227,7 +228,7 @@ fn on_download_clicked(
download_bttn.hide();
sender.send(Action::RefreshEpisodesViewBGR).unwrap();
thread::spawn(move || {
let download_fold = downloader::get_download_folder(&pd_title).unwrap();
let download_fold = get_download_folder(&pd_title).unwrap();
let e = downloader::get_episode(&mut ep, download_fold.as_str());
if let Err(err) = e {
error!("Error while trying to download: {:?}", ep.uri());
......
......@@ -6,8 +6,7 @@ use dissolve;
use hammond_data::dbqueries;
use hammond_data::Podcast;
use hammond_data::utils::replace_extra_spaces;
use hammond_downloader::downloader;
use hammond_data::utils::{delete_show, replace_extra_spaces};
use widgets::episode::episodes_listbox;
use utils::get_pixbuf_from_path;
......@@ -17,7 +16,6 @@ use app::Action;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::thread;
use std::fs;
#[derive(Debug, Clone)]
pub struct ShowWidget {
......@@ -126,19 +124,7 @@ fn on_unsub_button_clicked(
unsub_button.hide();
// Spawn a thread so it won't block the ui.
thread::spawn(clone!(pd => move || {
let res = dbqueries::remove_feed(&pd);
if res.is_ok() {
info!("{} was removed succesfully.", pd.title());
let dl_fold = downloader::get_download_folder(pd.title());
if let Ok(fold) = dl_fold {
let res3 = fs::remove_dir_all(&fold);
// TODO: Show errors?
if res3.is_ok() {
info!("All the content at, {} was removed succesfully", &fold);
}
};
}
delete_show(&pd)
}));
shows.switch_podcasts_animated();
// Queue a refresh after the switch to avoid blocking the db.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment