Hash imageproc on unified path

This commit is contained in:
Vincent Prouillet 2021-06-09 14:55:13 +02:00
parent 38ddb1c4d1
commit 0975b674c5
4 changed files with 14 additions and 25 deletions

View file

@ -115,7 +115,7 @@ impl TeraFn for GetUrl {
if cachebust { if cachebust {
match search_for_file(&self.base_path, &path_with_lang) match search_for_file(&self.base_path, &path_with_lang)
.and_then(|p| fs::File::open(&p).ok()) .and_then(|(p, _)| fs::File::open(&p).ok())
.and_then(|f| compute_file_hash::<Sha256>(f, false).ok()) .and_then(|f| compute_file_hash::<Sha256>(f, false).ok())
{ {
Some(hash) => { Some(hash) => {
@ -167,7 +167,7 @@ impl TeraFn for GetFileHash {
.unwrap_or(true); .unwrap_or(true);
let file_path = match search_for_file(&self.base_path, &path) { let file_path = match search_for_file(&self.base_path, &path) {
Some(f) => f, Some((f, _)) => f,
None => { None => {
return Err(format!("`get_file_hash`: Cannot find file: {}", path).into()); return Err(format!("`get_file_hash`: Cannot find file: {}", path).into());
} }

View file

@ -8,7 +8,8 @@ use std::path::{Path, PathBuf};
/// 3. base_path + content + path /// 3. base_path + content + path
/// A path starting with @/ will replace it with `content/` and a path starting with `/` will have /// A path starting with @/ will replace it with `content/` and a path starting with `/` will have
/// it removed. /// it removed.
pub fn search_for_file(base_path: &Path, path: &str) -> Option<PathBuf> { /// It also returns the unified path so it can be used as unique hash for a given file.
pub fn search_for_file(base_path: &Path, path: &str) -> Option<(PathBuf, String)> {
let search_paths = [base_path.join("static"), base_path.join("content")]; let search_paths = [base_path.join("static"), base_path.join("content")];
let actual_path = if path.starts_with("@/") { let actual_path = if path.starts_with("@/") {
Cow::Owned(path.replace("@/", "content/")) Cow::Owned(path.replace("@/", "content/"))
@ -31,7 +32,7 @@ pub fn search_for_file(base_path: &Path, path: &str) -> Option<PathBuf> {
} }
if file_exists { if file_exists {
Some(file_path) Some((file_path, actual_path.into_owned()))
} else { } else {
None None
} }

View file

@ -55,7 +55,7 @@ impl TeraFn for ResizeImage {
} }
let mut imageproc = self.imageproc.lock().unwrap(); let mut imageproc = self.imageproc.lock().unwrap();
let file_path = match search_for_file(&self.base_path, &path) { let (file_path, unified_path) = match search_for_file(&self.base_path, &path) {
Some(f) => f, Some(f) => f,
None => { None => {
return Err(format!("`resize_image`: Cannot find file: {}", path).into()); return Err(format!("`resize_image`: Cannot find file: {}", path).into());
@ -63,7 +63,7 @@ impl TeraFn for ResizeImage {
}; };
let response = imageproc let response = imageproc
.enqueue(path, file_path, &op, width, height, &format, quality) .enqueue(unified_path, file_path, &op, width, height, &format, quality)
.map_err(|e| format!("`resize_image`: {}", e))?; .map_err(|e| format!("`resize_image`: {}", e))?;
to_value(response).map_err(Into::into) to_value(response).map_err(Into::into)
@ -95,8 +95,8 @@ impl TeraFn for GetImageMetadata {
"`get_image_metadata`: `allow_missing` must be a boolean (true or false)" "`get_image_metadata`: `allow_missing` must be a boolean (true or false)"
) )
.unwrap_or(false); .unwrap_or(false);
let src_path = match search_for_file(&self.base_path, &path) { let (src_path, _) = match search_for_file(&self.base_path, &path) {
Some(f) => f, Some((f, p)) => (f, p),
None => { None => {
if allow_missing { if allow_missing {
println!("Image at path {} could not be found or loaded", path); println!("Image at path {} could not be found or loaded", path);
@ -180,25 +180,13 @@ mod tests {
// 3. resizing with an absolute path is the same as the above // 3. resizing with an absolute path is the same as the above
args.insert("path".to_string(), to_value("/content/gutenberg.jpg").unwrap()); args.insert("path".to_string(), to_value("/content/gutenberg.jpg").unwrap());
assert_eq!( let data2 = static_fn.call(&args).unwrap().as_object().unwrap().clone();
data["static_path"], assert_eq!(data, data2);
to_value(&format!("{}", static_path.join("202d9263f4dbc95900.jpg").display())).unwrap()
);
assert_eq!(
data["url"],
to_value("http://a-website.com/processed_images/202d9263f4dbc95900.jpg").unwrap()
);
// 4. resizing an image in content starting with `@/` is the same as 2 and 3 // 4. resizing an image in content starting with `@/` is the same as 2 and 3
args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap()); args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap());
assert_eq!( let data2 = static_fn.call(&args).unwrap().as_object().unwrap().clone();
data["static_path"], assert_eq!(data, data2);
to_value(&format!("{}", static_path.join("202d9263f4dbc95900.jpg").display())).unwrap()
);
assert_eq!(
data["url"],
to_value("http://a-website.com/processed_images/202d9263f4dbc95900.jpg").unwrap()
);
// 5. resizing an image with a relative path not starting with static or content // 5. resizing an image with a relative path not starting with static or content
args.insert("path".to_string(), to_value("gallery/asset.jpg").unwrap()); args.insert("path".to_string(), to_value("gallery/asset.jpg").unwrap());

View file

@ -95,7 +95,7 @@ impl DataSource {
if let Some(path) = path_arg { if let Some(path) = path_arg {
return match search_for_file(&base_path, &path) { return match search_for_file(&base_path, &path) {
Some(f) => Ok(Some(DataSource::Path(f))), Some((f, _)) => Ok(Some(DataSource::Path(f))),
None => Ok(None), None => Ok(None),
}; };
} }