Use cacher for downloading fullsize images.
This commit is contained in:
parent
3402d7bcf4
commit
58064a6309
@ -1,9 +1,11 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use cacher::Cacher;
|
use cacher::Cacher;
|
||||||
use google_photoslibrary1 as photos;
|
use google_photoslibrary1 as photos;
|
||||||
@ -26,13 +28,13 @@ pub struct Library {
|
|||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
originals_dir: PathBuf,
|
originals_dir: PathBuf,
|
||||||
cache_db: Arc<DB>,
|
cache_db: Arc<DB>,
|
||||||
image_cache: Arc<Box<dyn Cacher>>,
|
image_cache: Arc<Mutex<Box<dyn Cacher>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Library {
|
impl Library {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
image_cache: Arc<Box<dyn Cacher>>,
|
image_cache: Arc<Mutex<Box<dyn Cacher>>>,
|
||||||
) -> Result<Library, Box<dyn std::error::Error>> {
|
) -> Result<Library, Box<dyn std::error::Error>> {
|
||||||
let db = DB::open_default(root.join("cache"))?;
|
let db = DB::open_default(root.join("cache"))?;
|
||||||
let cache_db = Arc::new(db);
|
let cache_db = Arc::new(db);
|
||||||
@ -131,11 +133,23 @@ impl Library {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let download_path = image_path.with_extension("download");
|
let download_path = image_path.with_extension("download");
|
||||||
let url = format!("{}=d", base_url);
|
let c = Arc::clone(&self.image_cache);
|
||||||
let mut r = reqwest::blocking::get(&url)?;
|
let mut c = c.lock().unwrap();
|
||||||
let mut w = File::create(&download_path)?;
|
match c.get(media_items_id) {
|
||||||
info!("Downloading {}", &url);
|
Some(bytes) => {
|
||||||
let _n = io::copy(&mut r, &mut w)?;
|
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!(
|
info!(
|
||||||
"Rename {} -> {}",
|
"Rename {} -> {}",
|
||||||
download_path.to_string_lossy(),
|
download_path.to_string_lossy(),
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
@ -322,7 +322,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.init()
|
.init()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
debug!("opt: {:?}", opt);
|
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);
|
let image_cache = Arc::new(image_cache);
|
||||||
match opt.cmd {
|
match opt.cmd {
|
||||||
Command::ListAlbums { auth, title_filter } => {
|
Command::ListAlbums { auth, title_filter } => {
|
||||||
|
|||||||
@ -34,6 +34,7 @@ fn index() -> Result<Content<Vec<u8>>, NotFound<String>> {
|
|||||||
file("index.html")
|
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)]
|
#[get("/<path..>", rank = 99)]
|
||||||
fn path(path: PathBuf) -> Result<Content<Vec<u8>>, NotFound<String>> {
|
fn path(path: PathBuf) -> Result<Content<Vec<u8>>, NotFound<String>> {
|
||||||
let path = path.to_str().unwrap();
|
let path = path.to_str().unwrap();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user