Skip to content
Open
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
59 changes: 8 additions & 51 deletions src/AsyncAwsS3/AsyncAwsS3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
use League\Flysystem\Visibility;
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use League\MimeTypeDetection\MimeTypeDetector;
use Throwable;
Expand All @@ -53,7 +52,6 @@ class AsyncAwsS3Adapter implements FilesystemAdapter, PublicUrlGenerator, Checks
* @var string[]
*/
public const AVAILABLE_OPTIONS = [
'ACL',
'CacheControl',
'ContentDisposition',
'ContentEncoding',
Expand Down Expand Up @@ -92,8 +90,10 @@ class AsyncAwsS3Adapter implements FilesystemAdapter, PublicUrlGenerator, Checks
'VersionId',
];

const ON_VISIBILITY_THROW_ERROR = 'throw';
const ON_VISIBILITY_IGNORE = 'ignore';

private PathPrefixer $prefixer;
private VisibilityConverter $visibility;
private MimeTypeDetector $mimeTypeDetector;

/**
Expand All @@ -113,13 +113,12 @@ public function __construct(
private S3Client $client,
private string $bucket,
string $prefix = '',
?VisibilityConverter $visibility = null,
?MimeTypeDetector $mimeTypeDetector = null,
private string $visibilityHandling = self::ON_VISIBILITY_THROW_ERROR,
array $forwardedOptions = self::AVAILABLE_OPTIONS,
array $metadataFields = self::EXTRA_METADATA_FIELDS,
) {
$this->prefixer = new PathPrefixer($prefix);
$this->visibility = $visibility ?? new PortableVisibilityConverter();
$this->mimeTypeDetector = $mimeTypeDetector ?? new FinfoMimeTypeDetector();
$this->forwardedOptions = $forwardedOptions;
$this->metadataFields = $metadataFields;
Expand Down Expand Up @@ -209,9 +208,6 @@ public function deleteDirectory(string $path): void

public function createDirectory(string $path, Config $config): void
{
$defaultVisibility = $config->get(Config::OPTION_DIRECTORY_VISIBILITY, $this->visibility->defaultForDirectories());
$config = $config->withDefaults([Config::OPTION_VISIBILITY => $defaultVisibility]);

try {
$this->upload(rtrim($path, '/') . '/', '', $config);
} catch (Throwable $e) {
Expand All @@ -221,33 +217,14 @@ public function createDirectory(string $path, Config $config): void

public function setVisibility(string $path, string $visibility): void
{
$arguments = [
'Bucket' => $this->bucket,
'Key' => $this->prefixer->prefixPath($path),
'ACL' => $this->visibility->visibilityToAcl($visibility),
];

try {
$this->client->putObjectAcl($arguments);
} catch (Throwable $exception) {
throw UnableToSetVisibility::atLocation($path, $exception->getMessage(), $exception);
if ($this->visibilityHandling === self::ON_VISIBILITY_THROW_ERROR) {
throw UnableToSetVisibility::atLocation($path, 'AWS S3 does not support this operation.');
}
}

public function visibility(string $path): FileAttributes
{
$arguments = ['Bucket' => $this->bucket, 'Key' => $this->prefixer->prefixPath($path)];

try {
$result = $this->client->getObjectAcl($arguments);
$grants = $result->getGrants();
} catch (Throwable $exception) {
throw UnableToRetrieveMetadata::visibility($path, $exception->getMessage(), $exception);
}

$visibility = $this->visibility->aclToVisibility($grants);

return new FileAttributes($path, null, $visibility);
throw UnableToRetrieveMetadata::visibility($path, 'AWS S3 does not support visibility');
}

public function mimeType(string $path): FileAttributes
Expand Down Expand Up @@ -343,18 +320,7 @@ public function copy(string $source, string $destination, Config $config): void
return;
}

try {
$visibility = $config->get(Config::OPTION_VISIBILITY);

if ($visibility === null && $config->get(Config::OPTION_RETAIN_VISIBILITY, true)) {
$visibility = $this->visibility($source)->visibility();
}
} catch (Throwable $exception) {
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
}

$arguments = [
'ACL' => $this->visibility->visibilityToAcl($visibility ?: 'private'),
'Bucket' => $this->bucket,
'Key' => $this->prefixer->prefixPath($destination),
'CopySource' => rawurlencode($this->bucket . '/' . $this->prefixer->prefixPath($source)),
Expand All @@ -373,7 +339,6 @@ public function copy(string $source, string $destination, Config $config): void
private function upload(string $path, $body, Config $config): void
{
$key = $this->prefixer->prefixPath($path);
$acl = $this->determineAcl($config);
$options = $this->createOptionsFromConfig($config);
$shouldDetermineMimetype = '' !== $body && ! \array_key_exists('ContentType', $options);

Expand All @@ -384,27 +349,19 @@ private function upload(string $path, $body, Config $config): void
try {
if ($this->client instanceof SimpleS3Client) {
// Supports upload of files larger than 5GB
$this->client->upload($this->bucket, $key, $body, array_merge($options, ['ACL' => $acl]));
$this->client->upload($this->bucket, $key, $body, $options);
} else {
$this->client->putObject(array_merge($options, [
'Bucket' => $this->bucket,
'Key' => $key,
'Body' => $body,
'ACL' => $acl,
]));
}
} catch (Throwable $exception) {
throw UnableToWriteFile::atLocation($path, $exception->getMessage(), $exception);
}
}

private function determineAcl(Config $config): string
{
$visibility = (string) $config->get(Config::OPTION_VISIBILITY, Visibility::PRIVATE);

return $this->visibility->visibilityToAcl($visibility);
}

private function createOptionsFromConfig(Config $config): array
{
$options = [];
Expand Down
15 changes: 6 additions & 9 deletions src/AsyncAwsS3/AsyncAwsS3AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use function getenv;
use function iterator_to_array;

Expand Down Expand Up @@ -398,16 +397,16 @@ public function failing_to_write_a_file(): void
/**
* @test
*/
public function moving_a_file_with_visibility(): void
public function moving_a_file(): void
{
$this->runScenario(function () {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
'contents to be copied',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
new Config()
);
$adapter->move('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE]));
$adapter->move('source.txt', 'destination.txt', new Config());
$this->assertFalse(
$adapter->fileExists('source.txt'),
'After moving a file should no longer exist in the original location.'
Expand All @@ -416,29 +415,27 @@ public function moving_a_file_with_visibility(): void
$adapter->fileExists('destination.txt'),
'After moving, a file should be present at the new location.'
);
$this->assertEquals(Visibility::PRIVATE, $adapter->visibility('destination.txt')->visibility());
$this->assertEquals('contents to be copied', $adapter->read('destination.txt'));
});
}

/**
* @test
*/
public function copying_a_file_with_visibility(): void
public function copying_a_file(): void
{
$this->runScenario(function () {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
'contents to be copied',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
new Config()
);

$adapter->copy('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE]));
$adapter->copy('source.txt', 'destination.txt', new Config());

$this->assertTrue($adapter->fileExists('source.txt'));
$this->assertTrue($adapter->fileExists('destination.txt'));
$this->assertEquals(Visibility::PRIVATE, $adapter->visibility('destination.txt')->visibility());
$this->assertEquals('contents to be copied', $adapter->read('destination.txt'));
});
}
Expand Down
60 changes: 0 additions & 60 deletions src/AsyncAwsS3/PortableVisibilityConverter.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/AsyncAwsS3/VisibilityConverter.php

This file was deleted.