summaryrefslogtreecommitdiffstats
path: root/src/routes/model.rs
blob: 4f935a90f340a3e473eaa3b73371b4721ad04aea (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{
    errors::AppError,
    files::{delete_upload, upload},
    models::{
        auth::Claims,
        model::{Model, ModelCreate, ModelUpload, ModelUser},
        user::User,
    },
    pagination::Pagination,
    routes::JsonCreate,
};
use axum::{
    extract::{ContentLengthLimit, Multipart, Path, Query},
    http::StatusCode,
    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).delete(delete_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<JsonCreate<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(JsonCreate(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),
    }
}

/// The owner or a staffer can delete a model
async fn delete_model(claims: Claims, Path(model_id): Path<i32>) -> Result<StatusCode, AppError> {
    let model = match Model::find_by_id(model_id).await {
        Ok(model) => model,
        Err(_) => {
            return Err(AppError::NotFound("Model not found".to_string()));
        }
    };

    let user = User::find_by_id(claims.user_id).await?;

    let uploads: Vec<String> = model.upload_paths().await.unwrap();

    if model.author_id() != user.id {
        if !user.is_staff.unwrap() {
            return Err(AppError::Unauthorized);
        }
    }

    // If the model has been deleted, remove all old uploads from the file system
    if Model::delete(model_id).await.is_ok() {
        uploads
            .iter()
            .for_each(|path: &String| delete_upload(path).unwrap_or_default());
    }

    Ok(StatusCode::NO_CONTENT)
}