Skip to content
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
26 changes: 22 additions & 4 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function deletePath(string $path): bool
}

/**
* Check if file exists
* Check if file or directory exists
*
* @param string $path
* @return bool
Expand All @@ -625,11 +625,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;
}
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Storage/Device/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions tests/Storage/S3Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ abstract protected function getAdapterName(): string;
*/
abstract protected function getAdapterDescription(): string;

/**
* @return string
*/
abstract protected function getAdapterType(): string;

/**
* @var S3
*/
Expand Down Expand Up @@ -139,6 +144,27 @@ public function testFileExists()
$this->assertEquals(false, $this->object->exists($this->object->getPath('testing/kitten-5.jpg')));
}

public function testDirectoryExists()
{
$this->assertEquals(true, $this->object->exists($this->object->getPath('testing/')));

$this->assertEquals(false, $this->object->exists($this->object->getPath('nonexistent/')));

$this->object->write($this->object->getPath('nested/deep/structure/test.txt'), 'Hello World', 'text/plain');

$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($this->object->getPath('nested/deep/structure/test.txt')));

$this->object->delete($this->object->getPath('nested/deep/structure/test.txt'));

$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()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World', 'text/plain'));
Expand Down