use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;
use log::debug;
use serde::Deserialize;
use tokio::sync::RwLock;
use super::core::parse_dir_detail;
use super::core::parse_file_detail;
use super::core::SeafileCore;
use super::lister::SeafileLister;
use super::writer::SeafileWriter;
use super::writer::SeafileWriters;
use crate::raw::*;
use crate::services::seafile::core::SeafileSigner;
use crate::services::seafile::reader::SeafileReader;
use crate::*;
#[derive(Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct SeafileConfig {
pub root: Option<String>,
pub endpoint: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
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("endpoint", &self.endpoint)
.field("username", &self.username)
.field("repo_name", &self.repo_name);
d.finish_non_exhaustive()
}
}
#[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 {
pub fn root(&mut self, root: &str) -> &mut Self {
self.config.root = if root.is_empty() {
None
} else {
Some(root.to_string())
};
self
}
pub fn endpoint(&mut self, endpoint: &str) -> &mut Self {
self.config.endpoint = if endpoint.is_empty() {
None
} else {
Some(endpoint.to_string())
};
self
}
pub fn username(&mut self, username: &str) -> &mut Self {
self.config.username = if username.is_empty() {
None
} else {
Some(username.to_string())
};
self
}
pub fn password(&mut self, password: &str) -> &mut Self {
self.config.password = if password.is_empty() {
None
} else {
Some(password.to_string())
};
self
}
pub fn repo_name(&mut self, repo_name: &str) -> &mut Self {
self.config.repo_name = repo_name.to_string();
self
}
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;
fn from_map(map: HashMap<String, String>) -> Self {
let config = SeafileConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed");
SeafileBuilder {
config,
http_client: None,
}
}
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);
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 endpoint = match &self.config.endpoint {
Some(endpoint) => Ok(endpoint.clone()),
None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint 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,
endpoint,
username,
password,
repo_name: self.config.repo_name.clone(),
signer: Arc::new(RwLock::new(SeafileSigner::default())),
client,
}),
})
}
}
#[derive(Debug, Clone)]
pub struct SeafileBackend {
core: Arc<SeafileCore>,
}
impl Access for SeafileBackend {
type Reader = SeafileReader;
type Writer = SeafileWriters;
type Lister = oio::PageLister<SeafileLister>;
type BlockingReader = ();
type BlockingWriter = ();
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,
write: true,
write_can_empty: true,
delete: true,
list: true,
..Default::default()
});
am
}
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('/') {
let dir_detail = self.core.dir_detail(path).await?;
parse_dir_detail(dir_detail)
} else {
let file_detail = self.core.file_detail(path).await?;
parse_file_detail(file_detail)
};
metadata.map(RpStat::new)
}
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Ok((
RpRead::default(),
SeafileReader::new(self.core.clone(), path, args),
))
}
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
let w = SeafileWriter::new(self.core.clone(), args, path.to_string());
let w = oio::OneShotWriter::new(w);
Ok((RpWrite::default(), w))
}
async fn delete(&self, path: &str, _args: OpDelete) -> Result<RpDelete> {
self.core.delete(path).await?;
Ok(RpDelete::default())
}
async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Lister)> {
let l = SeafileLister::new(self.core.clone(), path);
Ok((RpList::default(), oio::PageLister::new(l)))
}
}