Store thumbnails in rocksdb.

This commit is contained in:
2020-02-17 14:50:09 -08:00
parent d07542b544
commit 914e30365e
4 changed files with 414 additions and 71 deletions

View File

@@ -2,7 +2,6 @@ use std::error::Error;
use std::net::SocketAddr;
use std::path::PathBuf;
use log::info;
use log::warn;
use prometheus::Encoder;
use rust_embed::RustEmbed;
@@ -32,6 +31,7 @@ fn metrics() -> impl Filter<Extract = (impl warp::reply::Reply,), Error = Reject
.with(warp::reply::with::headers(text_headers))
}
// TODO(wathiede): add caching for hashed files. Add at least etag for everything.
fn index(path: warp::path::FullPath) -> Result<impl warp::Reply, warp::Rejection> {
let path = path.as_str();
let path = if path.ends_with("/") {
@@ -47,7 +47,6 @@ fn index(path: warp::path::FullPath) -> Result<impl warp::Reply, warp::Rejection
Ok(warp::http::Response::builder()
.header("Content-Type", mime.essence_str())
.header("Content-Length", bytes.len())
.body(bytes.into_owned()))
}
None => Err(warp::reject::not_found()),
@@ -74,7 +73,6 @@ fn album(lib: Library, id: String) -> Result<impl warp::Reply, warp::Rejection>
struct ImageParams {
w: Option<u32>,
h: Option<u32>,
c: Option<bool>,
}
fn image(
@@ -82,42 +80,17 @@ fn image(
media_items_id: String,
params: ImageParams,
) -> Result<impl warp::Reply, warp::Rejection> {
info!("image_id {} params {:?}", media_items_id, params);
match lib.original(&media_items_id) {
match lib.thumbnail(
&media_items_id,
(params.w.unwrap_or(0), params.h.unwrap_or(0)),
) {
None => {
warn!("Couldn't find original {}", &media_items_id);
Err(warp::reject::not_found())
}
Some(path) => {
let orig_img = image::io::Reader::open(&path)
.map_err(|e| {
warn!("Couldn't open {}: {}", path.to_string_lossy(), e);
warp::reject::not_found()
})?
.with_guessed_format()
.map_err(|e| {
warn!("Couldn't guess format {}: {}", path.to_string_lossy(), e);
warp::reject::not_found()
})?
.decode()
.map_err(|e| {
warn!("Couldn't decode {}: {}", path.to_string_lossy(), e);
warp::reject::not_found()
})?;
let img = orig_img.resize(
params.w.unwrap_or(0),
params.h.unwrap_or(0),
image::imageops::FilterType::CatmullRom,
);
let mut buf = Vec::new();
img.write_to(&mut buf, image::ImageFormat::Jpeg)
.map_err(|e| {
warn!("Couldn't write_to {}: {}", path.to_string_lossy(), e);
warp::reject::not_found()
})?;
Ok(buf)
}
Some(bytes) => Ok(warp::http::Response::builder()
.header("Content-Type", "image/jpeg")
.body(bytes)),
}
}