jax_daemon/http_server/api/v0/bucket/
mkdir.rs1use axum::extract::State;
2use axum::response::IntoResponse;
3use axum::Json;
4use reqwest::{Client, RequestBuilder, Url};
5use serde::{Deserialize, Serialize};
6use std::path::PathBuf;
7use uuid::Uuid;
8
9use common::prelude::{Link, MountError};
10
11use crate::http_server::api::client::ApiRequest;
12use crate::ServiceState;
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct MkdirRequest {
16 pub bucket_id: Uuid,
17 pub path: String,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct MkdirResponse {
22 pub path: String,
23 pub link: Link,
24}
25
26pub async fn handler(
27 State(state): State<ServiceState>,
28 Json(request): Json<MkdirRequest>,
29) -> Result<impl IntoResponse, MkdirError> {
30 let path = PathBuf::from(&request.path);
31
32 let mut mount = state.peer().mount(request.bucket_id).await?;
34
35 mount.mkdir(&path).await?;
37
38 let new_link = state.peer().save_mount(&mount, false).await?;
40
41 Ok((
42 http::StatusCode::OK,
43 axum::Json(MkdirResponse {
44 path: request.path,
45 link: new_link,
46 }),
47 )
48 .into_response())
49}
50
51#[derive(Debug, thiserror::Error)]
52pub enum MkdirError {
53 #[error("Mount error: {0}")]
54 Mount(#[from] MountError),
55}
56
57impl IntoResponse for MkdirError {
58 fn into_response(self) -> axum::response::Response {
59 (
60 http::StatusCode::INTERNAL_SERVER_ERROR,
61 "Unexpected error".to_string(),
62 )
63 .into_response()
64 }
65}
66
67impl ApiRequest for MkdirRequest {
68 type Response = MkdirResponse;
69
70 fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
71 let full_url = base_url.join("/api/v0/bucket/mkdir").unwrap();
72 client.post(full_url).json(&self)
73 }
74}