Generate album art thumbnails

This commit is contained in:
Antoine Gersant 2016-10-23 15:12:49 -07:00
parent c50e957932
commit 7b3e0cfaef
7 changed files with 176 additions and 4 deletions

1
.gitignore vendored
View file

@ -2,4 +2,5 @@ target
release
*.dll
*.res
tmp
TestConfig.toml

98
Cargo.lock generated
View file

@ -5,6 +5,7 @@ dependencies = [
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)",
"id3 0.1.10 (git+https://github.com/jameshurst/rust-id3)",
"image 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)",
"iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (git+https://github.com/retep998/winapi-rs)",
"mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -57,6 +58,11 @@ name = "byteorder"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "color_quant"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "conduit-mime-types"
version = "0.7.3"
@ -76,6 +82,14 @@ dependencies = [
"url 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "deque"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dtoa"
version = "0.2.2"
@ -138,6 +152,14 @@ name = "encoding_index_tests"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "enum_primitive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.3.5"
@ -184,6 +206,20 @@ name = "getopts"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "gif"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"color_quant 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lzw 0.10.0 (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 = "hpack"
version = "0.2.0"
@ -264,6 +300,28 @@ dependencies = [
"unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "image"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"jpeg-decoder 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"png 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped_threadpool 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "inflate"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "iron"
version = "0.4.0"
@ -286,6 +344,15 @@ name = "itoa"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "jpeg-decoder"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -337,6 +404,11 @@ name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lzw"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "matches"
version = "0.1.2"
@ -613,6 +685,17 @@ dependencies = [
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "png"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.14"
@ -621,6 +704,16 @@ dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rayon"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.1.77"
@ -666,6 +759,11 @@ dependencies = [
"semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scoped_threadpool"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "semver"
version = "0.1.20"

View file

@ -10,6 +10,7 @@ ui = []
getopts = "0.2.14"
hyper = "0.9.10"
id3 = { git = "https://github.com/jameshurst/rust-id3" }
image = "0.10.3"
iron = "0.4.0"
mount = "0.2.1"
oven = { git = "https://github.com/agersant/oven", branch = "remove_cookie_dep" }

View file

@ -1,7 +1,7 @@
use core::str::Utf8Error;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::path::*;
use std::ops::Deref;
use std::sync::Arc;
use std::sync::Mutex;
@ -17,6 +17,7 @@ use url::percent_encoding::percent_decode;
use collection::*;
use error::*;
use thumbnails::*;
use utils::*;
impl From<PError> for IronError {
@ -37,6 +38,7 @@ impl From<PError> for IronError {
IronError::new(err, status::InternalServerError)
}
PError::AlbumArtSearchError => IronError::new(err, status::InternalServerError),
PError::ImageProcessingError => IronError::new(err, status::InternalServerError),
PError::ID3ParseError => IronError::new(err, status::InternalServerError),
PError::Unauthorized => IronError::new(err, status::Unauthorized),
PError::IncorrectCredentials => IronError::new(err, status::BadRequest),
@ -194,12 +196,16 @@ fn serve(request: &mut Request, collection: &Collection) -> IronResult<Response>
}
if is_image(real_path.as_path()) {
return art(request);
return art(request, real_path.as_path());
}
Err(IronError::from(PError::UnsupportedFileType))
}
fn art(_: &mut Request) -> IronResult<Response> {
Err(IronError::from(PError::UnsupportedFileType))
fn art(_: &mut Request, real_path: &Path) -> IronResult<Response> {
let thumb = get_thumbnail(real_path, 400);
match thumb {
Ok(path) => Ok(Response::with((status::Ok, path))),
Err(e) => Err(IronError::from(e))
}
}

View file

@ -2,6 +2,7 @@ use std::error;
use std::fmt;
use std::io;
use id3;
use image;
#[derive(Debug)]
pub enum PError {
@ -18,6 +19,7 @@ pub enum PError {
ConfigUsersParseError,
ConfigAlbumArtPatternParseError,
AlbumArtSearchError,
ImageProcessingError,
ID3ParseError,
Unauthorized,
IncorrectCredentials,
@ -35,6 +37,12 @@ impl From<id3::Error> for PError {
}
}
impl From<image::ImageError> for PError {
fn from(_: image::ImageError) -> PError {
PError::ImageProcessingError
}
}
impl error::Error for PError {
fn description(&self) -> &str {
match *self {
@ -55,6 +63,7 @@ impl error::Error for PError {
"Could not parse album art pattern in config file"
}
PError::AlbumArtSearchError => "Error while looking for album art",
PError::ImageProcessingError => "Error while processing image",
PError::ID3ParseError => "Error while reading ID3 tag",
PError::Unauthorized => "Authentication required",
PError::IncorrectCredentials => "Incorrect username/password combination",
@ -89,6 +98,7 @@ impl fmt::Display for PError {
write!(f, "Could not album art pattern in config file")
}
PError::AlbumArtSearchError => write!(f, "Error while looking for album art"),
PError::ImageProcessingError => write!(f, "Error while processing image"),
PError::ID3ParseError => write!(f, "Error while reading ID3 tag"),
PError::Unauthorized => write!(f, "Authentication required"),
PError::IncorrectCredentials => write!(f, "Incorrect username/password combination"),

View file

@ -2,6 +2,7 @@ extern crate core;
extern crate getopts;
extern crate hyper;
extern crate id3;
extern crate image;
extern crate iron;
extern crate mount;
extern crate oven;
@ -38,6 +39,7 @@ mod ddns;
mod error;
mod ui;
mod utils;
mod thumbnails;
mod vfs;
const DEFAULT_CONFIG_FILE_NAME: &'static str = "polaris.toml";

54
src/thumbnails.rs Normal file
View file

@ -0,0 +1,54 @@
use std::cmp;
use std::fs::DirBuilder;
use std::path::*;
use std::hash::{Hash, Hasher, SipHasher};
use image;
use image::GenericImage;
use image::ImageBuffer;
use image::FilterType;
use image::imageops::resize;
use error::*;
const THUMBNAILS_PATH: &'static str = "tmp/thumbnails";
fn hash(path: &Path, dimension: u32) -> u64 {
let path_string = path.to_string_lossy();
let hash_input = format!("{}:{}", path_string, dimension.to_string());
let mut hasher = SipHasher::new();
hash_input.hash(&mut hasher);
hasher.finish()
}
pub fn get_thumbnail(real_path: &Path, max_dimension: u32) -> Result<PathBuf, PError> {
let mut out_path = PathBuf::new();
out_path.push(THUMBNAILS_PATH);
let mut dir_builder = DirBuilder::new();
dir_builder.recursive(true);
try!(dir_builder.create(out_path.as_path()));
let source_image = try!(image::open(real_path));
let (source_width, source_height) = source_image.dimensions();
let cropped_dimension = cmp::max(source_width, source_height);
let out_dimension = cmp::min(max_dimension, cropped_dimension);
let hash = hash(real_path, out_dimension);
out_path.push(format!("{}.png", hash.to_string()));
if !out_path.exists() {
let source_aspect_ratio : f32 = source_width as f32 / source_height as f32;
if source_aspect_ratio < 0.8 || source_aspect_ratio > 1.2 {
let mut cropped_image = ImageBuffer::new(cropped_dimension, cropped_dimension);
cropped_image.copy_from(&source_image, (cropped_dimension - source_width)/2, (cropped_dimension - source_height)/2);
let out_image = resize(&cropped_image, out_dimension, out_dimension, FilterType::Lanczos3);
try!(out_image.save(out_path.as_path()));
} else {
let out_image = resize(&source_image, out_dimension, out_dimension, FilterType::Lanczos3);
try!(out_image.save(out_path.as_path()));
}
}
Ok(out_path)
}