Skip to content

Commit

Permalink
Merge pull request thephpleague#1437 from thephpleague/bugfix/aws-s3-…
Browse files Browse the repository at this point in the history
…v3-291

[BUGFIX] Ensure top-level directory is not returned in directory listing.
  • Loading branch information
frankdejonge authored Apr 2, 2022
2 parents fd92eb6 + 5510363 commit afce361
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/AwsS3V3/AwsS3V3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,8 @@ private function fetchFileMetadata(string $path, string $type): FileAttributes
return $attributes;
}

private function mapS3ObjectMetadata(array $metadata, string $path = null): StorageAttributes
private function mapS3ObjectMetadata(array $metadata, string $path): StorageAttributes
{
if ($path === null) {
$path = $this->prefixer->stripPrefix($metadata['Key'] ?? $metadata['Prefix']);
}

if (substr($path, -1) === '/') {
return new DirectoryAttributes(rtrim($path, '/'));
}
Expand Down Expand Up @@ -409,7 +405,13 @@ public function listContents(string $path, bool $deep): iterable
$listing = $this->retrievePaginatedListing($options);

foreach ($listing as $item) {
yield $this->mapS3ObjectMetadata($item);
$key = $item['Key'] ?? $item['Prefix'];

if ($key === $prefix) {
continue;
}

yield $this->mapS3ObjectMetadata($item, $this->prefixer->stripPrefix($key));
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/AwsS3V3/AwsS3V3AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use RuntimeException;

use function getenv;
use function iterator_to_array;

/**
* @group aws
Expand Down Expand Up @@ -118,6 +119,19 @@ public function writing_a_file_with_explicit_mime_type(): void
$this->assertEquals('text/plain+special', $mimeType);
}

/**
* @test
* @see https://github.com/thephpleague/flysystem-aws-s3-v3/issues/291
*/
public function issue_291(): void
{
$adapter = $this->adapter();
$adapter->createDirectory('directory', new Config());
$listing = iterator_to_array($adapter->listContents('directory', true));

self::assertCount(0, $listing);
}

/**
* @test
*/
Expand Down

0 comments on commit afce361

Please sign in to comment.