service/http_server/api/v0/bucket/
ls.rs1use axum::extract::{Json, State};
2use axum::response::{IntoResponse, Response};
3use reqwest::{Client, RequestBuilder, Url};
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7use common::prelude::{Link, MountError};
8
9use crate::http_server::api::client::ApiRequest;
10use crate::ServiceState;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[cfg_attr(feature = "clap", derive(clap::Args))]
14pub struct LsRequest {
15 #[cfg_attr(feature = "clap", arg(long))]
17 pub bucket_id: Uuid,
18
19 #[serde(skip_serializing_if = "Option::is_none")]
21 #[cfg_attr(feature = "clap", arg(long))]
22 pub path: Option<String>,
23
24 #[serde(skip_serializing_if = "Option::is_none")]
26 #[cfg_attr(feature = "clap", arg(long))]
27 pub deep: Option<bool>,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct LsResponse {
32 pub items: Vec<PathInfo>,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct PathInfo {
37 pub path: String,
38 pub name: String,
39 pub link: Link,
40 pub is_dir: bool,
41 pub mime_type: String,
42}
43
44#[axum::debug_handler]
45pub async fn handler(
46 State(state): State<ServiceState>,
47 Json(req): Json<LsRequest>,
48) -> Result<impl IntoResponse, LsError> {
49 let deep = req.deep.unwrap_or(false);
50
51 let items = crate::mount_ops::list_bucket_contents(req.bucket_id, req.path, deep, &state)
53 .await
54 .map_err(|e| match e {
55 crate::mount_ops::MountOpsError::BucketNotFound(id) => LsError::BucketNotFound(id),
56 crate::mount_ops::MountOpsError::Mount(me) => LsError::Mount(me),
57 e => LsError::MountOps(e.to_string()),
58 })?;
59
60 let path_infos = items
62 .into_iter()
63 .map(|item| PathInfo {
64 path: item.path,
65 name: item.name,
66 link: item.link,
67 is_dir: item.is_dir,
68 mime_type: item.mime_type,
69 })
70 .collect();
71
72 Ok((http::StatusCode::OK, Json(LsResponse { items: path_infos })).into_response())
73}
74
75#[derive(Debug, thiserror::Error)]
76pub enum LsError {
77 #[error("Bucket not found: {0}")]
78 BucketNotFound(Uuid),
79 #[error("MountOps error: {0}")]
80 MountOps(String),
81 #[error("Mount error: {0}")]
82 Mount(#[from] MountError),
83}
84
85impl IntoResponse for LsError {
86 fn into_response(self) -> Response {
87 match self {
88 LsError::BucketNotFound(id) => (
89 http::StatusCode::NOT_FOUND,
90 format!("Bucket not found: {}", id),
91 )
92 .into_response(),
93 LsError::MountOps(_) | LsError::Mount(_) => (
94 http::StatusCode::INTERNAL_SERVER_ERROR,
95 "Unexpected error".to_string(),
96 )
97 .into_response(),
98 }
99 }
100}
101
102impl ApiRequest for LsRequest {
104 type Response = LsResponse;
105
106 fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
107 let full_url = base_url.join("/api/v0/bucket/ls").unwrap();
108 client.post(full_url).json(&self)
109 }
110}