hoslo commented on code in PR #3771: URL: https://github.com/apache/incubator-opendal/pull/3771#discussion_r1429872975
########## core/src/services/seafile/backend.rs: ########## @@ -0,0 +1,343 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use async_trait::async_trait; +use http::StatusCode; +use log::debug; +use serde::Deserialize; +use std::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; +use tokio::sync::RwLock; + +use super::core::parse_dir_detail; +use super::core::parse_file_detail; +use super::core::SeafileCore; +use super::error::parse_error; +use super::lister::SeafileLister; +use super::writer::SeafileWriter; +use super::writer::SeafileWriters; +use crate::raw::*; +use crate::services::seafile::core::AuthInfo; +use crate::*; + +/// Config for backblaze seafile services support. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct SeafileConfig { + /// root of this backend. + /// + /// All operations will happen under this root. + pub root: Option<String>, + /// server address of this backend. + /// + /// - If server is set, we will take user's input first. + /// - If not, we will try to load it from environment. + pub server: Option<String>, + /// username of this backend. + /// + /// - If username is set, we will take user's input first. + /// - If not, we will try to load it from environment. + pub username: Option<String>, + /// password of this backend. + /// + /// - If password is set, we will take user's input first. + /// - If not, we will try to load it from environment. + pub password: Option<String>, + /// repo_name of this backend. + /// + /// required. + pub repo_name: String, +} + +impl Debug for SeafileConfig { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("SeafileConfig"); + + d.field("root", &self.root) + .field("server", &self.server) + .field("username", &self.username) + .field("repo_name", &self.repo_name); + + d.finish_non_exhaustive() + } +} + +/// [seafile](https://www.seafile.com) services support. +#[doc = include_str!("docs.md")] +#[derive(Default)] +pub struct SeafileBuilder { + config: SeafileConfig, + + http_client: Option<HttpClient>, +} + +impl Debug for SeafileBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("SeafileBuilder"); + + d.field("config", &self.config); + d.finish_non_exhaustive() + } +} + +impl SeafileBuilder { + /// Set root of this backend. + /// + /// All operations will happen under this root. + pub fn root(&mut self, root: &str) -> &mut Self { + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + + self + } + + /// server of this backend. + pub fn server(&mut self, server: &str) -> &mut Self { + self.config.server = if server.is_empty() { + None + } else { + Some(server.to_string()) + }; + + self + } + + /// username of this backend. + pub fn username(&mut self, username: &str) -> &mut Self { + self.config.username = if username.is_empty() { + None + } else { + Some(username.to_string()) + }; + + self + } + + /// password of this backend. + pub fn password(&mut self, password: &str) -> &mut Self { + self.config.password = if password.is_empty() { + None + } else { + Some(password.to_string()) + }; + + self + } + + /// Set repo name of this backend. + /// You can find it in {server}/sys/all-libraries/ + pub fn repo_name(&mut self, repo_name: &str) -> &mut Self { + self.config.repo_name = repo_name.to_string(); + + self + } + + /// Specify the http client that used by this service. + /// + /// # Notes + /// + /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed + /// during minor updates. + pub fn http_client(&mut self, client: HttpClient) -> &mut Self { + self.http_client = Some(client); + self + } +} + +impl Builder for SeafileBuilder { + const SCHEME: Scheme = Scheme::Seafile; + type Accessor = SeafileBackend; + + /// Converts a HashMap into an SeafileBuilder instance. + /// + /// # Arguments + /// + /// * `map` - A HashMap containing the configuration values. + /// + /// # Returns + /// + /// Returns an instance of SeafileBuilder. + fn from_map(map: HashMap<String, String>) -> Self { + // Deserialize the configuration from the HashMap. + let config = SeafileConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"); + + // Create an SeafileBuilder instance with the deserialized config. + SeafileBuilder { + config, + http_client: None, + } + } + + /// Builds the backend and returns the result of SeafileBackend. + fn build(&mut self) -> Result<Self::Accessor> { + debug!("backend build started: {:?}", &self); + + let root = normalize_root(&self.config.root.clone().unwrap_or_default()); + debug!("backend use root {}", &root); + + // Handle bucket. + if self.config.repo_name.is_empty() { + return Err(Error::new(ErrorKind::ConfigInvalid, "repo_name is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Seafile)); + } + + debug!("backend use repo_name {}", &self.config.repo_name); + + let server = match &self.config.server { + Some(server) => Ok(server.clone()), + None => Err(Error::new(ErrorKind::ConfigInvalid, "server is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Seafile)), + }?; + + let username = match &self.config.username { + Some(username) => Ok(username.clone()), + None => Err(Error::new(ErrorKind::ConfigInvalid, "username is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Seafile)), + }?; + + let password = match &self.config.password { + Some(password) => Ok(password.clone()), + None => Err(Error::new(ErrorKind::ConfigInvalid, "password is empty") + .with_operation("Builder::build") + .with_context("service", Scheme::Seafile)), + }?; + + let client = if let Some(client) = self.http_client.take() { + client + } else { + HttpClient::new().map_err(|err| { + err.with_operation("Builder::build") + .with_context("service", Scheme::Seafile) + })? + }; + + Ok(SeafileBackend { + core: Arc::new(SeafileCore { + root, + server, + username, + password, + repo_name: self.config.repo_name.clone(), + auth_info: Arc::new(RwLock::new(AuthInfo::default())), + client, + }), + }) + } +} + +/// Backend for seafile services. +#[derive(Debug, Clone)] +pub struct SeafileBackend { + core: Arc<SeafileCore>, +} + +#[async_trait] +impl Accessor for SeafileBackend { + type Reader = IncomingAsyncBody; + + type BlockingReader = (); + + type Writer = SeafileWriters; + + type BlockingWriter = (); + + type Lister = oio::PageLister<SeafileLister>; + + type BlockingLister = (); + + fn info(&self) -> AccessorInfo { + let mut am = AccessorInfo::default(); + am.set_scheme(Scheme::Seafile) + .set_root(&self.core.root) + .set_native_capability(Capability { + stat: true, + + read: true, + read_can_next: true, + + write: true, + write_can_empty: true, + + delete: true, + + list: true, + + ..Default::default() + }); + + am + } + + async fn read(&self, path: &str, _args: OpRead) -> Result<(RpRead, Self::Reader)> { + let resp = self.core.download_file(path).await?; + + let status = resp.status(); + + match status { + StatusCode::OK => { + let size = parse_content_length(resp.headers())?; + Ok((RpRead::new().with_size(size), resp.into_body())) + } + _ => Err(parse_error(resp).await?), + } + } + + async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> { + if path == "/" { + return Ok(RpStat::new(Metadata::new(EntryMode::DIR))); + } + + let metadata = if path.ends_with('/') { Review Comment: In Seafile, abc/ should be a folder, while abc is a file. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
