Skip to content

Commit

Permalink
refactor preview
Browse files Browse the repository at this point in the history
  • Loading branch information
blissd committed Mar 24, 2024
1 parent cb3213c commit a6b2758
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
37 changes: 30 additions & 7 deletions core/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<Vec<repo::Picture>> {
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<Vec<repo::Picture>> {
self.repo.all()
}

pub fn add_preview(&mut self, pic: &mut repo::Picture) -> Result<path::PathBuf> {
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::<Vec<repo::Picture>>();

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)
}
}
18 changes: 12 additions & 6 deletions core/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,29 +27,34 @@ 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<path::PathBuf> {
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);

square
.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<DynamicImage> {
fn from_path(&self, path: &path::Path) -> Result<DynamicImage> {
let img = ImageReader::open(path)
.map_err(|e| PreviewError(e.to_string()))?
.decode()
Expand Down
20 changes: 13 additions & 7 deletions src/all_photos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
},
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a6b2758

Please sign in to comment.