| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use anyhow::Result; |
| 6 |
use std::path::Path; |
| 7 |
|
| 8 |
|
| 9 |
pub fn put(data: &Path, bytes: &[u8]) -> Result<String> { |
| 10 |
let hash = blake3::hash(bytes).to_hex().to_string(); |
| 11 |
let dir = data.join("attachments").join(&hash[..2]); |
| 12 |
let path = dir.join(&hash); |
| 13 |
if !path.exists() { |
| 14 |
std::fs::create_dir_all(&dir)?; |
| 15 |
crate::durable::write_atomic(&path, bytes)?; |
| 16 |
} |
| 17 |
Ok(hash) |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
pub fn get(data: &Path, hash: &str) -> Option<Vec<u8>> { |
| 22 |
if hash.len() < 3 || !hash.bytes().all(|b| b.is_ascii_hexdigit()) { |
| 23 |
return None; |
| 24 |
} |
| 25 |
std::fs::read(data.join("attachments").join(&hash[..2]).join(hash)).ok() |
| 26 |
} |