From cb156dbfc221c794dc41e15f30a56f18500887f8 Mon Sep 17 00:00:00 2001 From: Zing Date: Wed, 22 Nov 2023 22:57:42 +0800 Subject: [PATCH] Allow copy without retaining visibility for adapters and implementations that required fetching visibility for copy and move. (#118) --- src/ObsAdapter.php | 12 ++++++++---- tests/MockAdapterTest.php | 22 ++++++++++++++++++++++ tests/ValidAdapterTest.php | 9 +++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/ObsAdapter.php b/src/ObsAdapter.php index c0629e3..77e7d95 100644 --- a/src/ObsAdapter.php +++ b/src/ObsAdapter.php @@ -29,6 +29,7 @@ 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 Obs\ObsClient; @@ -176,9 +177,12 @@ public function move(string $source, string $destination, Config $config): void public function copy(string $source, string $destination, Config $config): void { try { - /** @var string $visibility */ - $visibility = $config->get(Config::OPTION_VISIBILITY) ?: $this->visibility($source) - ->visibility(); + /** @var string|null $visibility */ + $visibility = $config->get(Config::OPTION_VISIBILITY); + if ($visibility === null && $config->get('retain_visibility', true)) { + $visibility = $this->visibility($source) + ->visibility(); + } } catch (FilesystemOperationFailed $filesystemOperationFailed) { throw UnableToCopyFile::fromLocationTo($source, $destination, $filesystemOperationFailed); } @@ -189,7 +193,7 @@ public function copy(string $source, string $destination, Config $config): void 'Key' => $this->pathPrefixer->prefixPath($destination), 'CopySource' => $this->bucket . '/' . $this->pathPrefixer->prefixPath($source), 'MetadataDirective' => ObsClient::CopyMetadata, - 'ACL' => $this->visibilityConverter->visibilityToAcl($visibility), + 'ACL' => $this->visibilityConverter->visibilityToAcl($visibility ?: Visibility::PRIVATE), ])); } catch (ObsException $obsException) { throw UnableToCopyFile::fromLocationTo($source, $destination, $obsException); diff --git a/tests/MockAdapterTest.php b/tests/MockAdapterTest.php index 51c7b88..16e78da 100644 --- a/tests/MockAdapterTest.php +++ b/tests/MockAdapterTest.php @@ -79,6 +79,28 @@ public function testCopy(): void $this->assertSame('write', $this->obsAdapter->read('copy.txt')); } + public function testCopyWithoutRetainVisibility(): void + { + $this->mockPutObject('file.txt', 'write'); + $this->obsAdapter->write('file.txt', 'write', new Config()); + $this->legacyMock->shouldReceive('copyObject') + ->withArgs([ + [ + 'Bucket' => 'test', + 'Key' => 'copy.txt', + 'CopySource' => 'test/file.txt', + 'MetadataDirective' => 'COPY', + 'ACL' => 'private', + ], + ])->andReturn(new Model()); + $this->mockGetVisibility('file.txt', Visibility::PUBLIC); + $this->obsAdapter->copy('file.txt', 'copy.txt', new Config([ + 'retain_visibility' => false, + ])); + $this->mockGetVisibility('copy.txt', Visibility::PRIVATE); + $this->assertSame(Visibility::PRIVATE, $this->obsAdapter->visibility('copy.txt')->visibility()); + } + public function testCopyFailed(): void { $this->mockPutObject('file.txt', 'write'); diff --git a/tests/ValidAdapterTest.php b/tests/ValidAdapterTest.php index 71a2d68..ae5f01a 100644 --- a/tests/ValidAdapterTest.php +++ b/tests/ValidAdapterTest.php @@ -74,6 +74,15 @@ public function testCopy(): void $this->assertSame('write', $this->obsAdapter->read('fixture/copy.txt')); } + public function testCopyWithoutRetainVisibility(): void + { + $this->obsAdapter->write('fixture/file.txt', 'write', new Config()); + $this->obsAdapter->copy('fixture/file.txt', 'fixture/copy.txt', new Config([ + 'retain_visibility' => false, + ])); + $this->assertSame(Visibility::PRIVATE, $this->obsAdapter->visibility('fixture/copy.txt')->visibility()); + } + public function testCreateDir(): void { $this->obsAdapter->createDirectory('fixture/path', new Config());