diff --git a/.gitignore b/.gitignore index bc33091..f47ea8a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ composer.lock .idea index.php .phpunit.result.cache +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index 854d875..19304f5 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "overtrue/flysystem-cos", "description": "Flysystem adapter for the QCloud COS storage.", "require": { + "php": ">=8.0.2", "league/flysystem": "^3.0", "overtrue/qcloud-cos-client": "^1.0.0" }, diff --git a/src/CosAdapter.php b/src/CosAdapter.php index 2e59454..bdd1808 100644 --- a/src/CosAdapter.php +++ b/src/CosAdapter.php @@ -3,6 +3,7 @@ namespace Overtrue\Flysystem\Cos; use GuzzleHttp\Psr7\Uri; +use JetBrains\PhpStorm\Pure; use League\Flysystem\Config; use League\Flysystem\DirectoryAttributes; use League\Flysystem\FileAttributes; @@ -15,6 +16,7 @@ use League\Flysystem\UnableToRetrieveMetadata; use League\Flysystem\UnableToWriteFile; use League\Flysystem\Visibility; +use Overtrue\CosClient\Exceptions\ClientException; use Overtrue\CosClient\ObjectClient; use Overtrue\CosClient\BucketClient; @@ -26,6 +28,7 @@ class CosAdapter implements FilesystemAdapter protected array $config; + #[Pure] public function __construct(array $config) { $this->config = \array_merge( @@ -43,10 +46,11 @@ public function __construct(array $config) /** * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + * @throws \Throwable */ public function fileExists(string $path): bool { - return $this->getMetadata($path) !== null; + return $this->getMetadata($path) !== false; } /** @@ -74,6 +78,9 @@ public function write(string $path, string $contents, Config $config): void } } + /** + * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + */ public function writeStream(string $path, $contents, Config $config): void { $this->write($path, \stream_get_contents($contents), $config); @@ -192,7 +199,7 @@ public function visibility(string $path): FileAttributes $meta = $this->getObjectClient()->getObjectACL($prefixedPath); foreach ($meta['AccessControlPolicy']['AccessControlList']['Grant'] ?? [] as $grant) { - if ('READ' === $grant['Permission'] && false !== strpos($grant['Grantee']['URI'] ?? '', 'global/AllUsers')) { + if ('READ' === $grant['Permission'] && str_contains($grant['Grantee']['URI'] ?? '', 'global/AllUsers')) { return new FileAttributes($path, null, Visibility::PUBLIC); } } @@ -202,6 +209,7 @@ public function visibility(string $path): FileAttributes /** * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + * @throws \Throwable */ public function mimeType(string $path): FileAttributes { @@ -213,11 +221,14 @@ public function mimeType(string $path): FileAttributes } /** + * @return \League\Flysystem\FileAttributes * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + * @throws \Throwable */ public function lastModified(string $path): FileAttributes { $meta = $this->getMetadata($path); + if ($meta->lastModified() === null) { throw UnableToRetrieveMetadata::lastModified($path); } @@ -227,10 +238,12 @@ public function lastModified(string $path): FileAttributes /** * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + * @throws \Throwable */ public function fileSize(string $path): FileAttributes { $meta = $this->getMetadata($path); + if ($meta->fileSize() === null) { throw UnableToRetrieveMetadata::fileSize($path); } @@ -260,7 +273,6 @@ public function listContents(string $path, bool $deep): iterable } /** - * @throws \League\Flysystem\FilesystemException * @throws \Overtrue\CosClient\Exceptions\InvalidArgumentException * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException */ @@ -373,14 +385,23 @@ protected function getSourcePath(string $path): string /** * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException + * @throws \Throwable */ - protected function getMetadata($path): ?FileAttributes + protected function getMetadata($path): bool|FileAttributes { - $prefixedPath = $this->prefixer->prefixPath($path); + try { + $prefixedPath = $this->prefixer->prefixPath($path); + + $meta = $this->getObjectClient()->headObject($prefixedPath)->getHeaders(); + if (empty($meta)) { + return false; + } + } catch (\Throwable $e) { + if ($e instanceof ClientException && $e->getCode() === 404) { + return false; + } - $meta = $this->getObjectClient()->headObject($prefixedPath)->getHeaders(); - if (empty($meta)) { - return null; + throw $e; } return new FileAttributes(