|
1 | 1 | use std::collections::HashMap; |
2 | | -use std::fs::{self, File}; |
| 2 | +use std::fs::File; |
3 | 3 | use std::io::BufReader; |
4 | 4 | use std::path::{Path, PathBuf}; |
5 | 5 | use std::str::FromStr as _; |
@@ -212,11 +212,24 @@ fn validate_image_sizes(images: &[ImageInfo]) -> Result<()> { |
212 | 212 | Ok(()) |
213 | 213 | } |
214 | 214 |
|
215 | | -fn compute_sha256_hash(data: &[u8]) -> String { |
| 215 | +fn compute_sha256_hash(path: &Path) -> Result<String> { |
| 216 | + use std::io::Read as _; |
| 217 | + |
| 218 | + let mut file = std::fs::File::open(path) |
| 219 | + .with_context(|| format!("Failed to open image for hashing: {}", path.display()))?; |
216 | 220 | let mut hasher = Sha256::new(); |
217 | | - hasher.update(data); |
| 221 | + let mut buffer = [0u8; 8192]; |
| 222 | + loop { |
| 223 | + let bytes_read = file |
| 224 | + .read(&mut buffer) |
| 225 | + .with_context(|| format!("Failed to read image for hashing: {}", path.display()))?; |
| 226 | + if bytes_read == 0 { |
| 227 | + break; |
| 228 | + } |
| 229 | + hasher.update(&buffer[..bytes_read]); |
| 230 | + } |
218 | 231 | let result = hasher.finalize(); |
219 | | - format!("{result:x}") |
| 232 | + Ok(format!("{result:x}")) |
220 | 233 | } |
221 | 234 |
|
222 | 235 | fn is_hidden(root: &Path, path: &Path) -> bool { |
@@ -297,15 +310,18 @@ fn upload_images( |
297 | 310 | for image in images { |
298 | 311 | debug!("Processing image: {}", image.path.display()); |
299 | 312 |
|
300 | | - let contents = fs::read(&image.path) |
301 | | - .with_context(|| format!("Failed to read image: {}", image.path.display()))?; |
302 | | - let hash = compute_sha256_hash(&contents); |
| 313 | + let hash = compute_sha256_hash(&image.path)?; |
| 314 | + let file = runtime |
| 315 | + .block_on(tokio::fs::File::open(&image.path)) |
| 316 | + .with_context(|| { |
| 317 | + format!("Failed to open image for upload: {}", image.path.display()) |
| 318 | + })?; |
303 | 319 |
|
304 | 320 | info!("Queueing {} as {hash}", image.relative_path.display()); |
305 | 321 |
|
306 | 322 | many_builder = many_builder.push( |
307 | 323 | session |
308 | | - .put(contents) |
| 324 | + .put_file(file) |
309 | 325 | .key(&hash) |
310 | 326 | .expiration_policy(expiration), |
311 | 327 | ); |
|
0 commit comments