Attempt to create a cacher but I can't make it work with warp.
This commit is contained in:
@@ -5,6 +5,7 @@ use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use cacher::Cacher;
|
||||
use google_photoslibrary1 as photos;
|
||||
use image::imageops;
|
||||
use imageutils::{load_image, resize, resize_to_fill, save_to_jpeg_bytes, FilterType};
|
||||
@@ -21,20 +22,28 @@ use rocksdb::DB;
|
||||
const LIBRARY_GENERATION: &'static str = "14";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Library {
|
||||
pub struct Library<C>
|
||||
where
|
||||
C: Cacher,
|
||||
{
|
||||
root: PathBuf,
|
||||
originals_dir: PathBuf,
|
||||
cache_db: Arc<DB>,
|
||||
image_cache: C,
|
||||
}
|
||||
|
||||
impl Library {
|
||||
pub fn new(root: PathBuf) -> Result<Library, Box<dyn std::error::Error>> {
|
||||
impl<C> Library<C>
|
||||
where
|
||||
C: Cacher,
|
||||
{
|
||||
pub fn new(root: PathBuf, image_cache: C) -> Result<Library<C>, Box<dyn std::error::Error>> {
|
||||
let db = DB::open_default(root.join("cache"))?;
|
||||
let cache_db = Arc::new(db);
|
||||
let lib = Library {
|
||||
originals_dir: root.join("images").join("originals"),
|
||||
cache_db,
|
||||
root,
|
||||
image_cache,
|
||||
};
|
||||
let cnt = lib.clean_db()?;
|
||||
if cnt != 0 {
|
||||
@@ -51,7 +60,7 @@ impl Library {
|
||||
}
|
||||
// Removes all data in the database from older schema.
|
||||
pub fn clean_db(&self) -> Result<usize, rocksdb::Error> {
|
||||
Library::gc(LIBRARY_GENERATION, &self.cache_db)
|
||||
Library::<C>::gc(LIBRARY_GENERATION, &self.cache_db)
|
||||
}
|
||||
fn gc(generation: &str, db: &DB) -> Result<usize, rocksdb::Error> {
|
||||
let gen = format!("{}/", generation);
|
||||
@@ -184,16 +193,22 @@ impl Library {
|
||||
dimensions: (Option<u32>, Option<u32>),
|
||||
fill: bool,
|
||||
) -> Option<Vec<u8>> {
|
||||
fn cache_key(media_items_id: &str, dimensions: (Option<u32>, Option<u32>)) -> String {
|
||||
fn cache_key<C: Cacher>(
|
||||
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))
|
||||
Library::<C>::generational_key(
|
||||
LIBRARY_GENERATION,
|
||||
&format!("{}{}", media_items_id, dim),
|
||||
)
|
||||
}
|
||||
let key = cache_key(media_items_id, dimensions);
|
||||
let key = cache_key::<C>(media_items_id, dimensions);
|
||||
let db = self.cache_db.clone();
|
||||
match db.get(key.as_bytes()) {
|
||||
// Cache hit, return bytes as-is.
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::path::PathBuf;
|
||||
use std::thread;
|
||||
use std::time;
|
||||
|
||||
use cacher::S3Cacher;
|
||||
use google_api_auth;
|
||||
use google_photoslibrary1 as photos;
|
||||
use hexihasher;
|
||||
@@ -320,6 +321,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.init()
|
||||
.unwrap();
|
||||
debug!("opt: {:?}", opt);
|
||||
let image_cache = S3Cacher::new("photosync")?;
|
||||
match opt.cmd {
|
||||
Command::ListAlbums { auth, title_filter } => {
|
||||
let client = new_client(&auth.credentials, &auth.token_cache)?;
|
||||
@@ -340,14 +342,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
},
|
||||
} => {
|
||||
let client = new_client(&auth.credentials, &auth.token_cache)?;
|
||||
let lib = Library::new(root)?;
|
||||
let lib = Library::new(root, image_cache)?;
|
||||
sync_albums(&client, &title_filter, &lib)?;
|
||||
Ok(())
|
||||
}
|
||||
Command::Serve {
|
||||
serve: Serve { addr, root },
|
||||
} => {
|
||||
let lib = Library::new(root)?;
|
||||
let lib = Library::new(root, image_cache)?;
|
||||
serve(addr, lib)
|
||||
}
|
||||
Command::ServeAndSync {
|
||||
@@ -361,7 +363,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
addr,
|
||||
} => {
|
||||
let client = new_client(&auth.credentials, &auth.token_cache)?;
|
||||
let lib = Library::new(root)?;
|
||||
let lib = Library::new(root, image_cache)?;
|
||||
background_sync(client, interval, title_filter, lib.clone())?;
|
||||
serve(addr, lib)?;
|
||||
Ok(())
|
||||
|
||||
17
src/web.rs
17
src/web.rs
@@ -2,6 +2,7 @@ use std::error::Error;
|
||||
use std::io::Write;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use cacher::Cacher;
|
||||
use log::warn;
|
||||
use prometheus::Encoder;
|
||||
use rust_embed::RustEmbed;
|
||||
@@ -53,7 +54,7 @@ fn index(path: warp::path::FullPath) -> Result<impl warp::Reply, warp::Rejection
|
||||
}
|
||||
}
|
||||
|
||||
fn albums(lib: Library) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
fn albums<C: Cacher>(lib: Library<C>) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
let albums = lib.albums().map_err(|e| {
|
||||
warn!("Couldn't find albums: {}", e);
|
||||
warp::reject::not_found()
|
||||
@@ -61,7 +62,7 @@ fn albums(lib: Library) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
Ok(warp::reply::json(&albums))
|
||||
}
|
||||
|
||||
fn album(lib: Library, id: String) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
fn album<C: Cacher>(lib: Library<C>, id: String) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
let album = lib.album(&id).map_err(|e| {
|
||||
warn!("Couldn't find album {}: {}", id, e);
|
||||
warp::reject::not_found()
|
||||
@@ -76,8 +77,8 @@ struct ImageParams {
|
||||
fill: Option<bool>,
|
||||
}
|
||||
|
||||
fn image(
|
||||
lib: Library,
|
||||
fn image<C: Cacher>(
|
||||
lib: Library<C>,
|
||||
media_items_id: String,
|
||||
params: ImageParams,
|
||||
) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
@@ -122,24 +123,24 @@ fn embedz() -> Result<impl warp::Reply, warp::Rejection> {
|
||||
.body(w))
|
||||
}
|
||||
|
||||
pub fn run(addr: SocketAddr, lib: Library) -> Result<(), Box<dyn Error>> {
|
||||
pub fn run<C: Cacher>(addr: SocketAddr, lib: Library<C>) -> Result<(), Box<dyn Error>> {
|
||||
let lib = warp::any().map(move || lib.clone());
|
||||
|
||||
let index = warp::get2().and(warp::path::full()).and_then(index);
|
||||
|
||||
let albums = warp::path("albums").and(lib.clone()).and_then(albums);
|
||||
let albums = warp::path("albums").and(lib.clone()).and_then(index);
|
||||
let embedz = warp::path("embedz").and_then(embedz);
|
||||
|
||||
let album = warp::path("album")
|
||||
.and(lib.clone())
|
||||
.and(warp::path::param())
|
||||
.and_then(album);
|
||||
.and_then(index);
|
||||
|
||||
let image = warp::path("image")
|
||||
.and(lib.clone())
|
||||
.and(warp::path::param())
|
||||
.and(warp::query::<ImageParams>())
|
||||
.and_then(image);
|
||||
.and_then(index);
|
||||
|
||||
let api = albums.or(album).or(image);
|
||||
let api = warp::path("api").and(api);
|
||||
|
||||
Reference in New Issue
Block a user