file uploads
This commit is contained in:
34
backend/src/api/warrens/upload_files.rs
Normal file
34
backend/src/api/warrens/upload_files.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use axum::extract::{Multipart, Path, State};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{Result, api::AppState, warrens::Warren};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(super) struct UploadPath {
|
||||
warren_id: Uuid,
|
||||
rest: Option<String>,
|
||||
}
|
||||
|
||||
pub(super) async fn route(
|
||||
State(state): State<AppState>,
|
||||
Path(UploadPath { warren_id, rest }): Path<UploadPath>,
|
||||
mut multipart: Multipart,
|
||||
) -> Result<()> {
|
||||
let warren = Warren::get(state.pool(), &warren_id).await?;
|
||||
|
||||
while let Ok(Some(field)) = multipart.next_field().await {
|
||||
if field.name().is_none_or(|name| name != "files") {
|
||||
continue;
|
||||
};
|
||||
|
||||
let file_name = field.file_name().map(str::to_owned).unwrap();
|
||||
let data = field.bytes().await.unwrap();
|
||||
|
||||
warren
|
||||
.upload(state.serve_dir(), rest.as_deref(), &file_name, &data)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user