diff --git a/src/library.rs b/src/library.rs index afefd54..7d95493 100644 --- a/src/library.rs +++ b/src/library.rs @@ -18,7 +18,7 @@ use rocksdb::IteratorMode; use rocksdb::DB; // Used to ensure DB is invalidated after schema changes. -const LIBRARY_GENERATION: &'static str = "5"; +const LIBRARY_GENERATION: &'static str = "11"; #[derive(Clone)] pub struct Library { @@ -36,7 +36,10 @@ impl Library { cache_db, root, }; - lib.clean_db()?; + let cnt = lib.clean_db()?; + if cnt != 0 { + info!("Deleted {} entries", cnt); + } if !lib.originals_dir.exists() { info!( "create originals dir {}", @@ -47,27 +50,30 @@ impl Library { Ok(lib) } // Removes all data in the database from older schema. - pub fn clean_db(&self) -> Result<(), rocksdb::Error> { + pub fn clean_db(&self) -> Result { Library::gc(LIBRARY_GENERATION, &self.cache_db) } - fn gc(generation: &str, db: &DB) -> Result<(), rocksdb::Error> { + fn gc(generation: &str, db: &DB) -> Result { let gen = format!("{}/", generation); // '0' is the next character after '/', so iterator's starting there would be after the // last `gen` entry. let next_gen = format!("{}0", generation); + let mut del_cnt = 0; for (k, _v) in db.iterator(IteratorMode::From(gen.as_bytes(), Direction::Reverse)) { if !k.starts_with(gen.as_bytes()) { info!("deleting stale key: {}", String::from_utf8_lossy(&k)); db.delete(k)?; + del_cnt += 1; } } for (k, _v) in db.iterator(IteratorMode::From(next_gen.as_bytes(), Direction::Forward)) { if !k.starts_with(gen.as_bytes()) { info!("deleting stale key: {}", String::from_utf8_lossy(&k)); db.delete(k)?; + del_cnt += 1; } } - Ok(()) + Ok(del_cnt) } pub fn create_album_index(&self, albums: &Vec) -> io::Result<()> { // Serialize it to a JSON string. @@ -148,10 +154,10 @@ impl Library { pub fn generate_thumbnail( &self, media_items_id: &str, - dimensions: (u32, u32), + w: Option, + h: Option, filter: FilterType, ) -> Result, io::Error> { - let (w, h) = dimensions; match self.original(&media_items_id) { None => { warn!("Couldn't find original {}", &media_items_id); @@ -165,6 +171,12 @@ impl Library { .with_guessed_format()? .decode() .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + let (orig_w, orig_h) = orig_img.dimensions(); + let (w, h) = match (w, h) { + (Some(w), Some(h)) => (w, h), + (Some(w), None) => (w, orig_h * w / orig_w), + (None, Some(h)) => (orig_w * h / orig_h, h), + }; let img = orig_img.resize_to_fill(w, h, filter); let mut buf = Vec::new(); img.write_to(&mut buf, ImageFormat::Jpeg)