Browse Source

Code cleanup and cache path changes

master
Drew Short 7 years ago
parent
commit
36b30e1388
  1. 22
      src/cache.rs
  2. 21
      src/main.rs

22
src/cache.rs

@ -137,7 +137,7 @@ impl<'a> Cache<'a> {
pub fn get_file_hash(&self, path: &Path) -> Result<String, Error> {
let mut source = try!(File::open(&path));
let mut buf: Vec<u8> = Vec::new();
try!(source.read_to_end(&mut buf));
source.read_to_end(&mut buf)?;
let mut sha1 = Sha1::new();
sha1.update(&buf);
let digest = sha1.digest();
@ -156,23 +156,24 @@ impl<'a> Cache<'a> {
let hash = self.get_file_hash(&path);
match hash {
Ok(sha1) => {
let cache_path_str = format!("{}/image/{}x{}/{}.{}",
let cache_path_str = format!("{}/image/{}x{}/{}/{}.{}",
self.cache_dir,
size,
size,
&sha1[..10],
sha1,
CACHED_IMAGE_EXT);
let cache_dir_str = format!("{}/image/{}x{}", self.cache_dir, size, size);
let cache_dir_str = format!("{}/image/{}x{}/{}", self.cache_dir, size, size, &sha1[..10]);
// println!("Saving: {}", cache_path_str);
match create_dir_all(cache_dir_str) {
Ok(_) => {
match File::create(Path::new(&cache_path_str)) {
Ok(mut file) => {
// Save the file into the cache
match image.save(& mut file, image::ImageFormat::PNG) {
match image.save(&mut file, image::ImageFormat::PNG) {
Ok(_) => {}
Err(e) => {
println ! ("Error: {}", e);
println!("Error: {}", e);
return Err(Error::new(ErrorKind::Other, e));
}
}
@ -203,10 +204,11 @@ impl<'a> Cache<'a> {
match hash {
Ok(sha1) => {
// Check if the file exists in the cache
let cache_path_str = format!("{}/image/{}x{}/{}.{}",
let cache_path_str = format!("{}/image/{}x{}/{}/{}.{}",
self.cache_dir,
size,
size,
&sha1[..10],
sha1,
CACHED_IMAGE_EXT);
let cached_path = Path::new(&cache_path_str);
@ -242,13 +244,14 @@ impl<'a> Cache<'a> {
let hash = self.get_file_hash(&path);
match hash {
Ok(sha1) => {
let cache_path_str = format!("{}/matrix/{}x{}/{}.{}",
let cache_path_str = format!("{}/matrix/{}x{}/{}/{}.{}",
self.cache_dir,
size,
size,
&sha1[..10],
sha1,
CACHED_MATRIX_EXT);
let cache_dir_str = format!("{}/matrix/{}x{}", self.cache_dir, size, size);
let cache_dir_str = format!("{}/matrix/{}x{}/{}", self.cache_dir, size, size, &sha1[..10]);
match create_dir_all(cache_dir_str) {
Ok(_) => {
let cached_path = Path::new(&cache_path_str);
@ -303,10 +306,11 @@ impl<'a> Cache<'a> {
match hash {
Ok(sha1) => {
// Check if the file exists in the cache
let cache_path_str = format!("{}/matrix/{}x{}/{}.{}",
let cache_path_str = format!("{}/matrix/{}x{}/{}/{}.{}",
self.cache_dir,
size,
size,
&sha1[..10],
sha1,
CACHED_MATRIX_EXT);
let cached_path = Path::new(&cache_path_str);

21
src/main.rs

@ -87,16 +87,17 @@ fn main() {
} else {
let image_path = Path::new(&args.arg_path);
let hashes = get_requested_perceptual_hashes(&lib, &image_path, &args);
let hash_result = format!(r#"
file: {}
ahash: {}
dhash: {}
phash: {}
"#,
hashes.orig_path,
hashes.ahash,
hashes.dhash,
hashes.phash);
let hash_result = format!(
r#"
file: {}
ahash: {}
dhash: {}
phash: {}
"#,
hashes.orig_path,
hashes.ahash,
hashes.dhash,
hashes.phash);
println!("{}", hash_result);
}
}

Loading…
Cancel
Save