diff --git a/src/ObsAdapter.php b/src/ObsAdapter.php index 81777db..45e7bff 100644 --- a/src/ObsAdapter.php +++ b/src/ObsAdapter.php @@ -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; @@ -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 @@ -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, [ @@ -273,7 +303,7 @@ public function createDir($dirname, Config $config) { $defaultFile = trim($dirname, '/') . '/'; - return $this->write($defaultFile, null, $config); + return $this->write($defaultFile, '', $config); } /** @@ -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; } - } /** diff --git a/tests/MockAdapterTest.php b/tests/MockAdapterTest.php index 1d35644..23b14c6 100644 --- a/tests/MockAdapterTest.php +++ b/tests/MockAdapterTest.php @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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, [ @@ -154,7 +159,7 @@ public function testCreateDir(): void [ 'Bucket' => 'test', 'Key' => 'path/', - 'Body' => null, + 'Body' => '', ], ])->andReturn(new Model()); $this->obsAdapter->createDir('path', new Config()); @@ -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()); @@ -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([