Skip to content

Commit

Permalink
Merge pull request #1547 from fnash/avoid-get-object-acl-in-copy
Browse files Browse the repository at this point in the history
Use visibility argument when ::copy() in AWS adapters
  • Loading branch information
frankdejonge committed Sep 17, 2022
2 parents 26c6ebd + fcfa49d commit bc7bac3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/AsyncAwsS3/AsyncAwsS3Adapter.php
Expand Up @@ -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);
}
Expand Down
49 changes: 49 additions & 0 deletions src/AsyncAwsS3/AsyncAwsS3AdapterTest.php
Expand Up @@ -23,6 +23,7 @@
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\Visibility;

/**
* @group aws
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/AwsS3V3/AwsS3V3Adapter.php
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions src/AwsS3V3/AwsS3V3AdapterTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit bc7bac3

Please sign in to comment.