Skip to content

Commit

Permalink
fix(services/webdav): Recreate root directory if need (#4173)
Browse files Browse the repository at this point in the history
Use absolute paths for recreate all dirs.
Fixes issue #4172
  • Loading branch information
AJIOB committed Feb 8, 2024
1 parent e628198 commit 9f0ebb4
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions core/src/services/webdav/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,9 @@ impl WebdavBackend {
self.client.send(req).await
}

async fn webdav_mkcol(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));
async fn webdav_mkcol_absolute_path(&self, path: &str) -> Result<Response<IncomingAsyncBody>> {
debug_assert!(path.starts_with('/'), "path must be absolute path");
let url = format!("{}{}", self.endpoint, percent_encode_path(path));

let mut req = Request::builder().method("MKCOL").uri(&url);
if let Some(auth) = &self.authorization {
Expand All @@ -517,9 +516,19 @@ impl WebdavBackend {
path: &str,
headers: Option<HeaderMap>,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);
let p = build_rooted_abs_path(&self.root, path);

let url = format!("{}/{}", self.endpoint, percent_encode_path(&p));
self.webdav_propfind_absolute_path(&p, headers).await
}

async fn webdav_propfind_absolute_path(
&self,
path: &str,
headers: Option<HeaderMap>,
) -> Result<Response<IncomingAsyncBody>> {
debug_assert!(path.starts_with('/'), "path must be absolute path");

let url = format!("{}{}", self.endpoint, percent_encode_path(path));
let mut req = Request::builder().method("PROPFIND").uri(&url);

if let Some(auth) = &self.authorization {
Expand Down Expand Up @@ -620,7 +629,14 @@ impl WebdavBackend {
}

async fn create_dir_internal(&self, path: &str) -> Result<()> {
let resp = self.webdav_mkcol(path).await?;
let p = build_rooted_abs_path(&self.root, path);
self.create_dir_internal_absolute_path(&p).await
}

async fn create_dir_internal_absolute_path(&self, path: &str) -> Result<()> {
debug_assert!(path.starts_with('/'), "path must be absolute path");

let resp = self.webdav_mkcol_absolute_path(path).await?;

let status = resp.status();

Expand All @@ -638,7 +654,10 @@ impl WebdavBackend {
}
}

async fn ensure_parent_path(&self, mut path: &str) -> Result<()> {
async fn ensure_parent_path(&self, path: &str) -> Result<()> {
let path = build_rooted_abs_path(&self.root, path);
let mut path = path.as_str();

let mut dirs = VecDeque::default();

while path != "/" {
Expand All @@ -650,7 +669,9 @@ impl WebdavBackend {
header_map.insert("Depth", "0".parse().unwrap());
header_map.insert(header::ACCEPT, "application/xml".parse().unwrap());

let resp = self.webdav_propfind(parent, Some(header_map)).await?;
let resp = self
.webdav_propfind_absolute_path(parent, Some(header_map))
.await?;
match resp.status() {
StatusCode::OK | StatusCode::MULTI_STATUS => break,
StatusCode::NOT_FOUND => {
Expand All @@ -662,7 +683,7 @@ impl WebdavBackend {
}

for dir in dirs {
self.create_dir_internal(dir).await?;
self.create_dir_internal_absolute_path(dir).await?;
}
Ok(())
}
Expand Down

0 comments on commit 9f0ebb4

Please sign in to comment.