Skip to content

Commit

Permalink
Add more options (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingimmick authored Mar 14, 2022
1 parent 1c5bdec commit 434b0cf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
36 changes: 33 additions & 3 deletions src/ObsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
use League\Flysystem\Util;
use Obs\ObsClient;
use Obs\ObsException;

Expand All @@ -31,7 +32,20 @@ class ObsAdapter extends AbstractAdapter
/**
* @var string[]
*/
protected static $metaOptions = ['ACL', 'Expires', 'StorageClass'];
protected static $metaOptions = [
'ACL',
'StorageClass',
'ContentType',
'ContentLength',
'Metadata',
'WebsiteRedirectLocation',
'SseKms',
'SseKmsKey',
'SseC',
'SseCKey',
'Expires',
'SuccessRedirect',
];

/**
* @var string
Expand Down Expand Up @@ -109,6 +123,22 @@ public function write($path, $contents, Config $config)
$path = $this->applyPathPrefix($path);

$options = $this->getOptionsFromConfig($config);
if (! isset($options['ACL'])) {
/** @var string|null $visibility */
$visibility = $config->get('visibility');
if ($visibility !== null) {
$options['ACL'] = $options['ACL'] ?? ($visibility === self::VISIBILITY_PUBLIC ? ObsClient::AclPublicRead : ObsClient::AclPrivate);
}
}

$shouldDetermineMimetype = $contents !== '' && ! \array_key_exists('ContentType', $options);

if ($shouldDetermineMimetype) {
$mimeType = Util::guessMimeType($path, $contents);
if ($mimeType) {
$options['ContentType'] = $mimeType;
}
}

try {
$this->client->putObject(array_merge($options, [
Expand Down Expand Up @@ -273,7 +303,7 @@ public function createDir($dirname, Config $config)
{
$defaultFile = trim($dirname, '/') . '/';

return $this->write($defaultFile, null, $config);
return $this->write($defaultFile, '', $config);
}

/**
Expand Down Expand Up @@ -724,11 +754,11 @@ public function signUrl($path, $expiration, array $options = [], $method = 'GET'
'Expires' => $expires,
'QueryParams' => $options,
]);

return $model['SignedUrl'];
} catch (ObsException $obsException) {
return false;
}

}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/MockAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function setUp(): void
'Bucket' => 'test',
'Key' => 'fixture/read.txt',
'Body' => 'read-test',
'ContentType' => 'text/plain',
],
])->andReturn(new Model([]));
$this->obsAdapter->write('fixture/read.txt', 'read-test', new Config());
Expand All @@ -49,6 +50,7 @@ public function testUpdate(): void
'Bucket' => 'test',
'Key' => 'file.txt',
'Body' => 'update',
'ContentType' => 'text/plain',
],
])->andReturn(new Model());
$this->obsAdapter->update('file.txt', 'update', new Config());
Expand All @@ -72,6 +74,7 @@ public function testUpdateStream(): void
'Bucket' => 'test',
'Key' => 'file.txt',
'Body' => 'write',
'ContentType' => 'text/plain',
],
])->andReturn(new Model());
$this->obsAdapter->write('file.txt', 'write', new Config());
Expand All @@ -81,6 +84,7 @@ public function testUpdateStream(): void
'Bucket' => 'test',
'Key' => 'file.txt',
'Body' => 'update',
'ContentType' => 'text/plain',
],
])->andReturn(new Model());
$this->obsAdapter->updateStream('file.txt', $this->streamFor('update')->detach(), new Config());
Expand All @@ -102,6 +106,7 @@ private function mockPutObject($path, $body, $visibility = null): void
'Bucket' => 'test',
'Key' => $path,
'Body' => $body,
'ContentType' => 'text/plain',
];
if ($visibility !== null) {
$arg = array_merge($arg, [
Expand Down Expand Up @@ -154,7 +159,7 @@ public function testCreateDir(): void
[
'Bucket' => 'test',
'Key' => 'path/',
'Body' => null,
'Body' => '',
],
])->andReturn(new Model());
$this->obsAdapter->createDir('path', new Config());
Expand Down Expand Up @@ -228,6 +233,7 @@ public function testSetVisibility(): void
'Bucket' => 'test',
'Key' => 'file.txt',
'Body' => 'write',
'ContentType' => 'text/plain',
],
])->andReturn(new Model());
$this->obsAdapter->write('file.txt', 'write', new Config());
Expand Down Expand Up @@ -545,6 +551,7 @@ public function testWriteStreamWithExpires(): void
'Bucket' => 'test',
'Key' => 'file.txt',
'Body' => 'write',
'ContentType' => 'text/plain',
],
])->andReturn(new Model());
$this->obsAdapter->writeStream('file.txt', $this->streamFor('write')->detach(), new Config([
Expand Down

0 comments on commit 434b0cf

Please sign in to comment.