Skip to content

Commit 7075fc8

Browse files
committed
refactor: Improve upload chunk reading error handling and remove unused imports.
1 parent bebd16a commit 7075fc8

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

backend/src/handlers/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use axum::{
3030
};
3131
use chrono::{DateTime, Duration as ChronoDuration, Utc};
3232
use sha2::{Digest, Sha256};
33-
use sqlx::{self, FromRow};
33+
use sqlx;
3434
use std::{env, sync::OnceLock, time::Duration};
3535

3636
/// Global salt for hashing login attempt identifiers.

backend/src/handlers/upload.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
models::{ErrorResponse, UploadResponse},
44
};
55
use axum::{
6-
extract::{Multipart, State},
6+
extract::Multipart,
77
http::StatusCode,
88
Json,
99
};
@@ -159,16 +159,26 @@ pub async fn upload_image(
159159

160160
let mut total_size = first_chunk.len();
161161

162-
while let Some(chunk) = field.chunk().await.map_err(|err| {
163-
tracing::error!("Failed to read chunk: {}", err);
164-
let _ = tokio::fs::remove_file(&filepath).await;
165-
(
166-
StatusCode::INTERNAL_SERVER_ERROR,
167-
Json(ErrorResponse {
168-
error: format!("Failed to read file: {}", err),
169-
}),
170-
)
171-
})? {
162+
loop {
163+
let chunk_option = match field.chunk().await {
164+
Ok(opt) => opt,
165+
Err(err) => {
166+
tracing::error!("Failed to read chunk: {}", err);
167+
let _ = tokio::fs::remove_file(&filepath).await;
168+
return Err((
169+
StatusCode::INTERNAL_SERVER_ERROR,
170+
Json(ErrorResponse {
171+
error: format!("Failed to read file: {}", err),
172+
}),
173+
));
174+
}
175+
};
176+
177+
let chunk = match chunk_option {
178+
Some(c) => c,
179+
None => break,
180+
};
181+
172182
total_size += chunk.len();
173183
if total_size > MAX_FILE_SIZE {
174184
let _ = tokio::fs::remove_file(&filepath).await;

backend/src/security/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use axum::{
2626
request::Parts,
2727
HeaderMap, HeaderValue, StatusCode,
2828
},
29-
Json,
29+
3030
};
3131
use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
3232
use chrono::{Duration, Utc};
@@ -38,7 +38,7 @@ use std::sync::LazyLock;
3838
use std::sync::OnceLock;
3939
use time::{Duration as TimeDuration, OffsetDateTime};
4040

41-
use crate::db::{self, DbPool};
41+
use crate::db::DbPool;
4242

4343
/// Global storage for the JWT secret key.
4444
/// Initialized once at application startup via init_jwt_secret().

0 commit comments

Comments
 (0)