Scaffholding for serving thumbnails

This commit is contained in:
Antoine Gersant 2016-10-23 12:07:37 -07:00
parent bb66b3a566
commit 61e4f6c7d2
5 changed files with 62 additions and 22 deletions

View file

@ -17,6 +17,7 @@ use url::percent_encoding::percent_decode;
use collection::*;
use error::*;
use utils::*;
impl From<PError> for IronError {
fn from(err: PError) -> IronError {
@ -26,6 +27,7 @@ impl From<PError> 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<Response>
};
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<Response> {
Err(IronError::from(PError::UnsupportedFileType))
}

View file

@ -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);
}

View file

@ -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"),

View file

@ -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";

37
src/utils.rs Normal file
View file

@ -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,
}
}