Skip to content

Commit

Permalink
Allow copy without retaining visibility for adapters and implementati…
Browse files Browse the repository at this point in the history
…ons that required fetching visibility for copy and move.
  • Loading branch information
zingimmick committed Nov 22, 2023
1 parent bd1aead commit f6a7c00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
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

0 comments on commit f6a7c00

Please sign in to comment.