Finish implementation of option parameter resize.

This commit is contained in:
Bill Thiede 2020-02-19 21:30:12 -08:00
parent ecca57f489
commit b552b922fa
4 changed files with 139 additions and 14 deletions

116
Cargo.lock generated
View File

@ -140,6 +140,18 @@ dependencies = [
"byte-tools", "byte-tools",
] ]
[[package]]
name = "bstr"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "502ae1441a0a5adb8fbd38a5955a6416b9493e92b465de5e4a9bde6a539c2c48"
dependencies = [
"lazy_static 1.4.0",
"memchr",
"regex-automata",
"serde",
]
[[package]] [[package]]
name = "buf_redux" name = "buf_redux"
version = "0.8.4" version = "0.8.4"
@ -206,6 +218,15 @@ dependencies = [
"ppv-lite86", "ppv-lite86",
] ]
[[package]]
name = "cast"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0"
dependencies = [
"rustc_version 0.2.3",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.50" version = "1.0.50"
@ -308,6 +329,41 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "criterion"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc755679c12bda8e5523a71e4d654b6bf2e14bd838dfc48cde6559a05caf7d1"
dependencies = [
"atty",
"cast",
"clap",
"criterion-plot",
"csv",
"itertools",
"lazy_static 1.4.0",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a01e15e0ea58e8234f96146b1f91fa9d0e4dd7a38da93ff7a75d42c0b9d3a545"
dependencies = [
"cast",
"itertools",
]
[[package]] [[package]]
name = "crossbeam-deque" name = "crossbeam-deque"
version = "0.7.2" version = "0.7.2"
@ -353,6 +409,28 @@ dependencies = [
"lazy_static 1.4.0", "lazy_static 1.4.0",
] ]
[[package]]
name = "csv"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00affe7f6ab566df61b4be3ce8cf16bc2576bca0963ceb0955e45d514bf9a279"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "ct-logs" name = "ct-logs"
version = "0.6.0" version = "0.6.0"
@ -1333,6 +1411,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]]
name = "oorandom"
version = "11.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcec7c9c2a95cacc7cd0ecb89d8a8454eca13906f6deb55258ffff0adeb9405"
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
version = "0.2.3" version = "0.2.3"
@ -1459,6 +1543,7 @@ dependencies = [
name = "photosync" name = "photosync"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"criterion",
"google-photoslibrary1", "google-photoslibrary1",
"google_api_auth", "google_api_auth",
"hexihasher", "hexihasher",
@ -1518,6 +1603,18 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
[[package]]
name = "plotters"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e3bb8da247d27ae212529352020f3e5ee16e83c0c258061d27b08ab92675eeb"
dependencies = [
"js-sys",
"num-traits",
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "png" name = "png"
version = "0.15.3" version = "0.15.3"
@ -1875,6 +1972,15 @@ dependencies = [
"thread_local 1.0.1", "thread_local 1.0.1",
] ]
[[package]]
name = "regex-automata"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92b73c2a1770c255c240eaa4ee600df1704a38dc3feaa6e949e7fcd4f8dc09f9"
dependencies = [
"byteorder 1.3.4",
]
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.14" version = "0.6.14"
@ -2454,6 +2560,16 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "tinytemplate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a3c6667d3e65eb1bc3aed6fd14011c6cbc3a0665218ab7f5daf040b9ec371a"
dependencies = [
"serde",
"serde_json",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "0.1.22" version = "0.1.22"

View File

@ -32,3 +32,8 @@ version = "0.7.0"
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.7" tempdir = "0.3.7"
criterion = "0.3"
#[[bench]]
#name = "image"
#harness = false

View File

@ -7,6 +7,7 @@ use std::sync::Arc;
use google_photoslibrary1 as photos; use google_photoslibrary1 as photos;
use image::imageops::FilterType; use image::imageops::FilterType;
use image::GenericImageView;
use image::ImageFormat; use image::ImageFormat;
use log::error; use log::error;
use log::info; use log::info;
@ -18,7 +19,7 @@ use rocksdb::IteratorMode;
use rocksdb::DB; use rocksdb::DB;
// Used to ensure DB is invalidated after schema changes. // Used to ensure DB is invalidated after schema changes.
const LIBRARY_GENERATION: &'static str = "11"; const LIBRARY_GENERATION: &'static str = "12";
#[derive(Clone)] #[derive(Clone)]
pub struct Library { pub struct Library {
@ -154,8 +155,7 @@ impl Library {
pub fn generate_thumbnail( pub fn generate_thumbnail(
&self, &self,
media_items_id: &str, media_items_id: &str,
w: Option<u32>, (w, h): (Option<u32>, Option<u32>),
h: Option<u32>,
filter: FilterType, filter: FilterType,
) -> Result<Vec<u8>, io::Error> { ) -> Result<Vec<u8>, io::Error> {
match self.original(&media_items_id) { match self.original(&media_items_id) {
@ -176,6 +176,7 @@ impl Library {
(Some(w), Some(h)) => (w, h), (Some(w), Some(h)) => (w, h),
(Some(w), None) => (w, orig_h * w / orig_w), (Some(w), None) => (w, orig_h * w / orig_w),
(None, Some(h)) => (orig_w * h / orig_h, h), (None, Some(h)) => (orig_w * h / orig_h, h),
(None, None) => (orig_w, orig_h),
}; };
let img = orig_img.resize_to_fill(w, h, filter); let img = orig_img.resize_to_fill(w, h, filter);
let mut buf = Vec::new(); let mut buf = Vec::new();
@ -185,13 +186,19 @@ impl Library {
} }
} }
} }
pub fn thumbnail(&self, media_items_id: &str, dimensions: (u32, u32)) -> Option<Vec<u8>> { pub fn thumbnail(
fn cache_key(media_items_id: &str, dimensions: (u32, u32)) -> String { &self,
let (w, h) = dimensions; media_items_id: &str,
Library::generational_key( dimensions: (Option<u32>, Option<u32>),
LIBRARY_GENERATION, ) -> Option<Vec<u8>> {
&format!("{}-w={}-h={}", media_items_id, w, h), fn cache_key(media_items_id: &str, dimensions: (Option<u32>, Option<u32>)) -> String {
) let dim = match dimensions {
(Some(w), Some(h)) => format!("-w={}-h={}", w, h),
(Some(w), None) => format!("-w={}", w),
(None, Some(h)) => format!("-h={}", h),
(None, None) => "".to_string(),
};
Library::generational_key(LIBRARY_GENERATION, &format!("{}{}", media_items_id, dim))
} }
let key = cache_key(media_items_id, dimensions); let key = cache_key(media_items_id, dimensions);
let db = self.cache_db.clone(); let db = self.cache_db.clone();

View File

@ -81,10 +81,7 @@ fn image(
params: ImageParams, params: ImageParams,
) -> Result<impl warp::Reply, warp::Rejection> { ) -> Result<impl warp::Reply, warp::Rejection> {
// TODO(wathiede): add caching headers. // TODO(wathiede): add caching headers.
match lib.thumbnail( match lib.thumbnail(&media_items_id, (params.w, params.h)) {
&media_items_id,
(params.w.unwrap_or(0), params.h.unwrap_or(0)),
) {
None => { None => {
warn!("Couldn't find original {}", &media_items_id); warn!("Couldn't find original {}", &media_items_id);
Err(warp::reject::not_found()) Err(warp::reject::not_found())