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
197 read: true,
198
199 write: true,
200 write_can_empty: true,
201
202 delete: true,
203
204 list: true,
205
206 shared: true,
207
208 ..Default::default()
209 });
210
211 #[allow(deprecated)]
213 if let Some(client) = self.http_client {
214 am.update_http_client(|_| client);
215 }
216
217 am.into()
218 },
219 root,
220 endpoint,
221 username,
222 password,
223 repo_name: self.config.repo_name.clone(),
224 signer: Arc::new(RwLock::new(SeafileSigner::default())),
225 }),
226 })
227 }
228}
229
230#[derive(Debug, Clone)]
232pub struct SeafileBackend {
233 core: Arc<SeafileCore>,
234}
235
236impl Access for SeafileBackend {
237 type Reader = HttpBody;
238 type Writer = SeafileWriters;
239 type Lister = oio::PageLister<SeafileLister>;
240 type Deleter = oio::OneShotDeleter<SeafileDeleter>;
241
242 fn info(&self) -> Arc<AccessorInfo> {
243 self.core.info.clone()
244 }
245
246 async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
247 if path == "/" {
248 return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
249 }
250
251 let metadata = if path.ends_with('/') {
252 let dir_detail = self.core.dir_detail(path).await?;
253 parse_dir_detail(dir_detail)
254 } else {
255 let file_detail = self.core.file_detail(path).await?;
256
257 parse_file_detail(file_detail)
258 };
259
260 metadata.map(RpStat::new)
261 }
262
263 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
264 let resp = self.core.download_file(path, args.range()).await?;
265
266 let status = resp.status();
267
268 match status {
269 StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
270 Ok((RpRead::default(), resp.into_body()))
271 }
272 _ => {
273 let (part, mut body) = resp.into_parts();
274 let buf = body.to_buffer().await?;
275 Err(parse_error(Response::from_parts(part, buf)))
276 }
277 }
278 }
279
280 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
281 let w = SeafileWriter::new(self.core.clone(), args, path.to_string());
282 let w = oio::OneShotWriter::new(w);
283
284 Ok((RpWrite::default(), w))
285 }
286
287 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
288 Ok((
289 RpDelete::default(),
290 oio::OneShotDeleter::new(SeafileDeleter::new(self.core.clone())),
291 ))
292 }
293
294 async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Lister)> {
295 let l = SeafileLister::new(self.core.clone(), path);
296 Ok((RpList::default(), oio::PageLister::new(l)))
297 }
298}