Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow copy without retaining visibility for adapters and implementations that required fetching visibility for copy and move. #118

Merged
merged 1 commit into from
Nov 22, 2023
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
12 changes: 8 additions & 4 deletions src/ObsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/MockAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions tests/ValidAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading