From 249c2da5ee39e8f4982184fa322e3d7234c2120c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 13 Sep 2022 15:00:45 +0200 Subject: Upload a file with a default name --- src/files.rs | 13 +++++++++---- src/routes/model.rs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/files.rs b/src/files.rs index 73e0497..c4b2669 100644 --- a/src/files.rs +++ b/src/files.rs @@ -8,10 +8,12 @@ use std::fs::read; use rand::random; -/// Upload a file. Returns an `AppError` or the path of the uploaded file +/// Upload a file. Returns an `AppError` or the path of the uploaded file. +/// If `filename` param has a value choose it as filename pub async fn upload( mut multipart: Multipart, allowed_extensions: Vec<&str>, + filename: Option, ) -> Result { let mut uploaded_file = String::new(); @@ -28,10 +30,13 @@ pub async fn upload( .iter() .any(|&x| x.to_lowercase() == ext_name) { - let rnd = (random::() * 1000000000 as f32) as i32; + let name = match filename { + Some(name) => name, + None => (random::() * 1000000000 as f32).to_string(), + }; - let save_filename = format!("{}/{}.{}", CONFIG.save_file_base_path, rnd, ext_name); - uploaded_file = format!("{}/{}.{}", CONFIG.uploads_endpoint, rnd, ext_name); + let save_filename = format!("{}/{}.{}", CONFIG.save_file_base_path, name, ext_name); + uploaded_file = format!("{}/{}.{}", CONFIG.uploads_endpoint, name, ext_name); let data = file.bytes().await.unwrap(); diff --git a/src/routes/model.rs b/src/routes/model.rs index 34e896d..bc66d25 100644 --- a/src/routes/model.rs +++ b/src/routes/model.rs @@ -83,7 +83,7 @@ async fn upload_model_file( let allowed_extensions = vec!["stl", "obj", "png", "jpg", "jpeg", "gif", "webp", "blend"]; - match upload(multipart, allowed_extensions).await { + match upload(multipart, allowed_extensions, None).await { Ok(saved_file) => { let model_file = ModelUpload::create(ModelUpload::new(saved_file, model_id)).await?; -- cgit v1.2.3-71-g8e6c