diff --git a/Cargo.lock b/Cargo.lock index 3e8538e..900c1e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -610,6 +610,7 @@ dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "hammond-data 0.1.0", "hyper 0.11.10 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/hammond-downloader/Cargo.toml b/hammond-downloader/Cargo.toml index 7116ab7..cb7d6f5 100644 --- a/hammond-downloader/Cargo.toml +++ b/hammond-downloader/Cargo.toml @@ -12,6 +12,7 @@ mime_guess = "1.8.3" reqwest = "0.8.2" tempdir = "0.3.5" glob = "0.2.11" +lazy_static = "1.0.0" [dependencies.diesel] features = ["sqlite"] diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index fc6220a..14a205d 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -34,7 +34,6 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { } let headers = resp.headers().clone(); - let ct_len = headers.get::().map(|ct_len| **ct_len); let ct_type = headers.get::(); ct_len.map(|x| info!("File Lenght: {}", x)); @@ -46,7 +45,6 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { // Construct a temp file to save desired content. // It has to be a `new_in` instead of new cause rename can't move cross filesystems. let tempdir = TempDir::new_in(dir, "temp_download")?; - let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),); // Save requested content into the file. @@ -60,7 +58,7 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { Ok(target) } -// Determine the file extension from the http content-type header. +/// Determine the file extension from the http content-type header. fn get_ext(content: Option) -> Option { let cont = content.clone()?; content @@ -121,7 +119,7 @@ pub fn get_episode(ep: &mut EpisodeWidgetQuery, download_folder: &str) -> Result ep.save()?; }; - let res = download_into(download_folder, ep.title(), ep.uri().unwrap()); + let res = download_into(download_folder, &ep.rowid().to_string(), ep.uri().unwrap()); if let Ok(path) = res { // If download succedes set episode local_uri to dlpath. @@ -147,14 +145,14 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option { return None; } - let download_fold = format!( + let cache_download_fold = format!( "{}{}", HAMMOND_CACHE.to_str().unwrap(), pd.title().to_owned() ); // Weird glob magic. - if let Ok(mut foo) = glob(&format!("{}/cover.*", download_fold)) { + if let Ok(mut foo) = glob(&format!("{}/cover.*", cache_download_fold)) { // For some reason there is no .first() method so nth(0) is used let path = foo.nth(0).and_then(|x| x.ok()); if let Some(p) = path { @@ -162,12 +160,13 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option { } }; + // Create the folders if they don't exist. DirBuilder::new() .recursive(true) - .create(&download_fold) + .create(&cache_download_fold) .unwrap(); - match download_into(&download_fold, "cover", &url) { + match download_into(&cache_download_fold, "cover", &url) { Ok(path) => { info!("Cached img into: {}", &path); Some(path) diff --git a/hammond-downloader/src/lib.rs b/hammond-downloader/src/lib.rs index 9e57db8..ef75fa5 100644 --- a/hammond-downloader/src/lib.rs +++ b/hammond-downloader/src/lib.rs @@ -7,6 +7,8 @@ extern crate glob; extern crate hammond_data; extern crate hyper; #[macro_use] +extern crate lazy_static; +#[macro_use] extern crate log; extern crate mime_guess; extern crate reqwest;