Skip to content

Commit

Permalink
make fetching from s3 to be backward compatibility, adjusted error me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
wojciechBiezynski committed Oct 9, 2024
1 parent 98fbd9a commit cdb3b59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/image_provider/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub mod client {
"the downloaded image '{}' exceeds the maximum allowed size of {} bytes",
resource, max_size
);
return Err(FileSizeExceeded);
return Err(FileSizeExceeded(max_size));
}
bytes.extend_from_slice(&chunk);
}
Expand Down
19 changes: 14 additions & 5 deletions src/image_provider/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub mod s3 {
"the downloaded image '{}' exceeds the maximum allowed size of {} bytes",
resource, max_size
);
return Err(FileSizeExceeded);
return Err(FileSizeExceeded(max_size));
}
binary_payload.write_all(&bytes).map_err(|e| {
error!(
Expand All @@ -177,15 +177,24 @@ pub mod s3 {
response_headers: headers,
})
} else {
let bytes = result.body.collect().await.map_err(|e| {
let mut binary_payload: Vec<u8> = Vec::new();
while let Some(bytes) = result.body.try_next().await.map_err(|e| {
error!(
"failed to read the binary payload of the image '{}'. error: {}",
"failed to read the response for the file '{}'. error: '{}'",
resource, e
);
ImageDownloadFailed
})?;
})? {
binary_payload.write_all(&bytes).map_err(|e| {
error!(
"failed to read the response for the file '{}'. error: '{}'",
resource, e
);
ImageDownloadFailed
})?;
}
Ok(ImageResponse {
bytes: bytes.into_bytes().to_vec(),
bytes: binary_payload,
response_headers: headers,
})
}
Expand Down
20 changes: 12 additions & 8 deletions src/routes/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub enum ImageProcessingError {
ProcessingWorkerJoinError,
#[error("the image processing with libvips has failed")]
LibvipsProcessingFailed(libvips::error::Error),
#[error("The image exceeds the allowed size")]
FileSizeExceeded,
#[error("the image exceeds the allowed size")]
FileSizeExceeded(u32),
}

impl IntoResponse for ImageProcessingError {
Expand All @@ -99,9 +99,9 @@ impl IntoResponse for ImageProcessingError {
StatusCode::BAD_REQUEST,
format!("The provided resource URI is not valid: '{}'", resource_uri)
),
ImageProcessingError::FileSizeExceeded => (
ImageProcessingError::FileSizeExceeded(max_allowed_size) => (
StatusCode::BAD_REQUEST,
String::from("The image exceeds the allowed size. Please ensure the file size is within the permissible limit."),
format!("The image exceeds the allowed size of {max_allowed_size} bytes. Please ensure the file size is within the permissible limit or adjust the configuration."),
),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -118,17 +118,21 @@ impl IntoResponse for ImageProcessingError {
}

pub async fn process_image(
State(app_state): State<AppState>,
State(AppState {
vips_app,
image_provider,
config,
}): State<AppState>,
ProcessImageRequestExtractor(params): ProcessImageRequestExtractor<ProcessImageRequest>,
) -> Result<Response<Body>, ImageProcessingError> {
let now = SystemTime::now();
let main_img = app_state.image_provider.get_file(&params.image_address, &app_state.config).await?;
let main_img = image_provider.get_file(&params.image_address, &config).await?;
let mut total_input_size = main_img.bytes.len();

let watermarks_futures = params
.watermarks
.iter()
.map(|wm| app_state.image_provider.get_file(&wm.image_address, &app_state.config));
.map(|wm| image_provider.get_file(&wm.image_address, &config));
let watermarks = join_all(watermarks_futures)
.await
.into_iter()
Expand Down Expand Up @@ -174,7 +178,7 @@ pub async fn process_image(
.map_err(|e| {
error!(
"the image processing has failed for the resource with the error: {}. libvips raw error is: {}",
e, app_state.vips_app.error_buffer().unwrap_or("").replace("\n", ". ")
e, vips_app.error_buffer().unwrap_or("").replace("\n", ". ")
);
ImageProcessingError::LibvipsProcessingFailed(e)
})?;
Expand Down

0 comments on commit cdb3b59

Please sign in to comment.