Use cacher for downloading fullsize images.

This commit is contained in:
Bill Thiede 2020-06-20 22:25:29 -07:00
parent 3402d7bcf4
commit 58064a6309
3 changed files with 25 additions and 9 deletions

View File

@ -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<DB>,
image_cache: Arc<Box<dyn Cacher>>,
image_cache: Arc<Mutex<Box<dyn Cacher>>>,
}
impl Library {
pub fn new(
root: PathBuf,
image_cache: Arc<Box<dyn Cacher>>,
image_cache: Arc<Mutex<Box<dyn Cacher>>>,
) -> Result<Library, Box<dyn std::error::Error>> {
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(),

View File

@ -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<dyn std::error::Error>> {
.init()
.unwrap();
debug!("opt: {:?}", opt);
let image_cache: Box<dyn Cacher> = Box::new(S3Cacher::new("photosync".to_string())?);
let image_cache: Mutex<Box<dyn Cacher>> =
Mutex::new(Box::new(S3Cacher::new("photosync".to_string())?));
let image_cache = Arc::new(image_cache);
match opt.cmd {
Command::ListAlbums { auth, title_filter } => {

View File

@ -34,6 +34,7 @@ fn index() -> Result<Content<Vec<u8>>, NotFound<String>> {
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("/<path..>", rank = 99)]
fn path(path: PathBuf) -> Result<Content<Vec<u8>>, NotFound<String>> {
let path = path.to_str().unwrap();