35 lines
880 B
Rust
35 lines
880 B
Rust
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?;
|
|
|
|
warren
|
|
.upload(state.serve_dir(), rest.as_deref(), &file_name, &data)
|
|
.await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|