Skip to main content

swarmhive_api_types/
storage.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use utoipa::ToSchema;
4use uuid::Uuid;
5
6/// 某个 backend 的对象如何生成下载 URL。
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
8#[serde(rename_all = "lowercase")]
9pub enum UrlMode {
10    /// 公开桶:直接给出以 `public_base_url` 为根的明文 URL。
11    Public,
12    /// 私有桶:给出短时效的预签名 GET URL。
13    Signed,
14}
15
16/// S3 兼容存储后端配置。secret 永不返回——`secret_set` 表示是否已存有 secret。
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
18pub struct StorageBackendView {
19    pub id: Uuid,
20    pub name: String,
21    pub kind: String,
22    pub active: bool,
23    pub endpoint: String,
24    pub bucket: String,
25    pub region: String,
26    pub access_key_id: String,
27    pub secret_set: bool,
28    pub force_path_style: bool,
29    pub prefix: Option<String>,
30    pub public_base_url: Option<String>,
31    pub url_mode: UrlMode,
32    pub signed_url_ttl_secs: i64,
33    pub supports_sha256_checksum: bool,
34    pub connectivity_status: Option<serde_json::Value>,
35    pub created_at: DateTime<Utc>,
36    pub updated_at: DateTime<Utc>,
37}
38
39fn default_ttl_secs() -> i64 {
40    600
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
44pub struct CreateStorageBackendRequest {
45    pub name: String,
46    pub endpoint: String,
47    pub bucket: String,
48    pub region: String,
49    pub access_key_id: String,
50    pub access_key_secret: String,
51    #[serde(default)]
52    pub force_path_style: bool,
53    #[serde(default)]
54    pub prefix: Option<String>,
55    #[serde(default)]
56    pub public_base_url: Option<String>,
57    pub url_mode: UrlMode,
58    #[serde(default = "default_ttl_secs")]
59    pub signed_url_ttl_secs: i64,
60}
61
62/// 全可选的 patch。`access_key_secret` 缺省 / 为空则保留已存的 secret 不变。
63#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema)]
64pub struct UpdateStorageBackendRequest {
65    #[serde(default)]
66    pub name: Option<String>,
67    #[serde(default)]
68    pub endpoint: Option<String>,
69    #[serde(default)]
70    pub bucket: Option<String>,
71    #[serde(default)]
72    pub region: Option<String>,
73    #[serde(default)]
74    pub access_key_id: Option<String>,
75    #[serde(default)]
76    pub access_key_secret: Option<String>,
77    #[serde(default)]
78    pub force_path_style: Option<bool>,
79    #[serde(default)]
80    pub prefix: Option<String>,
81    #[serde(default)]
82    pub public_base_url: Option<String>,
83    #[serde(default)]
84    pub url_mode: Option<UrlMode>,
85    #[serde(default)]
86    pub signed_url_ttl_secs: Option<i64>,
87}
88
89/// `POST /storage/backends/:id/test` 的返回结果。
90#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
91pub struct StorageTestResult {
92    pub ok: bool,
93    pub supports_sha256_checksum: bool,
94    pub detail: String,
95}
96
97/// `POST /storage/backends/:id/cors` 的请求:把这些源写进桶 CORS,放行浏览器直传。
98/// 通常是 Admin SPA 自己的 origin(`window.location.origin`)。
99#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
100pub struct CorsConfigRequest {
101    pub allowed_origins: Vec<String>,
102}
103
104/// `POST /storage/backends/:id/cors` 的返回结果。`ok=false` 表示后端(如阿里云 OSS
105/// 的 S3 兼容层)不支持 `PutBucketCors`,`detail` 给出手动配置指引(非 5xx)。
106#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
107pub struct CorsConfigResult {
108    pub ok: bool,
109    pub detail: String,
110}