From fcfa49d9a6bf17e5605a1bb6203ba6d88f8118c8 Mon Sep 17 00:00:00 2001 From: Fayez Naccache Date: Wed, 7 Sep 2022 15:26:00 +0200 Subject: [PATCH] Use visibility argument when ::copy() in AWS adapters. (avoids GetObjectAcl operation and permission) --- src/AsyncAwsS3/AsyncAwsS3Adapter.php | 2 +- src/AsyncAwsS3/AsyncAwsS3AdapterTest.php | 49 ++++++++++++++++++++++++ src/AwsS3V3/AwsS3V3Adapter.php | 2 +- src/AwsS3V3/AwsS3V3AdapterTest.php | 49 ++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/AsyncAwsS3/AsyncAwsS3Adapter.php b/src/AsyncAwsS3/AsyncAwsS3Adapter.php index c9a293b92..e5e1734fe 100644 --- a/src/AsyncAwsS3/AsyncAwsS3Adapter.php +++ b/src/AsyncAwsS3/AsyncAwsS3Adapter.php @@ -285,7 +285,7 @@ public function copy(string $source, string $destination, Config $config): void { try { /** @var string $visibility */ - $visibility = $this->visibility($source)->visibility(); + $visibility = $config->get(Config::OPTION_VISIBILITY) ?: $this->visibility($source)->visibility(); } catch (Throwable $exception) { throw UnableToCopyFile::fromLocationTo($source, $destination, $exception); } diff --git a/src/AsyncAwsS3/AsyncAwsS3AdapterTest.php b/src/AsyncAwsS3/AsyncAwsS3AdapterTest.php index 72171c730..f227f73f9 100644 --- a/src/AsyncAwsS3/AsyncAwsS3AdapterTest.php +++ b/src/AsyncAwsS3/AsyncAwsS3AdapterTest.php @@ -23,6 +23,7 @@ use League\Flysystem\UnableToDeleteFile; use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToRetrieveMetadata; +use League\Flysystem\Visibility; /** * @group aws @@ -275,6 +276,54 @@ public function write_with_simple_s3_client(): void $filesystem->write($file, $contents, new Config()); } + /** + * @test + */ + public function moving_a_file_with_visibility(): void + { + $this->runScenario(function () { + $adapter = $this->adapter(); + $adapter->write( + 'source.txt', + 'contents to be copied', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + $adapter->move('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE])); + $this->assertFalse( + $adapter->fileExists('source.txt'), + 'After moving a file should no longer exist in the original location.' + ); + $this->assertTrue( + $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 + { + $this->runScenario(function () { + $adapter = $this->adapter(); + $adapter->write( + 'source.txt', + 'contents to be copied', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + + $adapter->copy('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE])); + + $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')); + }); + } + protected static function createFilesystemAdapter(): FilesystemAdapter { static::$stubS3Client = new S3ClientStub(static::s3Client()); diff --git a/src/AwsS3V3/AwsS3V3Adapter.php b/src/AwsS3V3/AwsS3V3Adapter.php index 395977eaf..2cc4e9674 100644 --- a/src/AwsS3V3/AwsS3V3Adapter.php +++ b/src/AwsS3V3/AwsS3V3Adapter.php @@ -394,7 +394,7 @@ public function copy(string $source, string $destination, Config $config): void { try { /** @var string $visibility */ - $visibility = $this->visibility($source)->visibility(); + $visibility = $config->get(Config::OPTION_VISIBILITY) ?: $this->visibility($source)->visibility(); } catch (Throwable $exception) { throw UnableToCopyFile::fromLocationTo( $source, diff --git a/src/AwsS3V3/AwsS3V3AdapterTest.php b/src/AwsS3V3/AwsS3V3AdapterTest.php index 48134cfde..9bf9999c2 100644 --- a/src/AwsS3V3/AwsS3V3AdapterTest.php +++ b/src/AwsS3V3/AwsS3V3AdapterTest.php @@ -20,6 +20,7 @@ use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToRetrieveMetadata; use League\Flysystem\UnableToWriteFile; +use League\Flysystem\Visibility; use RuntimeException; use function getenv; @@ -308,6 +309,54 @@ public function setting_acl_via_options(): void self::assertEquals('FULL_CONTROL', $permission); } + /** + * @test + */ + public function moving_a_file_with_visibility(): void + { + $this->runScenario(function () { + $adapter = $this->adapter(); + $adapter->write( + 'source.txt', + 'contents to be copied', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + $adapter->move('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE])); + $this->assertFalse( + $adapter->fileExists('source.txt'), + 'After moving a file should no longer exist in the original location.' + ); + $this->assertTrue( + $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 + { + $this->runScenario(function () { + $adapter = $this->adapter(); + $adapter->write( + 'source.txt', + 'contents to be copied', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + + $adapter->copy('source.txt', 'destination.txt', new Config([Config::OPTION_VISIBILITY => Visibility::PRIVATE])); + + $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')); + }); + } + protected static function createFilesystemAdapter(bool $streaming = true, array $options = []): FilesystemAdapter { static::$stubS3Client = new S3ClientStub(static::s3Client());