From d3beb30f00dcda074904986095b6f6ad429d0156 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 10 Nov 2017 11:35:58 +0200 Subject: [PATCH] Closed #5. Proper guessing of the file extension from the mime-type. --- Cargo.lock | 22 ++++++++++++++ Cargo.toml | 2 +- hammond-downloader/Cargo.toml | 1 + hammond-downloader/src/downloader.rs | 44 ++++++---------------------- hammond-downloader/src/lib.rs | 1 + 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 005cc2b..91bf631 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -564,6 +564,7 @@ dependencies = [ "hyper 0.11.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 1.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "rss 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -760,6 +761,14 @@ dependencies = [ "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mime" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.3.5" @@ -768,6 +777,17 @@ dependencies = [ "unicase 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mime_guess" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime_guess" version = "2.0.0-alpha.2" @@ -1589,7 +1609,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum markup5ever 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2cf89d3e0486c32c9d99521455ddf9a438910a1ce2bd376936086edc15dff5fc" "checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376" "checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" +"checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e2e00e17be181010a91dbfefb01660b17311059dc8c7f48b9017677721e732bd" +"checksum mime_guess 1.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bbee1a836f344ac39d4a59bfe7be2bd3150353ff71678afb740216f8270b333e" "checksum mime_guess 2.0.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27a5e6679a0614e25adc14c6434ba84e41632b765a6d9cb2031a0cca682699ae" "checksum mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0e8411968194c7b139e9105bc4ae7db0bae232af087147e72f0616ebf5fdb9cb" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" diff --git a/Cargo.toml b/Cargo.toml index c4c9629..95e6219 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ members = [ ] [profile.release] -debug = true +debug = false diff --git a/hammond-downloader/Cargo.toml b/hammond-downloader/Cargo.toml index e403ddb..36de161 100644 --- a/hammond-downloader/Cargo.toml +++ b/hammond-downloader/Cargo.toml @@ -10,6 +10,7 @@ error-chain = "0.11" hyper = "0.11" log = "0.3" mime = "0.3" +mime_guess = "1.8" rand = "0.3" reqwest = "0.8" rss = "1.1" diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index e36c280..bc54374 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -3,12 +3,11 @@ use hyper::header::*; use tempdir::TempDir; use rand; use rand::Rng; -use mime::Mime; +use mime_guess; use std::fs::{rename, DirBuilder, File}; use std::io::{BufWriter, Read, Write}; use std::path::Path; -use std::collections::HashMap; // use std::str::FromStr; use errors::*; @@ -36,26 +35,23 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { ct_len.map(|x| info!("File Lenght: {}", x)); ct_type.map(|x| info!("Content Type: {}", x)); - // This sucks! let ext = if let Some(t) = ct_type { - let f = i_hate_this_func(t); - if let Some(foo_) = f { - info!("File Extension to be used: {}", &foo_); - foo_ + let mime = mime_guess::get_extensions(t.type_().as_ref(), t.subtype().as_ref()); + if let Some(m) = mime { + m.first().unwrap() } else { - error!("Failed to match mime-type: {}", t); - panic!("Unkown file extension."); + error!("Unkown mime type. {}", t); + "unkown" } } else { - // To be used only as fallback. - // FIXME: Unreliable and hacky way to extract the file extension from the url. - // https://gitlab.gnome.org/alatiera/Hammond/issues/5 - url.split('.').last().unwrap().to_string() + error!("Unkown mime type."); + "unkown" }; // Construct the download path. let filename = format!("{}.{}", file_title, ext); + // TODO: do a mime-type check after the file is downloaded to be sure. return save_io(dir, &filename, &mut resp, ct_len); } // Ok(String::from("")) @@ -101,28 +97,6 @@ fn save_io( Ok(target) } - -// Please, for the love of Cthulu, if you know the proper way to covnert mime-tyeps -// into file extension PLEASE let me know. -fn i_hate_this_func(m: &Mime) -> Option { - // This is terrible for performance but the whole thing is suboptimal anyway - let mut mimes = HashMap::new(); - mimes.insert("audio/ogg", "ogg"); - mimes.insert("audio/mpeg", "mp3"); - mimes.insert("audio/aac", "aac"); - mimes.insert("audio/x-m4a", "aac"); - mimes.insert("video/mpeg", "mp4"); - mimes.insert("image/jpeg", "jpg"); - mimes.insert("image/png", "png"); - - let res = mimes.get(m.as_ref()); - if let Some(r) = res { - return Some(r.to_string()); - } - - None -} - pub fn get_download_folder(pd_title: &str) -> Result { // It might be better to make it a hash of the title let download_fold = format!("{}/{}", DL_DIR.to_str().unwrap(), pd_title); diff --git a/hammond-downloader/src/lib.rs b/hammond-downloader/src/lib.rs index 8ec303b..d38d6b8 100644 --- a/hammond-downloader/src/lib.rs +++ b/hammond-downloader/src/lib.rs @@ -8,6 +8,7 @@ extern crate hyper; #[macro_use] extern crate log; extern crate mime; +extern crate mime_guess; extern crate rand; extern crate reqwest; extern crate tempdir;