diff --git a/components/templates/src/global_fns/files.rs b/components/templates/src/global_fns/files.rs index 34ece031..a379b044 100644 --- a/components/templates/src/global_fns/files.rs +++ b/components/templates/src/global_fns/files.rs @@ -115,7 +115,7 @@ impl TeraFn for GetUrl { if cachebust { 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::(f, false).ok()) { Some(hash) => { @@ -167,7 +167,7 @@ impl TeraFn for GetFileHash { .unwrap_or(true); let file_path = match search_for_file(&self.base_path, &path) { - Some(f) => f, + Some((f, _)) => f, None => { return Err(format!("`get_file_hash`: Cannot find file: {}", path).into()); } diff --git a/components/templates/src/global_fns/helpers.rs b/components/templates/src/global_fns/helpers.rs index 2e8cb459..1a1f2a85 100644 --- a/components/templates/src/global_fns/helpers.rs +++ b/components/templates/src/global_fns/helpers.rs @@ -8,7 +8,8 @@ use std::path::{Path, PathBuf}; /// 3. base_path + content + path /// A path starting with @/ will replace it with `content/` and a path starting with `/` will have /// it removed. -pub fn search_for_file(base_path: &Path, path: &str) -> Option { +/// 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 actual_path = if path.starts_with("@/") { Cow::Owned(path.replace("@/", "content/")) @@ -31,7 +32,7 @@ pub fn search_for_file(base_path: &Path, path: &str) -> Option { } if file_exists { - Some(file_path) + Some((file_path, actual_path.into_owned())) } else { None } diff --git a/components/templates/src/global_fns/images.rs b/components/templates/src/global_fns/images.rs index dbe428bd..0cdd87e3 100644 --- a/components/templates/src/global_fns/images.rs +++ b/components/templates/src/global_fns/images.rs @@ -55,7 +55,7 @@ impl TeraFn for ResizeImage { } 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, None => { return Err(format!("`resize_image`: Cannot find file: {}", path).into()); @@ -63,7 +63,7 @@ impl TeraFn for ResizeImage { }; 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))?; 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)" ) .unwrap_or(false); - let src_path = match search_for_file(&self.base_path, &path) { - Some(f) => f, + let (src_path, _) = match search_for_file(&self.base_path, &path) { + Some((f, p)) => (f, p), None => { if allow_missing { 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 args.insert("path".to_string(), to_value("/content/gutenberg.jpg").unwrap()); - assert_eq!( - data["static_path"], - 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() - ); + let data2 = static_fn.call(&args).unwrap().as_object().unwrap().clone(); + assert_eq!(data, data2); // 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()); - assert_eq!( - data["static_path"], - 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() - ); + let data2 = static_fn.call(&args).unwrap().as_object().unwrap().clone(); + assert_eq!(data, data2); // 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()); diff --git a/components/templates/src/global_fns/load_data.rs b/components/templates/src/global_fns/load_data.rs index d24e9e86..5478fd7a 100644 --- a/components/templates/src/global_fns/load_data.rs +++ b/components/templates/src/global_fns/load_data.rs @@ -95,7 +95,7 @@ impl DataSource { if let Some(path) = path_arg { 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), }; }