Skip to content

Commit

Permalink
Fixed #24
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 13, 2022
1 parent 18b1daa commit 09bd9c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ composer.lock
.idea
index.php
.phpunit.result.cache
.php-cs-fixer.cache
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
37 changes: 29 additions & 8 deletions src/CosAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -26,6 +28,7 @@ class CosAdapter implements FilesystemAdapter

protected array $config;

#[Pure]
public function __construct(array $config)
{
$this->config = \array_merge(
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -202,6 +209,7 @@ public function visibility(string $path): FileAttributes

/**
* @throws \Overtrue\CosClient\Exceptions\InvalidConfigException
* @throws \Throwable
*/
public function mimeType(string $path): FileAttributes
{
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 09bd9c9

Please sign in to comment.