summaryrefslogtreecommitdiffstats
path: root/src/files.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-13 13:00:45 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-13 13:00:45 +0000
commit249c2da5ee39e8f4982184fa322e3d7234c2120c (patch)
tree89b91761122b7084caf3d49c45c5295ad2120191 /src/files.rs
parent64d5f37d9e28363f27b49b9a5b421a19792d0165 (diff)
Upload a file with a default name
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs13
1 files changed, 9 insertions, 4 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();