From 26d40b9993820837aa9d2d683937e502bfd3be12 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 9 Apr 2021 14:05:27 +0530 Subject: [PATCH] path resolution w.r.t root --- actix-example/src/main.rs | 15 +++++---- src/hash.rs | 4 +-- src/map.rs | 67 +++++++++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/actix-example/src/main.rs b/actix-example/src/main.rs index 99bc547..25d9d04 100644 --- a/actix-example/src/main.rs +++ b/actix-example/src/main.rs @@ -3,20 +3,23 @@ use cache_buster::Files; fn main() { let files = Files::load(); - assert!(file_exists("../dist/log-out.svg", &files)); - assert!(file_exists( + assert!(get_full_path_runner("../dist/log-out.svg", &files)); + assert!(get_full_path_runner( "../dist/a/b/c/d/s/d/svg/credit-card.svg", &files )); - assert!(!file_exists("dist/log-out.svg", &files)); - assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files)); + assert!(!get_full_path_runner("dist/log-out.svg", &files)); + assert!(!get_full_path_runner( + "dist/a/b/c/d/s/d/svg/credit-card.svg", + &files + )); } -fn file_exists(path: &str, files: &Files) -> bool { +fn get_full_path_runner(path: &str, files: &Files) -> bool { use std::path::Path; - if let Some(file) = files.get(path) { + if let Some(file) = files.get_full_path(path) { Path::new(file).exists() } else { false diff --git a/src/hash.rs b/src/hash.rs index 525a5ac..3a8b9d7 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -54,7 +54,7 @@ impl Buster { // // doesn't process files for which mime is not resolved pub fn try_hash(&self) -> Result { - let mut file_map: Files = Files::default(); + let mut file_map: Files = Files::new(&self.result); for entry in WalkDir::new(&self.source) .follow_links(self.follow_links) .into_iter() @@ -92,7 +92,7 @@ impl Buster { // panics when mimetypes are detected. This way you'll know which files are ignored // from processing pub fn hash(&self) -> Result { - let mut file_map: Files = Files::default(); + let mut file_map: Files = Files::new(&self.result); for entry in WalkDir::new(&self.source) .follow_links(self.follow_links) diff --git a/src/map.rs b/src/map.rs index 7daff5f..ff6ffeb 100644 --- a/src/map.rs +++ b/src/map.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; const ENV_VAR_NAME: &str = "CACHE_BUSTER_FILE_MAP"; -#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] pub struct Files { pub map: HashMap, base_dir: String, @@ -26,7 +26,15 @@ impl Files { } } - pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> { + pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a str> { + if let Some(path) = self.map.get(path) { + Some(&path[self.base_dir.len()..]) + } else { + None + } + } + + pub fn get_full_path<'a>(&'a self, path: &'a str) -> Option<&'a String> { self.map.get(path) } @@ -70,7 +78,7 @@ mod tests { use std::path::Path; #[test] - fn get_works() { + fn get_full_path_works() { let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, @@ -90,19 +98,22 @@ mod tests { config.init().unwrap(); let files = config.hash().unwrap(); - assert!(file_exists("./dist/log-out.svg", &files)); - assert!(file_exists( + assert!(get_full_path_runner("./dist/log-out.svg", &files)); + assert!(get_full_path_runner( "./dist/a/b/c/d/s/d/svg/credit-card.svg", &files )); - assert!(!file_exists("dist/log-out.svg", &files)); - assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files)); + assert!(!get_full_path_runner("dist/log-out.svg", &files)); + assert!(!get_full_path_runner( + "dist/a/b/c/d/s/d/svg/credit-card.svg", + &files + )); cleanup(&config); } - fn file_exists(path: &str, files: &Files) -> bool { - if let Some(file) = files.get(path) { + fn get_full_path_runner(path: &str, files: &Files) -> bool { + if let Some(file) = files.get_full_path(path) { Path::new(file).exists() } else { false @@ -138,4 +149,42 @@ mod tests { cleanup(&config); } + + #[test] + fn get_works() { + let types = vec![ + mime::IMAGE_PNG, + mime::IMAGE_SVG, + mime::IMAGE_JPEG, + mime::IMAGE_GIF, + ]; + + let config = BusterBuilder::default() + .source("./dist") + .result("/tmp/prod5") + .mime_types(types) + .copy(true) + .follow_links(true) + .build() + .unwrap(); + + config.init().unwrap(); + let files = config.hash().unwrap(); + + assert!(get_runner("./dist/log-out.svg", &files)); + assert!(get_runner("./dist/a/b/c/d/s/d/svg/credit-card.svg", &files)); + + assert!(!get_runner("dist/log-out.svg", &files)); + assert!(!get_runner("dist/a/b/c/d/s/d/svg/credit-card.svg", &files)); + cleanup(&config); + } + + fn get_runner(path: &str, files: &Files) -> bool { + if let Some(file) = files.get(path) { + let path = Path::new(&files.base_dir).join(&file[1..]); + path.exists() + } else { + false + } + } }