opendal/services/seafile/
backend.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::sync::Arc;
21
22use http::Response;
23use http::StatusCode;
24use log::debug;
25use tokio::sync::RwLock;
26
27use super::core::parse_dir_detail;
28use super::core::parse_file_detail;
29use super::core::SeafileCore;
30use super::core::SeafileSigner;
31use super::delete::SeafileDeleter;
32use super::error::parse_error;
33use super::lister::SeafileLister;
34use super::writer::SeafileWriter;
35use super::writer::SeafileWriters;
36use crate::raw::*;
37use crate::services::SeafileConfig;
38use crate::*;
39
40impl Configurator for SeafileConfig {
41 type Builder = SeafileBuilder;
42
43 #[allow(deprecated)]
44 fn into_builder(self) -> Self::Builder {
45 SeafileBuilder {
46 config: self,
47 http_client: None,
48 }
49 }
50}
51
52#[doc = include_str!("docs.md")]
54#[derive(Default)]
55pub struct SeafileBuilder {
56 config: SeafileConfig,
57
58 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
59 http_client: Option<HttpClient>,
60}
61
62impl Debug for SeafileBuilder {
63 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64 let mut d = f.debug_struct("SeafileBuilder");
65
66 d.field("config", &self.config);
67 d.finish_non_exhaustive()
68 }
69}
70
71impl SeafileBuilder {
72 pub fn root(mut self, root: &str) -> Self {
76 self.config.root = if root.is_empty() {
77 None
78 } else {
79 Some(root.to_string())
80 };
81
82 self
83 }
84
85 pub fn endpoint(mut self, endpoint: &str) -> Self {
89 self.config.endpoint = if endpoint.is_empty() {
90 None
91 } else {
92 Some(endpoint.to_string())
93 };
94
95 self
96 }
97
98 pub fn username(mut self, username: &str) -> Self {
102 self.config.username = if username.is_empty() {
103 None
104 } else {
105 Some(username.to_string())
106 };
107
108 self
109 }
110
111 pub fn password(mut self, password: &str) -> Self {
115 self.config.password = if password.is_empty() {
116 None
117 } else {
118 Some(password.to_string())
119 };
120
121 self
122 }
123
124 pub fn repo_name(mut self, repo_name: &str) -> Self {
128 self.config.repo_name = repo_name.to_string();
129
130 self
131 }
132
133 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
140 #[allow(deprecated)]
141 pub fn http_client(mut self, client: HttpClient) -> Self {
142 self.http_client = Some(client);
143 self
144 }
145}
146
147impl Builder for SeafileBuilder {
148 const SCHEME: Scheme = Scheme::Seafile;
149 type Config = SeafileConfig;
150
151 fn build(self) -> Result<impl Access> {
153 debug!("backend build started: {:?}", &self);
154
155 let root = normalize_root(&self.config.root.clone().unwrap_or_default());
156 debug!("backend use root {}", &root);
157
158 if self.config.repo_name.is_empty() {
160 return Err(Error::new(ErrorKind::ConfigInvalid, "repo_name is empty")
161 .with_operation("Builder::build")
162 .with_context("service", Scheme::Seafile));
163 }
164
165 debug!("backend use repo_name {}", &self.config.repo_name);
166
167 let endpoint = match &self.config.endpoint {
168 Some(endpoint) => Ok(endpoint.clone()),
169 None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
170 .with_operation("Builder::build")
171 .with_context("service", Scheme::Seafile)),
172 }?;
173
174 let username = match &self.config.username {
175 Some(username) => Ok(username.clone()),
176 None => Err(Error::new(ErrorKind::ConfigInvalid, "username is empty")
177 .with_operation("Builder::build")
178 .with_context("service", Scheme::Seafile)),
179 }?;
180
181 let password = match &self.config.password {
182 Some(password) => Ok(password.clone()),
183 None => Err(Error::new(ErrorKind::ConfigInvalid, "password is empty")
184 .with_operation("Builder::build")
185 .with_context("service", Scheme::Seafile)),
186 }?;
187
188 Ok(SeafileBackend {
189 core: Arc::new(SeafileCore {
190 info: {
191 let am = AccessorInfo::default();
192 am.set_scheme(Scheme::Seafile)
193 .set_root(&root)
194 .set_native_capability(Capability {
195 stat: true,
196 stat_has_content_length: true,
197 stat_has_last_modified: true,
198
199 read: true,
200
201 write: true,
202 write_can_empty: true,
203
204 delete: true,
205
206 list: true,
207 list_has_content_length: true,
208 list_has_last_modified: true,
209
210 shared: true,
211
212 ..Default::default()
213 });
214
215 #[allow(deprecated)]
217 if let Some(client) = self.http_client {
218 am.update_http_client(|_| client);
219 }
220
221 am.into()
222 },
223 root,
224 endpoint,
225 username,
226 password,
227 repo_name: self.config.repo_name.clone(),
228 signer: Arc::new(RwLock::new(SeafileSigner::default())),
229 }),
230 })
231 }
232}
233
234#[derive(Debug, Clone)]
236pub struct SeafileBackend {
237 core: Arc<SeafileCore>,
238}
239
240impl Access for SeafileBackend {
241 type Reader = HttpBody;
242 type Writer = SeafileWriters;
243 type Lister = oio::PageLister<SeafileLister>;
244 type Deleter = oio::OneShotDeleter<SeafileDeleter>;
245 type BlockingReader = ();
246 type BlockingWriter = ();
247 type BlockingLister = ();
248 type BlockingDeleter = ();
249
250 fn info(&self) -> Arc<AccessorInfo> {
251 self.core.info.clone()
252 }
253
254 async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
255 if path == "/" {
256 return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
257 }
258
259 let metadata = if path.ends_with('/') {
260 let dir_detail = self.core.dir_detail(path).await?;
261 parse_dir_detail(dir_detail)
262 } else {
263 let file_detail = self.core.file_detail(path).await?;
264
265 parse_file_detail(file_detail)
266 };
267
268 metadata.map(RpStat::new)
269 }
270
271 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
272 let resp = self.core.download_file(path, args.range()).await?;
273
274 let status = resp.status();
275
276 match status {
277 StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
278 Ok((RpRead::default(), resp.into_body()))
279 }
280 _ => {
281 let (part, mut body) = resp.into_parts();
282 let buf = body.to_buffer().await?;
283 Err(parse_error(Response::from_parts(part, buf)))
284 }
285 }
286 }
287
288 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
289 let w = SeafileWriter::new(self.core.clone(), args, path.to_string());
290 let w = oio::OneShotWriter::new(w);
291
292 Ok((RpWrite::default(), w))
293 }
294
295 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
296 Ok((
297 RpDelete::default(),
298 oio::OneShotDeleter::new(SeafileDeleter::new(self.core.clone())),
299 ))
300 }
301
302 async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Lister)> {
303 let l = SeafileLister::new(self.core.clone(), path);
304 Ok((RpList::default(), oio::PageLister::new(l)))
305 }
306}