From 58064a6309d01836c9902243bee2eb0f18f950ec Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 20 Jun 2020 22:25:29 -0700 Subject: [PATCH] Use cacher for downloading fullsize images. --- src/library.rs | 28 +++++++++++++++++++++------- src/main.rs | 5 +++-- src/rweb.rs | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/library.rs b/src/library.rs index df132aa..2a44d98 100644 --- a/src/library.rs +++ b/src/library.rs @@ -1,9 +1,11 @@ use std::fs; use std::fs::File; use std::io; +use std::io::Read; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; +use std::sync::Mutex; use cacher::Cacher; use google_photoslibrary1 as photos; @@ -26,13 +28,13 @@ pub struct Library { root: PathBuf, originals_dir: PathBuf, cache_db: Arc, - image_cache: Arc>, + image_cache: Arc>>, } impl Library { pub fn new( root: PathBuf, - image_cache: Arc>, + image_cache: Arc>>, ) -> Result> { let db = DB::open_default(root.join("cache"))?; let cache_db = Arc::new(db); @@ -131,11 +133,23 @@ impl Library { ); } else { let download_path = image_path.with_extension("download"); - let url = format!("{}=d", base_url); - let mut r = reqwest::blocking::get(&url)?; - let mut w = File::create(&download_path)?; - info!("Downloading {}", &url); - let _n = io::copy(&mut r, &mut w)?; + let c = Arc::clone(&self.image_cache); + let mut c = c.lock().unwrap(); + match c.get(media_items_id) { + Some(bytes) => { + info!("saving local copy from cache {}", media_items_id); + fs::write(&download_path, bytes)?; + } + None => { + let url = format!("{}=d", base_url); + let mut r = reqwest::blocking::get(&url)?; + let mut buf = Vec::new(); + info!("Downloading {}", &url); + r.read_to_end(&mut buf)?; + fs::write(&download_path, &buf); + c.set(media_items_id, &buf); + } + }; info!( "Rename {} -> {}", download_path.to_string_lossy(), diff --git a/src/main.rs b/src/main.rs index 73f5645..81ae0f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::error::Error; use std::net::SocketAddr; use std::path::PathBuf; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::thread; use std::time; @@ -322,7 +322,8 @@ fn main() -> Result<(), Box> { .init() .unwrap(); debug!("opt: {:?}", opt); - let image_cache: Box = Box::new(S3Cacher::new("photosync".to_string())?); + let image_cache: Mutex> = + Mutex::new(Box::new(S3Cacher::new("photosync".to_string())?)); let image_cache = Arc::new(image_cache); match opt.cmd { Command::ListAlbums { auth, title_filter } => { diff --git a/src/rweb.rs b/src/rweb.rs index 88a1058..66df9c3 100644 --- a/src/rweb.rs +++ b/src/rweb.rs @@ -34,6 +34,7 @@ fn index() -> Result>, NotFound> { file("index.html") } +// This is the catch-all handler, it has a high rank so it is the last match in any tie-breaks. #[get("/", rank = 99)] fn path(path: PathBuf) -> Result>, NotFound> { let path = path.to_str().unwrap();