diff --git a/Cargo.lock b/Cargo.lock index 2d33e5d..3e8538e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -410,7 +410,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -519,6 +519,11 @@ dependencies = [ "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "gobject-sys" version = "0.5.0" @@ -602,6 +607,7 @@ version = "0.1.0" dependencies = [ "diesel 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "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)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1010,7 +1016,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "num_cpus" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1188,7 +1194,7 @@ dependencies = [ "coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1795,6 +1801,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a303bbf7a5e75ab3b627117ff10e495d1b9e97e1d68966285ac2b1f6270091bc" "checksum glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "450247060df7d52fdad31e1d66f30d967e925c9d1d26a0ae050cfe33dcd00d08" "checksum glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8" +"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2" "checksum gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0847c507e52c1feaede13ef56fb4847742438602655449d5f1f782e8633f146f" "checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f" @@ -1839,7 +1846,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" "checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" "checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" -"checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d" +"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum open 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c281318d992e4432cfa799969467003d05921582a7489a8325e37f8a450d5113" "checksum openssl 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "169a4b9160baf9b9b1ab975418c673686638995ba921683a7f1e01470dcb8854" "checksum openssl-sys 0.9.23 (registry+https://github.com/rust-lang/crates.io-index)" = "2200ffec628e3f14c39fc0131a301db214f1a7d584e36507ee8700b0c7fb7a46" diff --git a/hammond-downloader/Cargo.toml b/hammond-downloader/Cargo.toml index efb8754..7116ab7 100644 --- a/hammond-downloader/Cargo.toml +++ b/hammond-downloader/Cargo.toml @@ -11,6 +11,7 @@ log = "0.3.8" mime_guess = "1.8.3" reqwest = "0.8.2" tempdir = "0.3.5" +glob = "0.2.11" [dependencies.diesel] features = ["sqlite"] diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 7df62b6..fc6220a 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -2,6 +2,7 @@ use reqwest; use hyper::header::*; use tempdir::TempDir; use mime_guess; +use glob::glob; use std::fs::{rename, DirBuilder, File}; use std::io::{BufWriter, Read, Write}; @@ -43,7 +44,8 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { info!("Extension: {}", ext); // Construct a temp file to save desired content. - let tempdir = TempDir::new_in(dir, "")?; + // 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(),); @@ -151,21 +153,13 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option { pd.title().to_owned() ); - // Hacky way - // TODO: make it so it returns the first cover.* file encountered. - // Use glob instead - let png = format!("{}/cover.png", download_fold); - let jpg = format!("{}/cover.jpg", download_fold); - let jpe = format!("{}/cover.jpe", download_fold); - let jpeg = format!("{}/cover.jpeg", download_fold); - if Path::new(&png).exists() { - return Some(png); - } else if Path::new(&jpe).exists() { - return Some(jpe); - } else if Path::new(&jpg).exists() { - return Some(jpg); - } else if Path::new(&jpeg).exists() { - return Some(jpeg); + // Weird glob magic. + if let Ok(mut foo) = glob(&format!("{}/cover.*", 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 { + return Some(p.to_str()?.into()); + } }; DirBuilder::new() @@ -173,14 +167,16 @@ pub fn cache_image(pd: &PodcastCoverQuery) -> Option { .create(&download_fold) .unwrap(); - let dlpath = download_into(&download_fold, "cover", &url); - if let Ok(path) = dlpath { - info!("Cached img into: {}", &path); - Some(path) - } else { - error!("Failed to get feed image."); - error!("Error: {}", dlpath.unwrap_err()); - None + match download_into(&download_fold, "cover", &url) { + Ok(path) => { + info!("Cached img into: {}", &path); + Some(path) + } + Err(err) => { + error!("Failed to get feed image."); + error!("Error: {}", err); + None + } } } diff --git a/hammond-downloader/src/lib.rs b/hammond-downloader/src/lib.rs index 98f6678..9e57db8 100644 --- a/hammond-downloader/src/lib.rs +++ b/hammond-downloader/src/lib.rs @@ -3,6 +3,7 @@ extern crate diesel; #[macro_use] extern crate error_chain; +extern crate glob; extern crate hammond_data; extern crate hyper; #[macro_use]