fix(upload): check the content length while reading bytes

closes #1
This commit is contained in:
orhun 2021-07-27 23:49:35 +03:00
parent d02865b956
commit 60c25e2fbc
No known key found for this signature in database
GPG key ID: F83424824B3E4B90

View file

@ -51,12 +51,12 @@ async fn upload(
let mut bytes = Vec::<u8>::new(); let mut bytes = Vec::<u8>::new();
while let Some(chunk) = field.next().await { while let Some(chunk) = field.next().await {
bytes.append(&mut chunk?.to_vec()); bytes.append(&mut chunk?.to_vec());
}
let bytes_unit = Byte::from_bytes(bytes.len() as u128).get_appropriate_unit(false);
if bytes.len() as u128 > config.server.max_content_length.get_bytes() { if bytes.len() as u128 > config.server.max_content_length.get_bytes() {
log::warn!("upload rejected for {} ({})", host, bytes_unit); log::warn!("upload rejected for {}", host);
return Err(error::ErrorPayloadTooLarge("upload limit exceeded")); return Err(error::ErrorPayloadTooLarge("upload limit exceeded"));
} }
}
let bytes_unit = Byte::from_bytes(bytes.len() as u128).get_appropriate_unit(false);
let file_name = &file::save(content.get_file_name()?, &bytes, &config)?; let file_name = &file::save(content.get_file_name()?, &bytes, &config)?;
log::info!("{} ({}) is uploaded from {}", file_name, bytes_unit, host); log::info!("{} ({}) is uploaded from {}", file_name, bytes_unit, host);
urls.push(format!( urls.push(format!(