opendal/services/seafile/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use 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/// [seafile](https://www.seafile.com) services support.
53#[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    /// Set root of this backend.
73    ///
74    /// All operations will happen under this root.
75    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    /// endpoint of this backend.
86    ///
87    /// It is required. e.g. `http://127.0.0.1:80`
88    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    /// username of this backend.
99    ///
100    /// It is required. e.g. `me@example.com`
101    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    /// password of this backend.
112    ///
113    /// It is required. e.g. `asecret`
114    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    /// Set repo name of this backend.
125    ///
126    /// It is required. e.g. `myrepo`
127    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    /// Specify the http client that used by this service.
134    ///
135    /// # Notes
136    ///
137    /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
138    /// during minor updates.
139    #[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    /// Builds the backend and returns the result of SeafileBackend.
152    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        // Handle bucket.
159        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 api here for compatibility
216                    #[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/// Backend for seafile services.
235#[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}