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 super::DEFAULT_SCHEME;
37use crate::raw::*;
38use crate::services::SeafileConfig;
39use crate::*;
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    type Config = SeafileConfig;
149
150    /// Builds the backend and returns the result of SeafileBackend.
151    fn build(self) -> Result<impl Access> {
152        debug!("backend build started: {:?}", &self);
153
154        let root = normalize_root(&self.config.root.clone().unwrap_or_default());
155        debug!("backend use root {}", &root);
156
157        // Handle bucket.
158        if self.config.repo_name.is_empty() {
159            return Err(Error::new(ErrorKind::ConfigInvalid, "repo_name is empty")
160                .with_operation("Builder::build")
161                .with_context("service", Scheme::Seafile));
162        }
163
164        debug!("backend use repo_name {}", &self.config.repo_name);
165
166        let endpoint = match &self.config.endpoint {
167            Some(endpoint) => Ok(endpoint.clone()),
168            None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
169                .with_operation("Builder::build")
170                .with_context("service", Scheme::Seafile)),
171        }?;
172
173        let username = match &self.config.username {
174            Some(username) => Ok(username.clone()),
175            None => Err(Error::new(ErrorKind::ConfigInvalid, "username is empty")
176                .with_operation("Builder::build")
177                .with_context("service", Scheme::Seafile)),
178        }?;
179
180        let password = match &self.config.password {
181            Some(password) => Ok(password.clone()),
182            None => Err(Error::new(ErrorKind::ConfigInvalid, "password is empty")
183                .with_operation("Builder::build")
184                .with_context("service", Scheme::Seafile)),
185        }?;
186
187        Ok(SeafileBackend {
188            core: Arc::new(SeafileCore {
189                info: {
190                    let am = AccessorInfo::default();
191                    am.set_scheme(DEFAULT_SCHEME)
192                        .set_root(&root)
193                        .set_native_capability(Capability {
194                            stat: true,
195
196                            read: true,
197
198                            write: true,
199                            write_can_empty: true,
200
201                            delete: true,
202
203                            list: true,
204
205                            shared: true,
206
207                            ..Default::default()
208                        });
209
210                    // allow deprecated api here for compatibility
211                    #[allow(deprecated)]
212                    if let Some(client) = self.http_client {
213                        am.update_http_client(|_| client);
214                    }
215
216                    am.into()
217                },
218                root,
219                endpoint,
220                username,
221                password,
222                repo_name: self.config.repo_name.clone(),
223                signer: Arc::new(RwLock::new(SeafileSigner::default())),
224            }),
225        })
226    }
227}
228
229/// Backend for seafile services.
230#[derive(Debug, Clone)]
231pub struct SeafileBackend {
232    core: Arc<SeafileCore>,
233}
234
235impl Access for SeafileBackend {
236    type Reader = HttpBody;
237    type Writer = SeafileWriters;
238    type Lister = oio::PageLister<SeafileLister>;
239    type Deleter = oio::OneShotDeleter<SeafileDeleter>;
240
241    fn info(&self) -> Arc<AccessorInfo> {
242        self.core.info.clone()
243    }
244
245    async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
246        if path == "/" {
247            return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
248        }
249
250        let metadata = if path.ends_with('/') {
251            let dir_detail = self.core.dir_detail(path).await?;
252            parse_dir_detail(dir_detail)
253        } else {
254            let file_detail = self.core.file_detail(path).await?;
255
256            parse_file_detail(file_detail)
257        };
258
259        metadata.map(RpStat::new)
260    }
261
262    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
263        let resp = self.core.download_file(path, args.range()).await?;
264
265        let status = resp.status();
266
267        match status {
268            StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
269                Ok((RpRead::default(), resp.into_body()))
270            }
271            _ => {
272                let (part, mut body) = resp.into_parts();
273                let buf = body.to_buffer().await?;
274                Err(parse_error(Response::from_parts(part, buf)))
275            }
276        }
277    }
278
279    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
280        let w = SeafileWriter::new(self.core.clone(), args, path.to_string());
281        let w = oio::OneShotWriter::new(w);
282
283        Ok((RpWrite::default(), w))
284    }
285
286    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
287        Ok((
288            RpDelete::default(),
289            oio::OneShotDeleter::new(SeafileDeleter::new(self.core.clone())),
290        ))
291    }
292
293    async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Lister)> {
294        let l = SeafileLister::new(self.core.clone(), path);
295        Ok((RpList::default(), oio::PageLister::new(l)))
296    }
297}