Skip to content

Commit

Permalink
Fixed #16
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Mar 4, 2021
1 parent 6263051 commit efa7578
Showing 1 changed file with 63 additions and 32 deletions.
95 changes: 63 additions & 32 deletions src/CosAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ class CosAdapter extends AbstractAdapter implements CanOverwriteFiles
*/
public function __construct(array $config)
{
$this->config = \array_merge([
'bucket' => null,
'app_id' => null,
'region' => 'ap-guangzhou',
'signed_url' => false,
], $config);
$this->config = \array_merge(
[
'bucket' => null,
'app_id' => null,
'region' => 'ap-guangzhou',
'signed_url' => false,
],
$config
);

if (!empty($config['prefix'])) {
$this->setPathPrefix($config['prefix']);
Expand Down Expand Up @@ -156,9 +159,12 @@ public function copy($path, $newpath)
$destination = $this->applyPathPrefix($newpath);

try {
return $this->getObjectClient()->copyObject($destination, [
'x-cos-copy-source' => $location,
])->isSuccessful();
return $this->getObjectClient()->copyObject(
$destination,
[
'x-cos-copy-source' => $location,
]
)->isSuccessful();
} catch (\Exception $e) {
return false;
}
Expand All @@ -181,6 +187,17 @@ public function listContents($directory = '', $recursive = false)

$response = $this->listObjects($this->applyPathPrefix($directory), $recursive);

// 处理目录
foreach ($response['CommonPrefixes'] ?? [] as $prefix) {
$list[] = $this->normalizeFileInfo(
[
'Key' => $prefix['Prefix'],
'Size' => 0,
'LastModified' => 0,
]
);
}

foreach ($response['Contents'] ?? [] as $content) {
$list[] = $this->normalizeFileInfo($content);
}
Expand Down Expand Up @@ -252,9 +269,13 @@ public function getVisibility($path)
*/
public function setVisibility($path, $visibility)
{
return (bool) $this->getObjectClient()->putObjectACL($this->applyPathPrefix($path), [], [
'x-cos-acl' => $this->normalizeVisibility($visibility),
]);
return (bool)$this->getObjectClient()->putObjectACL(
$this->applyPathPrefix($path),
[],
[
'x-cos-acl' => $this->normalizeVisibility($visibility),
]
);
}

/**
Expand Down Expand Up @@ -287,16 +308,21 @@ public function deleteDir($dirname)
return true;
}

$keys = array_map(function ($item) {
return ['Key' => $item['Key']];
}, $response['Contents']);
$keys = array_map(
function ($item) {
return ['Key' => $item['Key']];
},
$response['Contents']
);

return $this->getObjectClient()->deleteObjects([
'Delete' => [
'Quiet' => 'false',
'Object' => $keys,
],
])->isSuccessful();
return $this->getObjectClient()->deleteObjects(
[
'Delete' => [
'Quiet' => 'false',
'Object' => $keys,
],
]
)->isSuccessful();
}

public function getUrl($path)
Expand All @@ -311,7 +337,7 @@ public function getUrl($path)
}

/**
* @param string $path
* @param string $path
* @param string $expires
*
* @return string
Expand Down Expand Up @@ -394,21 +420,26 @@ protected function normalizeFileInfo(array $content)

/**
* @param string $directory
* @param bool $recursive
* @param bool $recursive
*
* @return mixed
*/
protected function listObjects($directory = '', $recursive = false)
{
$result = $this->getBucketClient()->getObjects([
'prefix' => ('' === (string) $directory) ? '' : ($directory.'/'),
'delimiter' => $recursive ? '' : '/',
])['ListBucketResult'];

$result['Contents'] = $result['Contents'] ?? [];

if (($key = \key($result['Contents'])) !== 0) {
$result['Contents'] = \is_null($key) ? [] : [$result['Contents']];
$result = $this->getBucketClient()->getObjects(
[
'prefix' => ('' === (string)$directory) ? '' : ($directory.'/'),
'delimiter' => $recursive ? '' : '/',
]
)['ListBucketResult'];

foreach (['CommonPrefixes', 'Contents'] as $key) {
$result[$key] = $result[$key] ?? [];

// 确保是二维数组
if (($index = \key($result[$key])) !== 0) {
$result[$key] = \is_null($index) ? [] : [$result[$key]];
}
}

return $result;
Expand Down

0 comments on commit efa7578

Please sign in to comment.