diff --git a/core/src/controller.rs b/core/src/controller.rs index 5c2c594b..ab7f9df0 100644 --- a/core/src/controller.rs +++ b/core/src/controller.rs @@ -6,7 +6,6 @@ use crate::preview; use crate::repo; use crate::scanner; use crate::Result; -use std::path; /// Aggregate API for the scanner and the repository. #[derive(Debug)] @@ -33,17 +32,41 @@ impl Controller { Ok(()) } + // Gets all photos that have a preview + // TODO make a database query for this instead of filtering + pub fn all_with_previews(&self) -> Result> { + let pics = self.repo.all()?; + let pics = pics + .into_iter() + .filter(|p| p.square_preview_path.is_some()) + .collect(); + Ok(pics) + } + /// Gets all photos. pub fn all(&self) -> Result> { self.repo.all() } - pub fn add_preview(&mut self, pic: &mut repo::Picture) -> Result { - let preview = self.prev.from_picture(pic.picture_id.id(), &pic.path); - if let Ok(ref path) = preview { - pic.square_preview_path = Some(path.clone()); - self.repo.add_preview(pic)?; + pub fn update_previews(&mut self) -> Result<()> { + // TODO make a database query to get pictures + // that need a preview to be computed + let pics = self.repo.all()?; + let pics = pics + .into_iter() + .filter(|p| p.square_preview_path.is_none()) + .collect::>(); + + for mut pic in pics { + self.prev.set_preview(&mut pic)?; + self.repo.add_preview(&pic)?; } - preview + + Ok(()) + } + + pub fn add_preview(&mut self, pic: &mut repo::Picture) -> Result<()> { + self.prev.set_preview(pic)?; + self.repo.add_preview(pic) } } diff --git a/core/src/preview.rs b/core/src/preview.rs index ea8e9fff..9264c127 100644 --- a/core/src/preview.rs +++ b/core/src/preview.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: GPL-3.0-or-later +use crate::repo; use crate::Error::*; use crate::Result; use image::imageops::FilterType; @@ -26,17 +27,22 @@ impl Previewer { /// Computes a preview square for an image that has been inserted /// into the Repository. Preview image will be written to file system. - pub fn from_picture(&self, picture_id: i64, path: &path::Path) -> Result { + pub fn set_preview(&self, pic: &mut repo::Picture) -> Result<()> { + if pic.square_preview_path.is_some() { + return Ok(()); + } + let square_path = { - let file_name = format!("{}_{}x{}.jpg", picture_id, EDGE, EDGE); + let file_name = format!("{}_{}x{}.jpg", pic.picture_id, EDGE, EDGE); self.base_path.join("square").join(file_name) }; if square_path.exists() { - return Ok(square_path); + pic.square_preview_path = Some(square_path); + return Ok(()); } - let square = self.from_path(path)?; + let square = self.from_path(&pic.path)?; println!("preview = {:?}", square_path); @@ -44,11 +50,11 @@ impl Previewer { .save(&square_path) .map_err(|e| RepositoryError(e.to_string()))?; - Ok(square_path) + Ok(()) } /// Computes a preview square for an image on the file system. - pub fn from_path(&self, path: &path::Path) -> Result { + fn from_path(&self, path: &path::Path) -> Result { let img = ImageReader::open(path) .map_err(|e| PreviewError(e.to_string()))? .decode() diff --git a/src/all_photos.rs b/src/all_photos.rs index 7c89a66b..601e063b 100644 --- a/src/all_photos.rs +++ b/src/all_photos.rs @@ -52,9 +52,14 @@ impl RelmGridItem for PicturePreview { // compute preview image if it is absent if self.picture.square_preview_path.is_none() { let mut controller = self.controller.borrow_mut(); - let path = controller.add_preview(&mut self.picture); - if let Ok(p) = path { - self.picture.square_preview_path = Some(p); + match controller.add_preview(&mut self.picture) { + Ok(_) => {} + Err(e) => { + println!( + "Failed computing preview for {:?} with {:?}", + self.picture.path, e + ); + } } } @@ -84,8 +89,8 @@ impl SimpleComponent for AllPhotos { view! { gtk::Box { set_orientation: gtk::Orientation::Vertical, - set_spacing: 5, - set_margin_all: 5, + set_spacing: 0, + set_margin_all: 0, gtk::ScrolledWindow { //set_propagate_natural_height: true, @@ -95,7 +100,7 @@ impl SimpleComponent for AllPhotos { #[local_ref] pictures_box -> gtk::GridView { set_orientation: gtk::Orientation::Vertical, - set_max_columns: 3, + //set_max_columns: 3, }, }, } @@ -140,7 +145,8 @@ impl SimpleComponent for AllPhotos { let all_pictures = controller .borrow_mut() - .all() + //.all() + .all_with_previews() .unwrap() .into_iter() .map(|picture| PicturePreview {