Refactor thumbnail generation.
This commit is contained in:
parent
43974b7406
commit
d74a8117d7
@ -6,6 +6,8 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use google_photoslibrary1 as photos;
|
||||
use image::imageops::FilterType;
|
||||
use image::ImageFormat;
|
||||
use log::error;
|
||||
use log::info;
|
||||
use log::warn;
|
||||
@ -16,7 +18,7 @@ use rocksdb::IteratorMode;
|
||||
use rocksdb::DB;
|
||||
|
||||
// Used to ensure DB is invalidated after schema changes.
|
||||
const LIBRARY_GENERATION: &'static str = "2";
|
||||
const LIBRARY_GENERATION: &'static str = "5";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Library {
|
||||
@ -143,14 +145,14 @@ impl Library {
|
||||
fn generational_key(generation: &str, key: &str) -> String {
|
||||
format!("{}/{}", generation, key)
|
||||
}
|
||||
pub fn thumbnail(&self, media_items_id: &str, dimensions: (u32, u32)) -> Option<Vec<u8>> {
|
||||
fn generate_thumbnail(
|
||||
lib: &Library,
|
||||
pub fn generate_thumbnail(
|
||||
&self,
|
||||
media_items_id: &str,
|
||||
dimensions: (u32, u32),
|
||||
filter: FilterType,
|
||||
) -> Result<Vec<u8>, io::Error> {
|
||||
let (w, h) = dimensions;
|
||||
match lib.original(&media_items_id) {
|
||||
match self.original(&media_items_id) {
|
||||
None => {
|
||||
warn!("Couldn't find original {}", &media_items_id);
|
||||
Err(io::Error::new(
|
||||
@ -163,16 +165,15 @@ impl Library {
|
||||
.with_guessed_format()?
|
||||
.decode()
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||
// TODO (wathiede) resize..fill
|
||||
let img =
|
||||
orig_img.resize_to_fill(w, h, image::imageops::FilterType::CatmullRom);
|
||||
let img = orig_img.resize_to_fill(w, h, filter);
|
||||
let mut buf = Vec::new();
|
||||
img.write_to(&mut buf, image::ImageFormat::Jpeg)
|
||||
img.write_to(&mut buf, ImageFormat::Jpeg)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||
Ok(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn thumbnail(&self, media_items_id: &str, dimensions: (u32, u32)) -> Option<Vec<u8>> {
|
||||
fn cache_key(media_items_id: &str, dimensions: (u32, u32)) -> String {
|
||||
let (w, h) = dimensions;
|
||||
Library::generational_key(
|
||||
@ -191,7 +192,9 @@ impl Library {
|
||||
// Cache miss, fill cache and return.
|
||||
Ok(None) => {
|
||||
info!("cache MISS {}", key);
|
||||
let bytes = match generate_thumbnail(self, media_items_id, dimensions) {
|
||||
let bytes =
|
||||
match self.generate_thumbnail(media_items_id, dimensions, FilterType::Lanczos3)
|
||||
{
|
||||
Ok(bytes) => bytes,
|
||||
Err(e) => {
|
||||
error!("Failed to generate thumbnail for {}: {}", media_items_id, e);
|
||||
|
||||
@ -80,6 +80,7 @@ fn image(
|
||||
media_items_id: String,
|
||||
params: ImageParams,
|
||||
) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
// TODO(wathiede): add caching headers.
|
||||
match lib.thumbnail(
|
||||
&media_items_id,
|
||||
(params.w.unwrap_or(0), params.h.unwrap_or(0)),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user