From 2a90d0ff8d4336850a450f749140e2df2e94fc56 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 10 Sep 2022 17:23:02 +0200 Subject: Upload files --- src/files.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/files.rs (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..63b8eec --- /dev/null +++ b/src/files.rs @@ -0,0 +1,44 @@ +use crate::config::SAVE_FILE_BASE_PATH; +use crate::errors::AppError; +use axum::extract::Multipart; + +use rand::random; + +/// Upload a file. Returns an `AppError` or the path of the uploaded file +pub async fn upload( + mut multipart: Multipart, + allowed_extensions: Vec<&str>, +) -> Result { + let mut save_filename = String::new(); + + if let Some(file) = multipart.next_field().await.unwrap() { + let content_type = file.content_type().unwrap().to_string(); + + let index = content_type + .find("/") + .map(|i| i) + .unwrap_or(usize::max_value()); + let mut ext_name = "xxx"; + if index != usize::max_value() { + ext_name = &content_type[index + 1..]; + } + + if allowed_extensions + .iter() + .position(|&x| x.to_lowercase() == ext_name) + .is_some() + { + let rnd = (random::() * 1000000000 as f32) as i32; + + save_filename = format!("{}/{}.{}", SAVE_FILE_BASE_PATH, rnd, ext_name); + + let data = file.bytes().await.unwrap(); + + tokio::fs::write(&save_filename, &data) + .await + .map_err(|err| err.to_string())?; + } + } + + Ok(save_filename) +} -- cgit v1.2.3-71-g8e6c