Browse Source

Working on allowing the cache to be turned off

develop
Drew Short 8 years ago
parent
commit
1d408c2905
  1. 2
      Cargo.toml
  2. 131
      src/cache.rs
  3. 2
      src/lib.rs

2
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "pihash"
version = "0.2.5"
version = "0.2.6"
authors = ["Drew Short <warrick@sothr.com>"]
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/"

131
src/cache.rs

@ -48,10 +48,11 @@ impl PartialEq<CacheMetadata> 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<ImageBuffer<image::Luma<u8>, Vec<u8>>> {
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<Vec<Vec<f64>>> {
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<Vec<f64>> = 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<Vec<f64>> = 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 }
}
}

2
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.

Loading…
Cancel
Save