Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webdav): support copy #1870

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions core/src/services/webdav/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::*;
///
/// - [x] read
/// - [x] write
/// - [x] copy
/// - [x] list
/// - [ ] ~~scan~~
/// - [ ] ~~presign~~
Expand Down Expand Up @@ -265,30 +266,21 @@ impl Accessor for WebdavBackend {
ma.set_scheme(Scheme::Webdav)
.set_root(&self.root)
.set_capabilities(
AccessorCapability::Read | AccessorCapability::Write | AccessorCapability::List,
AccessorCapability::Read
| AccessorCapability::Write
| AccessorCapability::Copy
| AccessorCapability::List,
)
.set_hints(AccessorHint::ReadStreamable);

ma
}

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
// create dir recursively, split path by `/` and create each dir except the last one
let abs_path = build_abs_path(&self.root, path);
let abs_path = abs_path.as_str();
let mut parts: Vec<&str> = abs_path.split('/').filter(|x| !x.is_empty()).collect();
if !parts.is_empty() {
parts.pop();
}
self.ensure_parent_path(path).await?;

let mut sub_path = String::new();
for sub_part in parts {
let sub_path_with_slash = sub_part.to_owned() + "/";
sub_path.push_str(&sub_path_with_slash);
self.create_internal(&sub_path).await?;
}

self.create_internal(abs_path).await
let abs_path = build_abs_path(&self.root, path);
self.create_internal(&abs_path).await
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down Expand Up @@ -318,6 +310,19 @@ impl Accessor for WebdavBackend {
Ok((RpWrite::default(), WebdavWriter::new(self.clone(), args, p)))
}

async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
self.ensure_parent_path(to).await?;

let resp = self.webdav_copy(from, to).await?;

let status = resp.status();

match status {
StatusCode::CREATED | StatusCode::NO_CONTENT => Ok(RpCopy::default()),
_ => Err(parse_error(resp).await?),
}
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
// Stat root always returns a DIR.
if path == "/" {
Expand Down Expand Up @@ -549,6 +554,31 @@ impl WebdavBackend {
self.client.send_async(req).await
}

async fn webdav_copy(&self, from: &str, to: &str) -> Result<Response<IncomingAsyncBody>> {
suyanhanx marked this conversation as resolved.
Show resolved Hide resolved
let source = build_abs_path(&self.root, from);
let target = build_abs_path(&self.root, to);

let source = format!("{}/{}", self.endpoint, percent_encode_path(&source));
let target = format!("{}/{}", self.endpoint, percent_encode_path(&target));

let mut req = Request::builder().method("COPY").uri(&source);

if let Some(auth) = &self.authorization {
req = req.header(header::AUTHORIZATION, auth);
}

req = req.header("Destination", target);

// We always specific "T" for keeping to overwrite the destination.
req = req.header("Overwrite", "T");

let req = req
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;

self.client.send_async(req).await
}

async fn create_internal(&self, abs_path: &str) -> Result<RpCreate> {
let resp = if abs_path.ends_with('/') {
self.webdav_mkcol(abs_path, None, None, AsyncBody::Empty)
Expand All @@ -575,4 +605,23 @@ impl WebdavBackend {
_ => Err(parse_error(resp).await?),
}
}

async fn ensure_parent_path(&self, path: &str) -> Result<()> {
// create dir recursively, split path by `/` and create each dir except the last one
let abs_path = build_abs_path(&self.root, path);
let abs_path = abs_path.as_str();
let mut parts: Vec<&str> = abs_path.split('/').filter(|x| !x.is_empty()).collect();
if !parts.is_empty() {
parts.pop();
}

let mut sub_path = String::new();
for sub_part in parts {
let sub_path_with_slash = sub_part.to_owned() + "/";
sub_path.push_str(&sub_path_with_slash);
self.create_internal(&sub_path).await?;
}

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ http {
location / {
client_body_temp_path /tmp;
log_not_found off;
dav_methods PUT DELETE MKCOL;
dav_methods PUT DELETE MKCOL COPY;
dav_ext_methods PROPFIND;
create_full_put_path on;
client_max_body_size 1024M;
Expand Down
2 changes: 1 addition & 1 deletion core/src/services/webdav/fixtures/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ http {
location / {
client_body_temp_path /tmp;
log_not_found off;
dav_methods PUT DELETE MKCOL;
dav_methods PUT DELETE MKCOL COPY;
dav_ext_methods PROPFIND;
create_full_put_path on;
client_max_body_size 1024M;
Expand Down