From 448e7842d54bb548cc6c60725861db76e672f5d8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 2 Oct 2025 15:09:09 +0530 Subject: [PATCH 1/2] fix: exists method to check for dirs as well --- src/Storage/Device/S3.php | 26 ++++++++++++++++++---- tests/Storage/Device/LocalTest.php | 30 +++++++++++++++++++++++++ tests/Storage/S3Base.php | 35 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index f24e6cf6..46240bba 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -555,7 +555,7 @@ public function deletePath(string $path): bool } /** - * Check if file exists + * Check if file or directory exists * * @param string $path * @return bool @@ -564,11 +564,29 @@ public function exists(string $path): bool { try { $this->getInfo($path); + + return true; } catch (\Throwable $th) { - return false; - } + try { + $root = $this->getRoot(); + if (str_starts_with($path, $root.'/') || str_starts_with($path, $root.'\\')) { + $prefix = $path; + } else { + $prefix = $root.'/'.ltrim($path, '/'); + } - return true; + if (! empty($path) && ! str_ends_with($prefix, '/')) { + $prefix .= '/'; + } + + $objects = $this->listObjects($prefix, 1); + $count = (int) ($objects['KeyCount'] ?? 0); + + return $count > 0; + } catch (\Throwable $th2) { + return false; + } + } } /** diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index 30a721fc..723a7943 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -84,6 +84,36 @@ public function testFileExists() $this->object->delete($this->object->getPath('text-for-test-exists.txt')); } + public function testDirectoryExists() + { + $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory'))); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory/'))); + + $testDir = $this->object->getPath('test-directory-exists'); + $this->assertTrue($this->object->createDirectory($testDir)); + $this->assertEquals(true, $this->object->exists($testDir)); + $this->assertEquals(true, $this->object->exists($testDir.'/')); + + $nestedDir = $testDir.'/nested/deep/structure'; + $this->assertTrue($this->object->createDirectory($nestedDir)); + $this->object->write($nestedDir.'/test.txt', 'Hello World'); + + $this->assertEquals(true, $this->object->exists($testDir.'/nested')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep/')); + $this->assertEquals(true, $this->object->exists($nestedDir)); + $this->assertEquals(true, $this->object->exists($nestedDir.'/')); + + $this->assertEquals(true, $this->object->exists($nestedDir.'/test.txt')); + + $this->object->delete($testDir, true); + + $this->assertEquals(false, $this->object->exists($testDir)); + $this->assertEquals(false, $this->object->exists($testDir.'/nested')); + $this->assertEquals(false, $this->object->exists($nestedDir)); + } + public function testMove() { $this->assertEquals($this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World'), true); diff --git a/tests/Storage/S3Base.php b/tests/Storage/S3Base.php index 314cfe02..85cd1893 100644 --- a/tests/Storage/S3Base.php +++ b/tests/Storage/S3Base.php @@ -20,6 +20,11 @@ abstract protected function getAdapterName(): string; */ abstract protected function getAdapterDescription(): string; + /** + * @return string + */ + abstract protected function getAdapterType(): string; + /** * @var S3 */ @@ -139,6 +144,36 @@ public function testFileExists() $this->assertEquals(false, $this->object->exists($this->object->getPath('testing/kitten-5.jpg'))); } + public function testDirectoryExists() + { + $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory'))); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory/'))); + + $testDir = $this->object->getPath('test-directory-exists'); + $this->assertTrue($this->object->createDirectory($testDir)); + $this->assertEquals(true, $this->object->exists($testDir)); + $this->assertEquals(true, $this->object->exists($testDir.'/')); + + $nestedDir = $testDir.'/nested/deep/structure'; + $this->assertTrue($this->object->createDirectory($nestedDir)); + $this->object->write($nestedDir.'/test.txt', 'Hello World'); + + $this->assertEquals(true, $this->object->exists($testDir.'/nested')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep')); + $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep/')); + $this->assertEquals(true, $this->object->exists($nestedDir)); + $this->assertEquals(true, $this->object->exists($nestedDir.'/')); + + $this->assertEquals(true, $this->object->exists($nestedDir.'/test.txt')); + + $this->object->delete($testDir, true); + + $this->assertEquals(false, $this->object->exists($testDir)); + $this->assertEquals(false, $this->object->exists($testDir.'/nested')); + $this->assertEquals(false, $this->object->exists($nestedDir)); + } + public function testMove() { $this->assertEquals(true, $this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World', 'text/plain')); From d3019b7ab50d3ed928b8d9bcc821d83e43c2e7a4 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 2 Oct 2025 15:17:25 +0530 Subject: [PATCH 2/2] fix: tests --- tests/Storage/S3Base.php | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/Storage/S3Base.php b/tests/Storage/S3Base.php index 85cd1893..338537a3 100644 --- a/tests/Storage/S3Base.php +++ b/tests/Storage/S3Base.php @@ -146,32 +146,23 @@ public function testFileExists() public function testDirectoryExists() { - $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory'))); - $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent-directory/'))); + $this->assertEquals(true, $this->object->exists($this->object->getPath('testing/'))); - $testDir = $this->object->getPath('test-directory-exists'); - $this->assertTrue($this->object->createDirectory($testDir)); - $this->assertEquals(true, $this->object->exists($testDir)); - $this->assertEquals(true, $this->object->exists($testDir.'/')); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent/'))); - $nestedDir = $testDir.'/nested/deep/structure'; - $this->assertTrue($this->object->createDirectory($nestedDir)); - $this->object->write($nestedDir.'/test.txt', 'Hello World'); + $this->object->write($this->object->getPath('nested/deep/structure/test.txt'), 'Hello World', 'text/plain'); - $this->assertEquals(true, $this->object->exists($testDir.'/nested')); - $this->assertEquals(true, $this->object->exists($testDir.'/nested/')); - $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep')); - $this->assertEquals(true, $this->object->exists($testDir.'/nested/deep/')); - $this->assertEquals(true, $this->object->exists($nestedDir)); - $this->assertEquals(true, $this->object->exists($nestedDir.'/')); + $this->assertEquals(true, $this->object->exists($this->object->getPath('nested'))); + $this->assertEquals(true, $this->object->exists($this->object->getPath('nested/deep'))); + $this->assertEquals(true, $this->object->exists($this->object->getPath('nested/deep/structure'))); - $this->assertEquals(true, $this->object->exists($nestedDir.'/test.txt')); + $this->assertEquals(true, $this->object->exists($this->object->getPath('nested/deep/structure/test.txt'))); - $this->object->delete($testDir, true); + $this->object->delete($this->object->getPath('nested/deep/structure/test.txt')); - $this->assertEquals(false, $this->object->exists($testDir)); - $this->assertEquals(false, $this->object->exists($testDir.'/nested')); - $this->assertEquals(false, $this->object->exists($nestedDir)); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nested'))); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nested/deep'))); + $this->assertEquals(false, $this->object->exists($this->object->getPath('nested/deep/structure'))); } public function testMove()