cloudreve_api/cloudreve_api/
share.rs1use crate::Error;
4use crate::api::v3::models as v3_models;
5use crate::api::v4::models as v4_models;
6use crate::client::UnifiedClient;
7use log::debug;
8
9#[derive(Debug, Clone)]
11pub struct ShareItem {
12 pub id: String,
13 pub name: String,
14 pub url: String,
15 pub created_at: String,
16 pub expired: bool,
17}
18
19impl super::CloudreveAPI {
21 pub async fn create_share(
25 &self,
26 path: &str,
27 _name: Option<&str>,
28 expires_in: Option<u32>,
29 password: Option<&str>,
30 ) -> Result<String, Error> {
31 debug!("Creating share link for: {}", path);
32
33 match &self.inner {
34 UnifiedClient::V3(client) => {
35 let request = v3_models::ShareRequest {
36 id: path.to_string(),
37 is_dir: path.ends_with('/'),
38 password: password.unwrap_or("").to_string(),
39 downloads: 0,
40 expire: expires_in.unwrap_or(0) as i32,
41 preview: true,
42 };
43 let share = client.create_share(&request).await?;
44 Ok(share.key)
45 }
46 UnifiedClient::V4(client) => {
47 let permissions = v4_models::PermissionSetting {
48 user_explicit: serde_json::json!({}),
49 group_explicit: serde_json::json!({}),
50 same_group: "read".to_string(),
51 other: "read".to_string(),
52 anonymous: "read".to_string(),
53 everyone: "read".to_string(),
54 };
55 let request = v4_models::CreateShareLinkRequest {
56 permissions,
57 uri: path.to_string(),
58 is_private: Some(password.is_some()),
59 share_view: None,
60 expire: expires_in,
61 price: None,
62 password: password.map(|p| p.to_string()),
63 show_readme: None,
64 };
65 let share = client.create_share_link(&request).await?;
66 Ok(share)
67 }
68 }
69 }
70
71 pub async fn list_shares(&self) -> Result<Vec<ShareItem>, Error> {
75 debug!("Listing shares");
76
77 match &self.inner {
78 UnifiedClient::V3(_client) => {
79 Ok(Vec::new())
82 }
83 UnifiedClient::V4(client) => {
84 let shares = client.list_my_share_links().await?;
85 Ok(shares
86 .into_iter()
87 .map(|s| ShareItem {
88 id: s.id,
89 name: s.name,
90 url: s.url,
91 created_at: s.created_at,
92 expired: s.expired,
93 })
94 .collect())
95 }
96 }
97 }
98
99 pub async fn update_share(&self, id: &str, props: &ShareUpdateProps) -> Result<(), Error> {
103 debug!("Updating share: {}", id);
104
105 match &self.inner {
106 UnifiedClient::V3(_client) => Err(Error::UnsupportedFeature(
107 "share update".to_string(),
108 "v3".to_string(),
109 )),
110 UnifiedClient::V4(client) => {
111 let permissions = v4_models::PermissionSetting {
112 user_explicit: serde_json::json!({}),
113 group_explicit: serde_json::json!({}),
114 same_group: "read".to_string(),
115 other: "read".to_string(),
116 anonymous: "read".to_string(),
117 everyone: "read".to_string(),
118 };
119 let request = v4_models::EditShareLinkRequest {
120 permissions,
121 uri: String::new(), share_view: None,
123 expire: props.expires,
124 price: None,
125 show_readme: None,
126 };
127 client.edit_share_link(id, &request).await?;
128 Ok(())
129 }
130 }
131 }
132
133 pub async fn delete_share(&self, id: &str) -> Result<(), Error> {
137 debug!("Deleting share: {}", id);
138
139 match &self.inner {
140 UnifiedClient::V3(_client) => Err(Error::UnsupportedFeature(
141 "share deletion".to_string(),
142 "v3".to_string(),
143 )),
144 UnifiedClient::V4(client) => {
145 client.delete_share_link(id).await?;
146 Ok(())
147 }
148 }
149 }
150}
151
152#[derive(Debug, Clone, Default)]
154pub struct ShareUpdateProps {
155 pub password: Option<String>,
156 pub expires: Option<u32>,
157}