diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-10 15:57:22 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-10 15:57:22 +0000 |
| commit | 25f225a0ddbd7cce9498c1c8c812ad61288599f2 (patch) | |
| tree | 0503b1408b7f545b83d555186d21da2613ec1478 /src/routes | |
| parent | 05dcdfd5f148f43887fc4cda78c0111ed8d7a7a0 (diff) | |
Route for model uploading files
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/model.rs | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/routes/model.rs b/src/routes/model.rs index 4ac2688..66f8ec6 100644 --- a/src/routes/model.rs +++ b/src/routes/model.rs @@ -1,15 +1,23 @@ +use crate::config::MAX_UPLOAD_FILE_SIZE; use crate::errors::AppError; +use crate::files::upload; use crate::models::{ auth::Claims, model::{Model, ModelCreate, ModelUser}, }; use crate::pagination::Pagination; -use axum::{extract::Query, routing::get, Json, Router}; +use axum::{ + extract::{ContentLengthLimit, Multipart, Path, Query}, + routing::{get, post}, + Json, Router, +}; use serde::Serialize; /// Create routes for `/v1/models/` namespace pub fn create_route() -> Router { - Router::new().route("/", get(list_models).post(create_model)) + Router::new() + .route("/", get(list_models).post(create_model)) + .route("/:id/upload", post(upload_model_file)) } #[derive(Serialize)] @@ -47,3 +55,30 @@ async fn create_model( Ok(Json(model_new)) } + +/// Upload a file for a model +async fn upload_model_file( + claims: Claims, + Path(model_id): Path<i32>, + ContentLengthLimit(multipart): ContentLengthLimit<Multipart, { MAX_UPLOAD_FILE_SIZE }>, +) -> Result<String, AppError> { + let model = match Model::find_by_id(model_id).await { + Ok(model) => model, + Err(_) => { + return Err(AppError::NotFound("Model not found".to_string())); + } + }; + + if model.author_id() != claims.user_id { + return Err(AppError::Unauthorized); + } + + match upload(multipart, vec!["stl"]).await { + Ok(saved_file) => { + return Ok(format!("Uploaded {}", saved_file)); + } + Err(e) => { + return Err(e); + } + } +} |
