From 1d408c2905f96f849705aabb2202f0d40d1a4344 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sat, 5 Mar 2016 01:07:04 -0600 Subject: [PATCH] Working on allowing the cache to be turned off --- Cargo.toml | 2 +- src/cache.rs | 131 ++++++++++++++++++++++++++------------------------- src/lib.rs | 2 +- 3 files changed, 70 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb2b3ef..3a72d82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pihash" -version = "0.2.5" +version = "0.2.6" authors = ["Drew Short "] description = "A simple library for generating perceptual hashes for images and comparing images based on their perceptual hashes." repository = "https://github.com/warricksothr/Perceptual-Image-Hashing/" diff --git a/src/cache.rs b/src/cache.rs index b3de76a..45201bd 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -48,10 +48,11 @@ impl PartialEq for CacheMetadata { */ pub struct Cache<'a> { pub cache_dir: &'a str, + pub use_cache: bool, } impl<'a> Default for Cache<'a> { - fn default() -> Cache<'a> { Cache {cache_dir: CACHE_DIR } } + fn default() -> Cache<'a> { Cache {cache_dir: CACHE_DIR, use_cache: true } } } impl<'a> Cache<'a> { @@ -172,33 +173,35 @@ impl<'a> Cache<'a> { path: &Path, size: u32) -> Option, Vec>> { - let hash = self.get_file_hash(&path); - match hash { - Ok(sha1) => { - // Check if the file exists in the cache - let cache_path_str = format!("{}/image/{}x{}/{}.{}", - self.cache_dir, - size, - size, - sha1, - CACHED_IMAGE_EXT); - let cached_path = Path::new(&cache_path_str); - // Try to open, if it does, then we can read the image in - match File::open(&cached_path) { - Ok(_) => { - let image = image::open(&cached_path).unwrap(); - Some(image.to_luma()) + if self.use_cache { + let hash = self.get_file_hash(&path); + match hash { + Ok(sha1) => { + // Check if the file exists in the cache + let cache_path_str = format!("{}/image/{}x{}/{}.{}", + self.cache_dir, + size, + size, + sha1, + CACHED_IMAGE_EXT); + let cached_path = Path::new(&cache_path_str); + // Try to open, if it does, then we can read the image in + match File::open(&cached_path) { + Ok(_) => { + let image = image::open(&cached_path).unwrap(); + Some(image.to_luma()) + } + // Don't really care here, it just means an existing cached + // file doesn't exist, or can't be read. + Err(_) => None, } - // Don't really care here, it just means an existing cached - // file doesn't exist, or can't be read. - Err(_) => None, + } + Err(e) => { + println!("Error: {}", e); + None } } - Err(e) => { - println!("Error: {}", e); - None - } - } + } else { None } } /** @@ -263,48 +266,50 @@ impl<'a> Cache<'a> { path: &Path, size: u32) -> Option>> { - let hash = self.get_file_hash(&path); - match hash { - Ok(sha1) => { - // Check if the file exists in the cache - let cache_path_str = format!("{}/matrix/{}x{}/{}.{}", CACHE_DIR, size, size, sha1, CACHED_MATRIX_EXT); - let cached_path = Path::new(&cache_path_str); - // Try to open, if it does, then we can read the image in - match File::open(&cached_path) { - Ok(file) => { - let mut decoder = ZlibDecoder::new(&file); - let mut matrix_data_str = String::new(); - match decoder.read_to_string(&mut matrix_data_str) { - Ok(_) => {} - Err(e) => { - println!("Unable to decompress matrix: {}", e); - return None; - } - }; - // convert the matrix - let matrix: Vec> = matrix_data_str.trim() - .split("\n") - .map(|line| { - line.split(",") - .map(|f| { - f64::from_str(f).unwrap() - }) - .collect() - }) - .collect(); + if self.use_cache { + let hash = self.get_file_hash(&path); + match hash { + Ok(sha1) => { + // Check if the file exists in the cache + let cache_path_str = format!("{}/matrix/{}x{}/{}.{}", CACHE_DIR, size, size, sha1, CACHED_MATRIX_EXT); + let cached_path = Path::new(&cache_path_str); + // Try to open, if it does, then we can read the image in + match File::open(&cached_path) { + Ok(file) => { + let mut decoder = ZlibDecoder::new(&file); + let mut matrix_data_str = String::new(); + match decoder.read_to_string(&mut matrix_data_str) { + Ok(_) => {} + Err(e) => { + println!("Unable to decompress matrix: {}", e); + return None; + } + }; + // convert the matrix + let matrix: Vec> = matrix_data_str.trim() + .split("\n") + .map(|line| { + line.split(",") + .map(|f| { + f64::from_str(f).unwrap() + }) + .collect() + }) + .collect(); - Some(matrix) + Some(matrix) + } + // Don't really care here, it just means an existing cached + // file doesn't exist, or can't be read. + Err(_) => None, } - // Don't really care here, it just means an existing cached - // file doesn't exist, or can't be read. - Err(_) => None, + } + Err(e) => { + println!("Error: {}", e); + None } } - Err(e) => { - println!("Error: {}", e); - None - } - } + } else { None } } } diff --git a/src/lib.rs b/src/lib.rs index 032ed30..b5a7b03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ use hash::PerceptualHash; use std::ffi::CStr; use cache::Cache; -static LIB_CACHE: Cache<'static> = Cache { cache_dir: cache::CACHE_DIR }; +static LIB_CACHE: Cache<'static> = Cache { cache_dir: cache::CACHE_DIR, use_cache: true }; /** * Prepare the library for work.