Skip to content

Commit

Permalink
Add url_path attribute to resize_image output
Browse files Browse the repository at this point in the history
  • Loading branch information
syphoxy committed Sep 16, 2022
1 parent ad6c834 commit 50f68ef
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
26 changes: 9 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -36,7 +36,7 @@ pathdiff = "0.2"
mime_guess = "2.0"
# For essence_str() function, see https://github.com/getzola/zola/issues/1845
mime = "0.3.16"

url = "2.3.1"

site = { path = "components/site" }
errors = { path = "components/errors" }
Expand Down
29 changes: 29 additions & 0 deletions components/config/src/config/mod.rs
Expand Up @@ -10,6 +10,7 @@ use std::path::{Path, PathBuf};

use libs::globset::{Glob, GlobSet, GlobSetBuilder};
use libs::toml::Value as Toml;
use libs::url::Url;
use serde::{Deserialize, Serialize};

use crate::theme::Theme;
Expand Down Expand Up @@ -203,6 +204,34 @@ impl Config {
}
}

/// Makes an absolute path based on the base url, taking into account that the base url might
/// have a trailing slash
pub fn make_absolute_path(&self, path: &str) -> String {
let trailing_bit =
if path.ends_with('/') || path.ends_with(&self.feed_filename) || path.is_empty() {
""
} else {
"/"
};

let url = Url::parse(&self.base_url).unwrap();
let url_path = url.path();

// Index section with a base url that has a trailing slash
if url_path.ends_with('/') && path == "/" {
url_path.to_string()
} else if path == "/" {
// index section with a base url that doesn't have a trailing slash
format!("{}/", url_path)
} else if url_path.ends_with('/') && path.starts_with('/') {
format!("{}{}{}", url_path, &path[1..], trailing_bit)
} else if url_path.ends_with('/') || path.starts_with('/') {
format!("{}{}{}", url_path, path, trailing_bit)
} else {
format!("{}/{}{}", url_path, path, trailing_bit)
}
}

/// Adds the default language to the list of languages if not present
pub fn add_default_language(&mut self) {
// We automatically insert a language option for the default language *if* it isn't present
Expand Down
21 changes: 15 additions & 6 deletions components/imageproc/src/lib.rs
Expand Up @@ -375,6 +375,8 @@ pub fn fix_orientation(img: &DynamicImage, path: &Path) -> Option<DynamicImage>
pub struct EnqueueResponse {
/// The final URL for that asset
pub url: String,
/// The final absolute path for that asset
pub url_path: String,
/// The path to the static asset generated
pub static_path: String,
/// New image width
Expand All @@ -388,12 +390,12 @@ pub struct EnqueueResponse {
}

impl EnqueueResponse {
fn new(url: String, static_path: PathBuf, meta: &ImageMeta, op: &ResizeOp) -> Self {
fn new(url: String, url_path: String, static_path: PathBuf, meta: &ImageMeta, op: &ResizeOp) -> Self {
let static_path = static_path.to_string_lossy().into_owned();
let (width, height) = op.resize.unwrap_or(meta.size);
let (orig_width, orig_height) = meta.size;

Self { url, static_path, width, height, orig_width, orig_height }
Self { url, url_path, static_path, width, height, orig_width, orig_height }
}
}

Expand All @@ -403,6 +405,7 @@ impl EnqueueResponse {
#[derive(Debug)]
pub struct Processor {
base_url: String,
base_path: String,
output_dir: PathBuf,
/// A map of a ImageOps by their stored hash.
/// Note that this cannot be a HashSet, because hashset handles collisions and we don't want that,
Expand All @@ -417,6 +420,7 @@ impl Processor {
Processor {
output_dir: base_path.join("static").join(RESIZED_SUBDIR),
base_url: config.make_permalink(RESIZED_SUBDIR),
base_path: config.make_absolute_path(RESIZED_SUBDIR),
img_ops: HashMap::new(),
img_ops_collisions: Vec::new(),
}
Expand All @@ -426,6 +430,10 @@ impl Processor {
self.base_url = config.make_permalink(RESIZED_SUBDIR);
}

pub fn set_base_path(&mut self, config: &Config) {
self.base_path = config.make_absolute_path(RESIZED_SUBDIR);
}

pub fn num_img_ops(&self) -> usize {
self.img_ops.len() + self.img_ops_collisions.len()
}
Expand All @@ -448,9 +456,9 @@ impl Processor {
let op = ResizeOp::new(args, meta.size);
let format = Format::from_args(&meta, format, quality)?;
let img_op = ImageOp::new(input_src, input_path, op.clone(), format);
let (static_path, url) = self.insert(img_op);
let (static_path, url, url_path) = self.insert(img_op);

Ok(EnqueueResponse::new(url, static_path, &meta, &op))
Ok(EnqueueResponse::new(url, url_path, static_path, &meta, &op))
}

fn insert_with_collisions(&mut self, mut img_op: ImageOp) -> u32 {
Expand Down Expand Up @@ -507,13 +515,14 @@ impl Processor {

/// Adds the given operation to the queue but do not process it immediately.
/// Returns (path in static folder, final URL).
fn insert(&mut self, img_op: ImageOp) -> (PathBuf, String) {
fn insert(&mut self, img_op: ImageOp) -> (PathBuf, String, String) {
let hash = img_op.hash;
let format = img_op.format;
let collision_id = self.insert_with_collisions(img_op);
let filename = Self::op_filename(hash, collision_id, format);
let url = format!("{}{}", self.base_url, filename);
(Path::new("static").join(RESIZED_SUBDIR).join(filename), url)
let url_path = format!("{}{}", self.base_path, filename);
(Path::new("static").join(RESIZED_SUBDIR).join(filename), url, url_path)
}

/// Remove stale processed images in the output directory
Expand Down

0 comments on commit 50f68ef

Please sign in to comment.