diff --git a/src/commands/build/snapshots.rs b/src/commands/build/snapshots.rs index 196ca51a27..6b66563899 100644 --- a/src/commands/build/snapshots.rs +++ b/src/commands/build/snapshots.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fs::{self, File}; +use std::fs::File; use std::io::BufReader; use std::path::{Path, PathBuf}; use std::str::FromStr as _; @@ -212,11 +212,24 @@ fn validate_image_sizes(images: &[ImageInfo]) -> Result<()> { Ok(()) } -fn compute_sha256_hash(data: &[u8]) -> String { +fn compute_sha256_hash(path: &Path) -> Result { + use std::io::Read as _; + + let mut file = std::fs::File::open(path) + .with_context(|| format!("Failed to open image for hashing: {}", path.display()))?; let mut hasher = Sha256::new(); - hasher.update(data); + let mut buffer = [0u8; 8192]; + loop { + let bytes_read = file + .read(&mut buffer) + .with_context(|| format!("Failed to read image for hashing: {}", path.display()))?; + if bytes_read == 0 { + break; + } + hasher.update(&buffer[..bytes_read]); + } let result = hasher.finalize(); - format!("{result:x}") + Ok(format!("{result:x}")) } fn is_hidden(root: &Path, path: &Path) -> bool { @@ -297,15 +310,18 @@ fn upload_images( for image in images { debug!("Processing image: {}", image.path.display()); - let contents = fs::read(&image.path) - .with_context(|| format!("Failed to read image: {}", image.path.display()))?; - let hash = compute_sha256_hash(&contents); + let hash = compute_sha256_hash(&image.path)?; + let file = runtime + .block_on(tokio::fs::File::open(&image.path)) + .with_context(|| { + format!("Failed to open image for upload: {}", image.path.display()) + })?; info!("Queueing {} as {hash}", image.relative_path.display()); many_builder = many_builder.push( session - .put(contents) + .put_file(file) .key(&hash) .expiration_policy(expiration), );