You've already forked AstralRinth
forked from didirus/AstralRinth
Rustic cleanups, dedups and making the code less hard to read in general (#251)
* typos :help_me: * (part 1/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 2/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 3/?) cut down some pretty major heap allocations here - more Bytes and BytesMuts, less Vec<u8>s also I don't really understand why you need to `to_vec` when you don't really use it again afterwards * (part 4/?) deduplicate error handling in backblaze logic * (part 5/?) fixes, cleanups, refactors, and reformatting * (part 6/?) cleanups and refactors * remove loads of `as_str` in types that already are `Display` * Revert "remove loads of `as_str` in types that already are `Display`" This reverts commit 4f974310cfb167ceba03001d81388db4f0fbb509. * reformat and move routes util to the util module * use streams * Run prepare + formatting issues Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@ use crate::models::projects::{
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::routes::project_creation::{CreateError, UploadedFile};
|
||||
use crate::util::auth::get_user_from_headers;
|
||||
use crate::util::routes::read_from_field;
|
||||
use crate::util::validate::validation_errors_to_string;
|
||||
use crate::validate::{validate_file, ValidationResult};
|
||||
use actix_multipart::{Field, Multipart};
|
||||
@@ -587,20 +588,10 @@ pub async fn upload_file(
|
||||
let content_type = crate::util::ext::project_file_type(file_extension)
|
||||
.ok_or_else(|| CreateError::InvalidFileType(file_extension.to_string()))?;
|
||||
|
||||
let mut data = Vec::new();
|
||||
while let Some(chunk) = field.next().await {
|
||||
// Project file size limit of 100MiB
|
||||
const FILE_SIZE_CAP: usize = 100 * (1 << 20);
|
||||
|
||||
if data.len() >= FILE_SIZE_CAP {
|
||||
return Err(CreateError::InvalidInput(
|
||||
String::from("Project file exceeds the maximum of 100MiB. Contact a moderator or admin to request permission to upload larger files.")
|
||||
));
|
||||
} else {
|
||||
let bytes = chunk.map_err(CreateError::MultipartError)?;
|
||||
data.append(&mut bytes.to_vec());
|
||||
}
|
||||
}
|
||||
let data = read_from_field(
|
||||
field, 100 * (1 << 20),
|
||||
"Project file exceeds the maximum of 100MiB. Contact a moderator or admin to request permission to upload larger files."
|
||||
).await?;
|
||||
|
||||
let hash = sha1::Sha1::from(&data).hexdigest();
|
||||
let exists = sqlx::query!(
|
||||
@@ -623,7 +614,7 @@ pub async fn upload_file(
|
||||
}
|
||||
|
||||
let validation_result = validate_file(
|
||||
data.as_slice(),
|
||||
&data,
|
||||
file_extension,
|
||||
project_type,
|
||||
loaders,
|
||||
@@ -638,7 +629,7 @@ pub async fn upload_file(
|
||||
"data/{}/versions/{}/{}",
|
||||
project_id, version_number, file_name
|
||||
),
|
||||
data.to_vec(),
|
||||
data.freeze(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user