.
This commit is contained in:
54
src/web/route/file/upload.rs
Normal file
54
src/web/route/file/upload.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use axum::Json;
|
||||
use axum::body::Bytes;
|
||||
use axum::extract::State;
|
||||
use axum::response::IntoResponse;
|
||||
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
|
||||
use validator::Validate;
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::util::SerdeFieldData;
|
||||
use crate::{object_store, web};
|
||||
|
||||
#[derive(Debug, Validate, TryFromMultipart)]
|
||||
#[try_from_multipart(rename_all = "camelCase")]
|
||||
pub struct UploadPayload {
|
||||
#[form_data(limit = "50MB")]
|
||||
#[validate(length(min = 1, max = 16))]
|
||||
files: Vec<SerdeFieldData<Bytes>>,
|
||||
}
|
||||
|
||||
pub async fn upload(
|
||||
State(state): State<AppState>,
|
||||
TypedMultipart(payload): TypedMultipart<UploadPayload>,
|
||||
) -> web::Result<impl IntoResponse> {
|
||||
match payload.validate() {
|
||||
Ok(_) => {},
|
||||
Err(e) => return Err(web::error::ClientError::ValidationFailed(e).into()),
|
||||
};
|
||||
|
||||
let mut file_ids = Vec::new();
|
||||
|
||||
for file in payload.files {
|
||||
let db_file = state
|
||||
.database
|
||||
.insert_file(
|
||||
file.metadata
|
||||
.file_name
|
||||
.as_deref()
|
||||
.unwrap_or_else(|| "unknown"),
|
||||
file.metadata.content_type.as_deref().unwrap_or_default(),
|
||||
file.contents.len(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
state
|
||||
.object_store
|
||||
.put_object(&db_file.id.to_string(), &file.contents)
|
||||
.await
|
||||
.map_err(object_store::Error::from)?;
|
||||
|
||||
file_ids.push(db_file.id);
|
||||
}
|
||||
|
||||
Ok(Json(file_ids))
|
||||
}
|
||||
Reference in New Issue
Block a user