diff options
author | Julian T <julian@jtle.dk> | 2021-07-26 23:50:07 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-07-26 23:50:40 +0200 |
commit | d7244c2cc7b8f62f2f42cca7f3dece89c30bf105 (patch) | |
tree | 8b7bae761350bc05e1db0cc338daffc947a8e745 /src | |
parent | 0ddf24fa0be620fc10cc07247b2c738b32943042 (diff) |
Add scaled image to page
Diffstat (limited to 'src')
-rw-r--r-- | src/context.rs | 15 | ||||
-rw-r--r-- | src/picture.rs | 6 | ||||
-rw-r--r-- | src/piece.rs | 15 |
3 files changed, 22 insertions, 14 deletions
diff --git a/src/context.rs b/src/context.rs index 925db0b..f7b97ed 100644 --- a/src/context.rs +++ b/src/context.rs @@ -4,12 +4,12 @@ use std::io; use std::path::{Path, PathBuf}; use structopt::StructOpt; -use serde::Deserialize; +use serde::{Serialize, Deserialize}; use tera::{Tera, Filter, Value}; const TMPL_FILES: &'static [&'static str] = &["index.html"]; -#[derive(Debug, StructOpt)] +#[derive(Debug, StructOpt, Serialize)] #[structopt(name = "gallery")] pub struct Options { #[structopt(long, help = "Just a thing")] @@ -23,10 +23,8 @@ pub struct Options { #[structopt(long, short, default_value = "build", help = "Where to build site")] pub builddir: PathBuf, - #[structopt(long, default_value = "1080", help = "Scaled size for image")] - pub size_scaled: u32, - #[structopt(long, default_value = "720", help = "Thumbnail size for image")] - pub size_thumb: u32, + #[structopt(long, help = "Full size url prefix")] + pub url_prefix: Option<String>, } #[derive(Debug)] @@ -37,10 +35,11 @@ pub enum ConfigError { CompileTemplate(tera::Error), } -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, Serialize)] pub struct Config { pub imageglob: String, pub info: HashMap<String, String>, + pub sizes: HashMap<String, u32>, #[serde(skip)] pub imageglob_compiled: glob::Pattern, @@ -126,8 +125,6 @@ impl Filter for ReltoFilter { Err(tera::Error::msg("Input to relto filter must be string")) } } - - fn is_safe(&self) -> bool { true } } impl Config { diff --git a/src/picture.rs b/src/picture.rs index 76a33df..3f78f90 100644 --- a/src/picture.rs +++ b/src/picture.rs @@ -11,6 +11,7 @@ use crate::context::Context; #[derive(Debug)] pub enum LoadError { + PathError, Io(io::Error), ExifParser(exif::Error) } @@ -26,6 +27,7 @@ pub struct Picture { pub taken: Option<String>, hash: String, pub path: PathBuf, + pub file_name: String, } pub struct Converter<'a> { @@ -72,6 +74,10 @@ impl Picture { taken, hash: hash_reader(&mut reader)?, path: path.to_path_buf(), + file_name: match path.file_name() { + Some(fname) => Ok(fname.to_string_lossy().to_string()), + None => Err(LoadError::PathError), + }?, }) } diff --git a/src/piece.rs b/src/piece.rs index 2c0b545..1ebad66 100644 --- a/src/piece.rs +++ b/src/piece.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use std::collections::HashMap; use crate::picture::{Picture, ConversionError}; use crate::context::Context; use tera::Context as WebContext; @@ -8,14 +9,14 @@ use serde::Serialize; pub struct Piece { pic: Picture, - scaled_path: PathBuf, - //thumb_path: PathBuf, + scaled: HashMap<String, PathBuf>, info: Option<String>, } pub fn create_index(ctx: &Context, pieces: &[Piece]) -> Result<(), tera::Error> { let mut web_ctx = WebContext::new(); web_ctx.insert("pieces", pieces); + web_ctx.insert("opts", &ctx.options); println!("render: {}", ctx.tmpl.render("index.html", &web_ctx)?); Ok(()) @@ -25,15 +26,19 @@ impl Piece { pub fn new(ctx: &Context, pic: Picture) -> Result<Self, ConversionError> { println!("Creating piece from file {}", pic.path.display()); let mut conv = pic.convert()?; - let scaled_path = conv.get_size(ctx, ctx.options.size_scaled)?; - //let thumb_path = conv.get_size(ctx, ctx.options.size_thumb)?; + let mut scaled = HashMap::with_capacity(ctx.config.sizes.capacity()); + + // Create all needed sizes + for (id, size) in ctx.config.sizes.iter() { + scaled.insert(id.clone(), conv.get_size(ctx, *size)?); + } let info: Option<String> = pic.path.file_name() .map(|name| name.to_str()).flatten() .map(|name| ctx.config.info.get(name).map(|s| s.clone())).flatten(); Ok(Piece { - pic, scaled_path, info, + pic, scaled, info, }) } } |