check key field first

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2025-11-16 18:10:40 +05:30
parent 63a0268695
commit 1516480216

View File

@@ -227,12 +227,10 @@ fn clean_filename(filename: &str) -> String {
async fn upload(mut payload: Multipart) -> Result<impl IntoResponse, YamafError> {
let mut responses = Vec::new();
let mut found_key = false;
while let Some(mut field) = payload.next_field().await.unwrap() {
match field.name() {
Some("key") => {
if let Ok(ref key) = CONFIG.key {
if let Some(field) = payload.next_field().await.unwrap() {
if field.name() == Some("key") {
let bytes = field
.bytes()
.await
@@ -244,16 +242,16 @@ async fn upload(mut payload: Multipart) -> Result<impl IntoResponse, YamafError>
if s != *key {
return Err(YamafError::BadRequest("Wrong key".into()));
}
found_key = true;
}
}
Some("file") => {
if CONFIG.key.is_ok() && found_key == false {
} else {
return Err(YamafError::BadRequest("Missing key".into()));
}
} else {
return Err(YamafError::BadRequest("Missing key".into()));
}
}
while let Some(mut field) = payload.next_field().await.unwrap() {
if field.name() == Some("file") {
let filename = field
.file_name()
.map_or(format!("{}-upload", random(10)), |filename| {
@@ -298,9 +296,6 @@ async fn upload(mut payload: Multipart) -> Result<impl IntoResponse, YamafError>
size = written as f64 / 1024 as f64
));
}
None | Some(_) => {}
}
}
if responses.is_empty() {