1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
use crate::errors::AppError;
use crate::files::upload;
use crate::models::{
auth::Claims,
model::{Model, ModelCreate, ModelUpload, ModelUser},
};
use crate::pagination::Pagination;
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))
.route("/:id", get(get_model))
.route("/:id/upload", post(upload_model_file))
}
#[derive(Serialize)]
struct ModelPagination {
count: i64,
results: Vec<ModelUser>,
}
/// List models.
async fn list_models(pagination: Query<Pagination>) -> Result<Json<ModelPagination>, AppError> {
let page = pagination.0.page.unwrap_or_default();
let results = Model::list(page).await?;
let count = Model::count().await?;
Ok(Json(ModelPagination { count, results }))
}
/// Create a model. Checks Authorization token
async fn create_model(
Json(payload): Json<ModelCreate>,
claims: Claims,
) -> Result<Json<Model>, AppError> {
let model = Model::new(
payload.name,
payload.description,
payload.duration,
payload.height,
payload.weight,
payload.printer,
payload.material,
claims.user_id,
);
let model_new = Model::create(model).await?;
Ok(Json(model_new))
}
/// Get a model with id = `model_id`
async fn get_model(Path(model_id): Path<i32>) -> Result<Json<ModelUser>, AppError> {
match Model::find_by_id(model_id).await {
Ok(model) => Ok(Json(model)),
Err(_) => Err(AppError::NotFound("Model not found".to_string())),
}
}
/// Upload a file for a model
async fn upload_model_file(
claims: Claims,
Path(model_id): Path<i32>,
ContentLengthLimit(multipart): ContentLengthLimit<Multipart, { 1024 * 1024 }>,
) -> Result<Json<ModelUpload>, 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);
}
let allowed_extensions = vec!["stl", "obj", "png", "jpg", "jpeg", "gif", "webp", "blend"];
match upload(multipart, allowed_extensions, None).await {
Ok(saved_file) => {
let model_file = ModelUpload::create(ModelUpload::new(saved_file, model_id)).await?;
Ok(Json(model_file))
}
Err(e) => Err(e),
}
}
|