From 61e4f6c7d2f47dda27e1ee9d3c0cff0a1a442747 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 23 Oct 2016 12:07:37 -0700 Subject: [PATCH] Scaffholding for serving thumbnails --- src/api.rs | 18 ++++++++++++++++-- src/collection.rs | 25 +++++-------------------- src/error.rs | 3 +++ src/main.rs | 1 + src/utils.rs | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 src/utils.rs diff --git a/src/api.rs b/src/api.rs index 2ecbc2d..8db1c46 100644 --- a/src/api.rs +++ b/src/api.rs @@ -17,6 +17,7 @@ use url::percent_encoding::percent_decode; use collection::*; use error::*; +use utils::*; impl From for IronError { fn from(err: PError) -> IronError { @@ -26,6 +27,7 @@ impl From for IronError { PError::ConflictingMount => IronError::new(err, status::BadRequest), PError::PathNotInVfs => IronError::new(err, status::NotFound), PError::CannotServeDirectory => IronError::new(err, status::BadRequest), + PError::UnsupportedFileType => IronError::new(err, status::BadRequest), PError::ConfigFileOpenError => IronError::new(err, status::InternalServerError), PError::ConfigFileReadError => IronError::new(err, status::InternalServerError), PError::ConfigFileParseError => IronError::new(err, status::InternalServerError), @@ -184,8 +186,20 @@ fn serve(request: &mut Request, collection: &Collection) -> IronResult }; if !metadata.is_file() { - return Err(IronError::new(PError::CannotServeDirectory, status::BadRequest)); + return Err(IronError::from(PError::CannotServeDirectory)); } - Ok(Response::with((status::Ok, real_path))) + if is_song(real_path.as_path()) { + return Ok(Response::with((status::Ok, real_path))) + } + + if is_image(real_path.as_path()) { + return art(request); + } + + Err(IronError::from(PError::UnsupportedFileType)) } + +fn art(_: &mut Request) -> IronResult { + Err(IronError::from(PError::UnsupportedFileType)) +} \ No newline at end of file diff --git a/src/collection.rs b/src/collection.rs index 27f10fe..313d2d0 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -7,6 +7,7 @@ use regex::Regex; use config::Config; use vfs::*; use error::*; +use utils::*; #[derive(Debug, RustcEncodable)] pub struct Album { @@ -69,7 +70,7 @@ impl Album { } else { let find_song = try!(fs::read_dir(real_path)).find(|f| { match *f { - Ok(ref dir_entry) => Song::is_song(dir_entry.path().as_path()), + Ok(ref dir_entry) => is_song(dir_entry.path().as_path()), _ => false, } }); @@ -149,23 +150,7 @@ impl Song { } } - fn is_song(path: &Path) -> bool { - let extension = match path.extension() { - Some(e) => e, - _ => return false, - }; - let extension = match extension.to_str() { - Some(e) => e, - _ => return false, - }; - match extension { - "mp3" => return true, - "ogg" => return true, - "m4a" => return true, - "flac" => return true, - _ => return false, - } - } + } #[derive(Debug, RustcEncodable)] @@ -250,7 +235,7 @@ impl Collection { let file_path = file.path(); let file_path = file_path.as_path(); if file_meta.is_file() { - if Song::is_song(file_path) { + if is_song(file_path) { let song = try!(Song::read(self, file_path)); out.push(CollectionFile::Song(song)); } @@ -273,7 +258,7 @@ impl Collection { let file_path = file.path(); let file_path = file_path.as_path(); if file_meta.is_file() { - if Song::is_song(file_path) { + if is_song(file_path) { let song = try!(Song::read(self, file_path)); acc.push(song); } diff --git a/src/error.rs b/src/error.rs index abd9900..e56af21 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ pub enum PError { ConflictingMount, PathNotInVfs, CannotServeDirectory, + UnsupportedFileType, ConfigFileOpenError, ConfigFileReadError, ConfigFileParseError, @@ -44,6 +45,7 @@ impl error::Error for PError { } PError::PathNotInVfs => "Requested path does not index a mount point", PError::CannotServeDirectory => "Only individual files can be served", + PError::UnsupportedFileType => "Unrecognized extension", PError::ConfigFileOpenError => "Could not open config file", PError::ConfigFileReadError => "Could not read config file", PError::ConfigFileParseError => "Could not parse config file", @@ -75,6 +77,7 @@ impl fmt::Display for PError { PError::ConflictingMount => write!(f, "Mount point already has a target directory"), PError::PathNotInVfs => write!(f, "Requested path does not index a mount point"), PError::CannotServeDirectory => write!(f, "Only individual files can be served"), + PError::UnsupportedFileType => write!(f, "Unrecognized extension"), PError::ConfigFileOpenError => write!(f, "Could not open config file"), PError::ConfigFileReadError => write!(f, "Could not read config file"), PError::ConfigFileParseError => write!(f, "Could not parse config file"), diff --git a/src/main.rs b/src/main.rs index a49c486..22e46f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ mod config; mod ddns; mod error; mod ui; +mod utils; mod vfs; const DEFAULT_CONFIG_FILE_NAME: &'static str = "polaris.toml"; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..1bfca64 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,37 @@ +use std::path::Path; + +pub fn is_song(path: &Path) -> bool { + let extension = match path.extension() { + Some(e) => e, + _ => return false, + }; + let extension = match extension.to_str() { + Some(e) => e, + _ => return false, + }; + match extension { + "mp3" => return true, + "ogg" => return true, + "m4a" => return true, + "flac" => return true, + _ => return false, + } +} + +pub fn is_image(path: &Path) -> bool { + let extension = match path.extension() { + Some(e) => e, + _ => return false, + }; + let extension = match extension.to_str() { + Some(e) => e, + _ => return false, + }; + match extension { + "png" => return true, + "gif" => return true, + "jpg" => return true, + "bmp" => return true, + _ => return false, + } +} \ No newline at end of file