[API] Refactor: set upload endpoint to form-data and increase limit to 5gb per upload

This commit is contained in:
Mercurio 2025-06-03 19:54:41 +02:00
parent 98c5986dd5
commit f191fc646e
3 changed files with 4 additions and 7 deletions

View file

@ -19,7 +19,7 @@ pub struct Upload<'r> {
file: TempFile<'r>, file: TempFile<'r>,
} }
#[post("/upload", data = "<upload>")] #[post("/upload", data = "<upload>", format = "multipart/form-data")]
pub async fn upload_file( pub async fn upload_file(
pool: &State<PgPool>, pool: &State<PgPool>,
user: AuthenticatedUser, user: AuthenticatedUser,
@ -41,7 +41,7 @@ pub async fn upload_file(
let encrypted = encrypt_data(&buffer, key.as_bytes(), &nonce); let encrypted = encrypt_data(&buffer, key.as_bytes(), &nonce);
let file_id = Uuid::new_v4().to_string(); let file_id = Uuid::new_v4().to_string();
let storage_path = format!("/data/{}", file_id); let storage_path = format!("./data/{}", file_id);
fs::write(&storage_path, &encrypted).await.ok(); fs::write(&storage_path, &encrypted).await.ok();
fs::write(format!("{}.nonce", &storage_path), &nonce).await.ok(); fs::write(format!("{}.nonce", &storage_path), &nonce).await.ok();

View file

@ -52,6 +52,7 @@ async fn main() -> Result<(), rocket::Error> {
.merge(("limits", rocket::data::Limits::new() .merge(("limits", rocket::data::Limits::new()
.limit("form", ByteUnit::Gigabyte(5)) // 5 GB .limit("form", ByteUnit::Gigabyte(5)) // 5 GB
.limit("file", ByteUnit::Gigabyte(1)) // 1 GB .limit("file", ByteUnit::Gigabyte(1)) // 1 GB
.limit("data-form", ByteUnit::Gigabyte(5)) // 5 GB for multipart/form-data
)); ));

View file

@ -141,11 +141,9 @@ class _MainPageState extends State<MainPage> {
filename: file.name, filename: file.name,
), ),
); );
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Caricamento in corso...')), SnackBar(content: Text('Caricamento di ${file.name} in corso...')),
); );
final response = await request.send(); final response = await request.send();
if (response.statusCode == 200 || response.statusCode == 201) { if (response.statusCode == 200 || response.statusCode == 201) {
@ -185,9 +183,7 @@ class _MainPageState extends State<MainPage> {
); );
if (response.statusCode == 200) { if (response.statusCode == 200) {
// Save the file to disk
if (Platform.isAndroid || Platform.isIOS) { if (Platform.isAndroid || Platform.isIOS) {
// Mobile platforms
await FileSaver.instance.saveFile( await FileSaver.instance.saveFile(
name: file.name, name: file.name,
bytes: response.bodyBytes, bytes: response.bodyBytes,