diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-13 13:00:45 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-13 13:00:45 +0000 |
| commit | 249c2da5ee39e8f4982184fa322e3d7234c2120c (patch) | |
| tree | 89b91761122b7084caf3d49c45c5295ad2120191 /src | |
| parent | 64d5f37d9e28363f27b49b9a5b421a19792d0165 (diff) | |
Upload a file with a default name
Diffstat (limited to 'src')
| -rw-r--r-- | src/files.rs | 13 | ||||
| -rw-r--r-- | 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<String>, ) -> Result<String, AppError> { 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::<f32>() * 1000000000 as f32) as i32; + let name = match filename { + Some(name) => name, + None => (random::<f32>() * 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?; |
