fix: loading of data from the database seems to do something

This commit is contained in:
2026-04-04 21:46:56 +02:00
parent a7cc12ffb8
commit 989efeaf25
12 changed files with 303 additions and 55 deletions

View File

@@ -1,4 +1,6 @@
use sha2::{Digest, Sha256};
use std::io::Read;
use std::path::Path;
/// Compute a hex-encoded SHA-256 hash of the given content.
pub fn content_hash(content: &[u8]) -> String {
@@ -8,6 +10,21 @@ pub fn content_hash(content: &[u8]) -> String {
hex::encode(result)
}
/// Compute a hex-encoded SHA-256 hash of a file by streaming (8 KB chunks).
pub fn file_hash(path: &Path) -> std::io::Result<String> {
let mut file = std::fs::File::open(path)?;
let mut hasher = Sha256::new();
let mut buf = [0u8; 8192];
loop {
let n = file.read(&mut buf)?;
if n == 0 {
break;
}
hasher.update(&buf[..n]);
}
Ok(hex::encode(hasher.finalize()))
}
// sha2 doesn't include hex encoding, so we use a tiny inline helper.
mod hex {
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";

View File

@@ -8,7 +8,7 @@ pub mod sidecar;
pub mod thumbnail;
pub use slug::{slugify, ensure_unique};
pub use checksum::content_hash;
pub use checksum::{content_hash, file_hash};
pub use timestamp::{unix_ms_to_iso, iso_to_unix_ms, year_month_from_unix_ms, year_month_day_from_unix_ms, now_unix_ms};
pub use atomic_write::{atomic_write, atomic_write_str};
pub use paths::*;

View File

@@ -236,15 +236,16 @@ fn apply_orientation(img: DynamicImage, orientation: u16) -> DynamicImage {
}
}
/// Extract image dimensions from a file.
/// Extract image dimensions from a file (header-only, no full decode).
pub fn image_dimensions(path: &Path) -> Result<(u32, u32), String> {
let img = ImageReader::open(path)
let reader = ImageReader::open(path)
.map_err(|e| format!("open: {e}"))?
.with_guessed_format()
.map_err(|e| format!("format: {e}"))?
.decode()
.map_err(|e| format!("decode: {e}"))?;
Ok(img.dimensions())
.map_err(|e| format!("format: {e}"))?;
let (w, h) = reader
.into_dimensions()
.map_err(|e| format!("dimensions: {e}"))?;
Ok((w, h))
}
/// Detect MIME type from file extension.