Finish implementation of option parameter resize.

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

View File

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

View File

@@ -81,10 +81,7 @@ fn image(
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)),
) {
match lib.thumbnail(&media_items_id, (params.w, params.h)) {
None => {
warn!("Couldn't find original {}", &media_items_id);
Err(warp::reject::not_found())