refactor hashing

This commit is contained in:
Maciej Jur 2024-07-19 01:10:59 +02:00
parent 142ea69ff7
commit 005d4c15e5
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
3 changed files with 22 additions and 81 deletions

63
Cargo.lock generated
View file

@ -2,15 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
@ -131,21 +122,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "biblatex"
version = "0.9.3"
@ -665,12 +641,6 @@ dependencies = [
"weezl",
]
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "glob"
version = "0.3.1"
@ -1137,15 +1107,6 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e25be21376a772d15f97ae789845340a9651d3c4246ff5ebb6a2b35f9c37bd31"
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.19.0"
@ -1206,12 +1167,6 @@ dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "png"
version = "0.17.13"
@ -1434,12 +1389,6 @@ dependencies = [
"thiserror",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustversion"
version = "1.0.14"
@ -1543,7 +1492,6 @@ dependencies = [
"bytes",
"hex",
"sha2",
"tokio",
]
[[package]]
@ -1737,17 +1685,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.38.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
dependencies = [
"backtrace",
"bytes",
"pin-project-lite",
]
[[package]]
name = "tree-sitter"
version = "0.22.6"

View file

@ -19,7 +19,7 @@ once_cell = "1.19.0"
rayon = "1.10.0"
regex = "1.10.5"
serde = { version = "1.0.203", features = ["derive"] }
sha256 = "1.5.0"
sha256 = { version = "1.5.0", default-features = false }
# Watch
notify = "6.1.1"

View file

@ -108,23 +108,27 @@ fn render_all(
.collect()
}
fn store_hash(buffer: &[u8]) -> Utf8PathBuf {
let store = Utf8Path::new(".hash");
let hash = sha256::digest(buffer);
fn add_hash(buffer: &[u8], file: &Utf8Path, path: &Utf8Path) {
println!("Hashing image {} -> {}", file, path);
let img = image::load_from_memory(buffer).expect("Couldn't load image");
let store_hash = store.join(&hash).with_extension("webp");
let dim = (img.width(), img.height());
if !store_hash.exists() {
let dim = (img.width(), img.height());
let mut out = Vec::new();
let encoder = image::codecs::webp::WebPEncoder::new_lossless(&mut out);
let mut out = Vec::new();
let encoder = image::codecs::webp::WebPEncoder::new_lossless(&mut out);
encoder
.encode(&img.to_rgba8(), dim.0, dim.1, image::ColorType::Rgba8)
.expect("Encoding error");
encoder
.encode(&img.to_rgba8(), dim.0, dim.1, image::ColorType::Rgba8)
.expect("Encoding error");
fs::create_dir_all(store).unwrap();
fs::write(store_hash, out).expect("Couldn't output optimized image");
fs::write(path, out).expect("Couldn't output optimized image");
}
fn ensure_hashed(cache: &Utf8Path, buffer: &[u8], file: &Utf8Path) -> Utf8PathBuf {
let hash = sha256::digest(buffer);
let path = cache.join(&hash).with_extension("webp");
if !path.exists() {
add_hash(buffer, file, &path)
}
Utf8Path::new("/")
@ -134,6 +138,9 @@ fn store_hash(buffer: &[u8]) -> Utf8PathBuf {
}
pub(crate) fn store_hash_all(items: &[&Output]) -> Vec<Hashed> {
let cache = Utf8Path::new(".hash");
fs::create_dir_all(cache).unwrap();
items
.par_iter()
.filter_map(|item| match item.kind {
@ -146,12 +153,9 @@ pub(crate) fn store_hash_all(items: &[&Output]) -> Vec<Hashed> {
return None;
}
let hash = store_hash(&buffer);
println!("Hashing image {} as {}", asset.meta.path, hash);
Some(Hashed {
file: item.path.to_owned(),
hash,
hash: ensure_hashed(cache, &buffer, &asset.meta.path),
})
}
_ => None,