From 9ef7aebdc6b4fc2ca649672f6b72a4501b436597 Mon Sep 17 00:00:00 2001 From: Massimiliano Torromeo Date: Tue, 16 Aug 2022 17:30:40 +0200 Subject: [PATCH 1/2] Apply prefix to WebDAV paths before encoding the URL --- src/WebDAV/WebDAVAdapter.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/WebDAV/WebDAVAdapter.php b/src/WebDAV/WebDAVAdapter.php index 83d0f3d02..0ee6eb34f 100644 --- a/src/WebDAV/WebDAVAdapter.php +++ b/src/WebDAV/WebDAVAdapter.php @@ -62,7 +62,7 @@ public function __construct( public function fileExists(string $path): bool { - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $properties = $this->client->propFind($location, ['{DAV:}resourcetype', '{DAV:}iscollection']); @@ -90,7 +90,7 @@ protected function encodePath(string $path): string public function directoryExists(string $path): bool { - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $properties = $this->client->propFind($location, ['{DAV:}resourcetype', '{DAV:}iscollection']); @@ -121,7 +121,7 @@ public function writeStream(string $path, $contents, Config $config): void private function upload(string $path, mixed $contents): void { $this->createParentDirFor($path); - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $response = $this->client->request('PUT', $location, $contents); @@ -137,7 +137,7 @@ private function upload(string $path, mixed $contents): void public function read(string $path): string { - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $response = $this->client->request('GET', $location); @@ -154,7 +154,7 @@ public function read(string $path): string public function readStream(string $path) { - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $url = $this->client->getAbsoluteUrl($location); @@ -174,7 +174,7 @@ public function readStream(string $path) public function delete(string $path): void { - $location = $this->prefixer->prefixPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixPath($path)); try { $response = $this->client->request('DELETE', $location); @@ -192,7 +192,7 @@ public function delete(string $path): void public function deleteDirectory(string $path): void { - $location = $this->prefixer->prefixDirectoryPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixDirectoryPath($path)); try { $statusCode = $this->client->request('DELETE', $location)['statusCode']; @@ -209,13 +209,13 @@ public function deleteDirectory(string $path): void public function createDirectory(string $path, Config $config): void { - $parts = explode('/', $path); + $parts = explode('/', $this->prefixer->prefixDirectoryPath($path)); $directoryParts = []; foreach ($parts as $directory) { $directoryParts[] = $directory; $directoryPath = implode('/', $directoryParts); - $location = $this->prefixer->prefixDirectoryPath($this->encodePath($directoryPath)); + $location = $this->encodePath($directoryPath); if ($this->directoryExists($directoryPath)) { continue; @@ -268,7 +268,7 @@ public function fileSize(string $path): FileAttributes public function listContents(string $path, bool $deep): iterable { - $location = $this->prefixer->prefixDirectoryPath($this->encodePath($path)); + $location = $this->encodePath($this->prefixer->prefixDirectoryPath($path)); $response = $this->client->propFind($location, self::FIND_PROPERTIES, 1); array_shift($response); @@ -330,8 +330,8 @@ public function move(string $source, string $destination, Config $config): void } $this->createParentDirFor($destination); - $location = $this->prefixer->prefixPath($this->encodePath($source)); - $newLocation = $this->prefixer->prefixPath($this->encodePath($destination)); + $location = $this->encodePath($this->prefixer->prefixPath($source)); + $newLocation = $this->encodePath($this->prefixer->prefixPath($destination)); try { $response = $this->client->request('MOVE', '/' . ltrim($location, '/'), null, [ @@ -367,8 +367,8 @@ public function copy(string $source, string $destination, Config $config): void } $this->createParentDirFor($destination); - $location = $this->prefixer->prefixPath($this->encodePath($source)); - $newLocation = $this->prefixer->prefixPath($this->encodePath($destination)); + $location = $this->encodePath($this->prefixer->prefixPath($source)); + $newLocation = $this->encodePath($this->prefixer->prefixPath($destination)); try { $response = $this->client->request('COPY', '/' . ltrim($location, '/'), null, [ From c1da0a3f0daf498c0338054dd0e0d7a241d2a18c Mon Sep 17 00:00:00 2001 From: Massimiliano Torromeo Date: Tue, 16 Aug 2022 17:31:32 +0200 Subject: [PATCH 2/2] Recursive directory creation should also consider the prefix --- src/WebDAV/WebDAVAdapter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/WebDAV/WebDAVAdapter.php b/src/WebDAV/WebDAVAdapter.php index 0ee6eb34f..243567fe5 100644 --- a/src/WebDAV/WebDAVAdapter.php +++ b/src/WebDAV/WebDAVAdapter.php @@ -213,6 +213,10 @@ public function createDirectory(string $path, Config $config): void $directoryParts = []; foreach ($parts as $directory) { + if ($directory === '.' || $directory === '') { + return; + } + $directoryParts[] = $directory; $directoryPath = implode('/', $directoryParts); $location = $this->encodePath($directoryPath); @@ -410,10 +414,6 @@ private function createParentDirFor(string $path): void { $dirname = dirname($path); - if ($dirname === '.' || $dirname === '') { - return; - } - if ($this->directoryExists($dirname)) { return; }