feat: file dedup — content_hash SHA256 + /files/lookup API + auto-rename on name collision
This commit is contained in:
18
src/core/storage/content_hash.rs
Normal file
18
src/core/storage/content_hash.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Compute SHA256 of the entire file content
|
||||
pub fn compute_sha256(path: &Path) -> Result<String> {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buf = [0u8; 65536];
|
||||
loop {
|
||||
let n = file.read(&mut buf)?;
|
||||
if n == 0 { break; }
|
||||
hasher.update(&buf[..n]);
|
||||
}
|
||||
let hash = format!("{:x}", hasher.finalize());
|
||||
Ok(hash)
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
pub mod content_hash;
|
||||
pub mod file_manager;
|
||||
pub mod output_dir;
|
||||
pub mod uuid;
|
||||
|
||||
pub use content_hash::compute_sha256;
|
||||
pub use file_manager::FileManager;
|
||||
pub use output_dir::OutputDir;
|
||||
pub use uuid::compute_uuid;
|
||||
|
||||
Reference in New Issue
Block a user