From cfdbc7809854396994955ba3288ab696db4149bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 20 Aug 2023 11:56:08 +0000 Subject: [PATCH 1/8] Expose getFiles in local device --- src/Storage/Device/Local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 6d5c0f13..7f3190eb 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -503,7 +503,7 @@ public function getPartitionTotalSpace(): float * @param string $dir Directory to scan * @return string[] */ - private function getFiles(string $dir): array + public function getFiles(string $dir): array { if (! (\str_ends_with($dir, DIRECTORY_SEPARATOR))) { $dir .= DIRECTORY_SEPARATOR; From d23bf23b7f30598be41db21cda667edce14d002d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:12:05 +0000 Subject: [PATCH 2/8] add tests, update adapter with new method --- src/Storage/Device.php | 8 ++++++++ src/Storage/Device/Local.php | 2 +- src/Storage/Device/S3.php | 11 +++++++++++ tests/Storage/Device/LocalTest.php | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Storage/Device.php b/src/Storage/Device.php index 23027d94..fdc73483 100644 --- a/src/Storage/Device.php +++ b/src/Storage/Device.php @@ -267,6 +267,14 @@ abstract public function getPartitionFreeSpace(): float; */ abstract public function getPartitionTotalSpace(): float; + /** + * Get all files and directories inside a directory. + * + * @param string $dir Directory to scan + * @return string[] + */ + abstract public function getFiles(string $dir): array; + /** * Get the absolute path by resolving strings like ../, .., //, /\ and so on. * diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 7f3190eb..b4b3eecb 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -498,7 +498,7 @@ public function getPartitionTotalSpace(): float } /** - * Get all files inside a directory. + * Get all files and directories inside a directory. * * @param string $dir Directory to scan * @return string[] diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 2c771719..625056b5 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -653,6 +653,17 @@ public function getPartitionTotalSpace(): float return -1; } + /** + * Get all files and directories inside a directory. + * + * @param string $dir Directory to scan + * @return string[] + */ + public function getFiles(string $dir): array + { + throw new Exception('Not implemented.'); + } + /** * Get file info * diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index 9db0510c..ab106a8b 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -323,4 +323,22 @@ public function testDeletePath() $this->assertEquals(false, $this->object->exists($path2)); $this->assertEquals(false, $this->object->exists($path3)); } + + public function testGetFiles() + { + $files = $this->object->getFiles(DIRECTORY_SEPARATOR); + $count = \count($files); + + $path = $this->object->getPath('new-file.txt'); + $this->object->write($path, 'Hello World'); + + $path = $this->object->getPath('new-file-two.txt'); + $this->object->write($path, 'Hello World'); + + $files = $this->object->getFiles(DIRECTORY_SEPARATOR); + $count2 = \count($files); + + $this->assertEquals($count2, $count + 2); + + } } From bd4c9935232a31c8f97d40261646556805fc1940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:36:25 +0000 Subject: [PATCH 3/8] Fix new test --- tests/Storage/Device/LocalTest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index ab106a8b..af679efe 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -326,19 +326,17 @@ public function testDeletePath() public function testGetFiles() { - $files = $this->object->getFiles(DIRECTORY_SEPARATOR); - $count = \count($files); + $dir = DIRECTORY_SEPARATOR.'get-files-test'; - $path = $this->object->getPath('new-file.txt'); - $this->object->write($path, 'Hello World'); - - $path = $this->object->getPath('new-file-two.txt'); - $this->object->write($path, 'Hello World'); + $this->assertTrue($this->object->createDirectory($dir)); - $files = $this->object->getFiles(DIRECTORY_SEPARATOR); - $count2 = \count($files); + $files = $this->object->getFiles($dir); + $this->assertEquals(0, \count($files)); - $this->assertEquals($count2, $count + 2); + $this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World'); + $this->object->write($dir.DIRECTORY_SEPARATOR.'new-file-two.txt', 'Hello World'); + $files = $this->object->getFiles($dir); + $this->assertEquals(2, \count($files)); } } From 6167c5fe5a8703e06b370d488d2cea8260ccc31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:49:02 +0000 Subject: [PATCH 4/8] Fix nested deletePath --- src/Storage/Device/Local.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index b4b3eecb..1ef5b6c1 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -348,21 +348,25 @@ public function delete(string $path, bool $recursive = false): bool */ public function deletePath(string $path): bool { - $path = realpath($this->getRoot().DIRECTORY_SEPARATOR.$path); + $path = realpath($this->getRoot() . DIRECTORY_SEPARATOR . $path); - if (\is_dir($path)) { - $files = $this->getFiles($path); + if (!file_exists($path) || is_dir($path)) { + return false; + } - foreach ($files as $file) { + $files = $this->getFiles($path); + + foreach ($files as $file) { + if(is_dir($file)) { + $this->deletePath($file); + } else { $this->delete($file, true); } - - \rmdir($path); - - return true; } - return false; + \rmdir($path); + + return true; } /** From ace0d954d72b9e86ee56f47c5786b58ebbcdd1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:49:12 +0000 Subject: [PATCH 5/8] Linter fix --- src/Storage/Device/Local.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 1ef5b6c1..cfa421e5 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -348,16 +348,16 @@ public function delete(string $path, bool $recursive = false): bool */ public function deletePath(string $path): bool { - $path = realpath($this->getRoot() . DIRECTORY_SEPARATOR . $path); + $path = realpath($this->getRoot().DIRECTORY_SEPARATOR.$path); - if (!file_exists($path) || is_dir($path)) { + if (! file_exists($path) || is_dir($path)) { return false; } $files = $this->getFiles($path); foreach ($files as $file) { - if(is_dir($file)) { + if (is_dir($file)) { $this->deletePath($file); } else { $this->delete($file, true); From 74ad4ec3e5b47ff1e47808a6ddc8a30c437add81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:52:14 +0000 Subject: [PATCH 6/8] Fix bug in deletePath --- src/Storage/Device/Local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index cfa421e5..a08fe60e 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -350,7 +350,7 @@ public function deletePath(string $path): bool { $path = realpath($this->getRoot().DIRECTORY_SEPARATOR.$path); - if (! file_exists($path) || is_dir($path)) { + if (! file_exists($path) || ! is_dir($path)) { return false; } From 1dfdf99fe92512b8aa0f17c1100bdaf41751a327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 08:56:30 +0000 Subject: [PATCH 7/8] Add test for nested delete changes --- tests/Storage/Device/LocalTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index af679efe..b341a82c 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -339,4 +339,21 @@ public function testGetFiles() $files = $this->object->getFiles($dir); $this->assertEquals(2, \count($files)); } + + public function testNestedDeletePath() + { + $dir = DIRECTORY_SEPARATOR.'nested-delete-path-test'; + $dir2 = $dir.DIRECTORY_SEPARATOR.'dir2'; + $dir3 = $dir2.DIRECTORY_SEPARATOR.'dir3'; + + $this->assertTrue($this->object->createDirectory($dir)); + $this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World'); + $this->assertTrue($this->object->createDirectory($dir2)); + $this->object->write($dir2.DIRECTORY_SEPARATOR.'new-file-2.txt', 'Hello World'); + $this->assertTrue($this->object->createDirectory($dir3)); + $this->object->write($dir3.DIRECTORY_SEPARATOR.'new-file-3.txt', 'Hello World'); + + $this->assertTrue($this->object->deletePath($dir)); + $this->assertFalse($this->object->exists($dir)); + } } From 0b2ada49a204031148d25ce85889ccb7c7394e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 11:00:50 +0000 Subject: [PATCH 8/8] fix nested deletePath tests --- src/Storage/Device/Local.php | 2 +- tests/Storage/Device/LocalTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index a08fe60e..6ef23b63 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -358,7 +358,7 @@ public function deletePath(string $path): bool foreach ($files as $file) { if (is_dir($file)) { - $this->deletePath($file); + $this->deletePath(\ltrim($file, $this->getRoot().DIRECTORY_SEPARATOR)); } else { $this->delete($file, true); } diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index b341a82c..a2156b6a 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -342,7 +342,7 @@ public function testGetFiles() public function testNestedDeletePath() { - $dir = DIRECTORY_SEPARATOR.'nested-delete-path-test'; + $dir = $this->object->getPath('nested-delete-path-test'); $dir2 = $dir.DIRECTORY_SEPARATOR.'dir2'; $dir3 = $dir2.DIRECTORY_SEPARATOR.'dir3'; @@ -353,7 +353,7 @@ public function testNestedDeletePath() $this->assertTrue($this->object->createDirectory($dir3)); $this->object->write($dir3.DIRECTORY_SEPARATOR.'new-file-3.txt', 'Hello World'); - $this->assertTrue($this->object->deletePath($dir)); + $this->assertTrue($this->object->deletePath('nested-delete-path-test')); $this->assertFalse($this->object->exists($dir)); } }