diff --git a/src/OssAdapter.php b/src/OssAdapter.php index 9512ab02bc394116f497abbbb6f6fd5b2b0c1ab7..b75cee0cafcf37fd270a5713a8a7332760256e60 100644 --- a/src/OssAdapter.php +++ b/src/OssAdapter.php @@ -44,14 +44,14 @@ class OssAdapter implements FilesystemAdapter // 系统参数 public const SYSTEM_FIELD = [ - 'bucket' => '${bucket}', - 'etag' => '${etag}', + 'bucket' => '${bucket}', + 'etag' => '${etag}', 'filename' => '${object}', - 'size' => '${size}', + 'size' => '${size}', 'mimeType' => '${mimeType}', - 'height' => '${imageInfo.height}', - 'width' => '${imageInfo.width}', - 'format' => '${imageInfo.format}', + 'height' => '${imageInfo.height}', + 'width' => '${imageInfo.width}', + 'format' => '${imageInfo.format}', ]; protected $accessKeyId; @@ -89,9 +89,6 @@ class OssAdapter implements FilesystemAdapter */ protected $useSSL = false; - /** - * @var string|null - */ protected ?string $cdnUrl = null; /** @@ -119,8 +116,6 @@ class OssAdapter implements FilesystemAdapter /** * init default bucket adapter. - * - * @return \Iidestiny\Flysystem\Oss\OssAdapter */ protected function initDefaultBucketAdapter(): OssAdapter { @@ -134,7 +129,6 @@ class OssAdapter implements FilesystemAdapter /** * set cdn url. * - * @param string|null $url * @return $this */ public function setCdnUrl(?string $url): OssAdapter @@ -153,6 +147,7 @@ class OssAdapter implements FilesystemAdapter * get bucket adapter by bucket name. * * @return $this + * * @throws \InvalidArgumentException */ public function bucket($bucket): OssAdapter @@ -163,8 +158,6 @@ class OssAdapter implements FilesystemAdapter /** * create bucket adapter by bucket name. * - * @param string $bucket - * @return \Iidestiny\Flysystem\Oss\OssAdapter * @throws \InvalidArgumentException */ protected function createBucketAdapter(string $bucket): OssAdapter @@ -192,9 +185,6 @@ class OssAdapter implements FilesystemAdapter /** * extract extra config. - * - * @param array $config - * @return array */ protected function extraConfig(array $config): array { @@ -213,7 +203,7 @@ class OssAdapter implements FilesystemAdapter $this->client = new OssClient([ 'endpoint' => rtrim($this->endpoint, '/'), - 'cname' => $this->isCName, + 'cname' => $this->isCName, 'provider' => $provider, ...$this->params, ]); @@ -230,14 +220,14 @@ class OssAdapter implements FilesystemAdapter } /** - * Callback 验签 + * Callback 验签. * * @doc https://help.aliyun.com/zh/oss/developer-reference/callback - * @param string $authorizationBase64 Base64 编码的签名 - * @param string $pubKeyUrlBase64 Base64 编码的公钥 URL - * @param string $path 请求路径 - * @param string $body 请求体 - * @return array + * + * @param string $authorizationBase64 Base64 编码的签名 + * @param string $pubKeyUrlBase64 Base64 编码的公钥 URL + * @param string $path 请求路径 + * @param string $body 请求体 */ public function verify($authorizationBase64 = '', $pubKeyUrlBase64 = '', $path = '', $body = ''): array { @@ -290,15 +280,16 @@ class OssAdapter implements FilesystemAdapter /** * oss 直传配置. * - * @param string $prefix 目录前缀 - * @param null $callBackUrl 回调地址 - * @param array $customData 自定义参数 - * @param int $expire 过期时间(秒) - * @param array $systemData 系统接收参数,回调时会返回 - * @param array $policyData 自定义 policy 参数 - * see: https://help.aliyun.com/zh/oss/developer-reference/postobject#section-d5z-1ww-wdb - * @return string + * @param string $prefix 目录前缀 + * @param null $callBackUrl 回调地址 + * @param array $customData 自定义参数 + * @param int $expire 过期时间(秒) + * @param array $systemData 系统接收参数,回调时会返回 + * @param array $policyData 自定义 policy 参数 + * see: https://help.aliyun.com/zh/oss/developer-reference/postobject#section-d5z-1ww-wdb + * * @throws \JsonException|\InvalidArgumentException|\DateMalformedStringException + * * @deprecated use static::policyTokenSignatureV4() * @see https://help.aliyun.com/zh/oss/use-cases/overview-20 */ @@ -398,11 +389,14 @@ class OssAdapter implements FilesystemAdapter * callback_body_system_variables: array, * callback_body_custom_variables: array, * } $option ... + * * @return array{policy_token: array, policy_token_json: string} + * * @throws \InvalidArgumentException|\JsonException - * @link https://help.aliyun.com/zh/oss/developer-reference/postobject#section-d5z-1ww-wdb - * @link https://help.aliyun.com/zh/oss/use-cases/obtain-signature-information-from-the-server-and-upload-data-to-oss - * @link https://help.aliyun.com/zh/oss/use-cases/overview-20 + * + * @see https://help.aliyun.com/zh/oss/developer-reference/postobject#section-d5z-1ww-wdb + * @see https://help.aliyun.com/zh/oss/use-cases/obtain-signature-information-from-the-server-and-upload-data-to-oss + * @see https://help.aliyun.com/zh/oss/use-cases/overview-20 */ public function policyTokenSignatureV4(array $option = []): array { @@ -416,11 +410,11 @@ class OssAdapter implements FilesystemAdapter $utcTime = new \DateTime('now', new \DateTimeZone('UTC')); $date = $utcTime->format('Ymd'); $xOSSDate = $utcTime->format('Ymd\THis\Z'); - $expires = (int)($option['expire'] ?? 60); + $expires = (int) ($option['expire'] ?? 60); if ($expires <= 0) { $expires = 60; // 60 seconds } - $expiration = (clone $utcTime)->add(new \DateInterval('PT' . $expires . 'S')); + $expiration = (clone $utcTime)->add(new \DateInterval('PT'.$expires.'S')); // conditions $ossService = 'oss'; @@ -482,7 +476,7 @@ class OssAdapter implements FilesystemAdapter // build signature signing key $hmacHash = 'sha256'; - $dateHash = hash_hmac($hmacHash, $date, 'aliyun_v4' . $this->accessKeySecret, true); + $dateHash = hash_hmac($hmacHash, $date, 'aliyun_v4'.$this->accessKeySecret, true); $dateRegionKey = hash_hmac($hmacHash, $region, $dateHash, true); $dateRegionServiceKey = hash_hmac($hmacHash, $ossService, $dateRegionKey, true); $signingKey = hash_hmac($hmacHash, 'aliyun_v4_request', $dateRegionServiceKey, true); @@ -501,7 +495,7 @@ class OssAdapter implements FilesystemAdapter } else { foreach ($option['callback_body_system_variables'] as $key => $value) { if (!in_array($value, self::SYSTEM_FIELD, true)) { - throw new \InvalidArgumentException('Invalid OSS callback system field: ' . $value); + throw new \InvalidArgumentException('Invalid OSS callback system field: '.$value); } $systemVariables[$key] = $value; } @@ -511,7 +505,7 @@ class OssAdapter implements FilesystemAdapter $customVariables = []; if (!empty($option['callback_body_custom_variables'])) { foreach ($option['callback_body_custom_variables'] as $key => $value) { - $customVariables[$key] = '${x:' . $key . '}'; + $customVariables[$key] = '${x:'.$key.'}'; } } @@ -680,8 +674,6 @@ class OssAdapter implements FilesystemAdapter /** * visibility. * - * @param string $path - * @param string $visibility * @throws \OSS\Http\RequestCore_Exception */ public function setVisibility(string $path, string $visibility): void @@ -714,8 +706,6 @@ class OssAdapter implements FilesystemAdapter /** * Check whether a file exists. * - * @param string $path - * @return bool * @throws \OSS\Core\OssException * @throws \OSS\Http\RequestCore_Exception */ @@ -767,6 +757,7 @@ class OssAdapter implements FilesystemAdapter * read a file stream. * * @return array|bool|false + * * @throws \OSS\Http\RequestCore_Exception */ public function readStream(string $path) @@ -849,7 +840,7 @@ class OssAdapter implements FilesystemAdapter throw UnableToRetrieveMetadata::create($path, 'metadata', $exception->getErrorCode(), $exception); } - $size = (int)($result['content-length'] ?? 0); + $size = (int) ($result['content-length'] ?? 0); $timestamp = isset($result['last-modified']) ? strtotime($result['last-modified']) : 0; $mimetype = $result['content-type'] ?? ''; @@ -859,8 +850,6 @@ class OssAdapter implements FilesystemAdapter /** * get the size of file. * - * @param string $path - * @return \League\Flysystem\FileAttributes * @throws \OSS\Http\RequestCore_Exception */ public function fileSize(string $path): FileAttributes @@ -876,8 +865,6 @@ class OssAdapter implements FilesystemAdapter /** * get mime type. * - * @param string $path - * @return \League\Flysystem\FileAttributes * @throws \OSS\Http\RequestCore_Exception */ public function mimeType(string $path): FileAttributes @@ -893,8 +880,6 @@ class OssAdapter implements FilesystemAdapter /** * get timestamp. * - * @param string $path - * @return \League\Flysystem\FileAttributes * @throws \OSS\Http\RequestCore_Exception */ public function lastModified(string $path): FileAttributes diff --git a/src/Traits/SignatureTrait.php b/src/Traits/SignatureTrait.php index 49b2d1b4aed669029950eba3ed836c60aadf7b1b..dbe15bdd4a8deae91fdcdf304ce08e95a4297c11 100644 --- a/src/Traits/SignatureTrait.php +++ b/src/Traits/SignatureTrait.php @@ -16,8 +16,8 @@ trait SignatureTrait /** * gmt. * - * @param int $time * @return string + * * @throws \DateMalformedStringException */ public function gmt_iso8601(int $time) diff --git a/tests/OssAdapterTest.php b/tests/OssAdapterTest.php index 778b58e4574bfffc47856c8e456ce6b8a78cf0ad..49e68480dba3e94575fd6b48774640d667343be6 100644 --- a/tests/OssAdapterTest.php +++ b/tests/OssAdapterTest.php @@ -31,7 +31,7 @@ class OssAdapterTest extends TestCase { $extra = [ 'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4, - 'region' => 'cn-beijing', + 'region' => 'cn-beijing', ]; $adapter = new OssAdapter( '', @@ -44,9 +44,9 @@ class OssAdapterTest extends TestCase 'test' => [ 'access_key' => '', 'secret_key' => '', - 'bucket' => '', - 'endpoint' => 'https://oss-cn-beijing.aliyuncs.com', - 'isCName' => false, + 'bucket' => '', + 'endpoint' => 'https://oss-cn-beijing.aliyuncs.com', + 'isCName' => false, ], ], ...$extra, @@ -57,12 +57,11 @@ class OssAdapterTest extends TestCase $this->assertEquals('', $adapter->bucket('test')->getBucketName()); } - public function testPolicyTokenSignatureV4(): void { $extra = [ 'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4, - 'region' => 'cn-hangzhou', + 'region' => 'cn-hangzhou', ]; $adapter = new OssAdapter( '', @@ -75,21 +74,21 @@ class OssAdapterTest extends TestCase ...$extra, ); - $result = $adapter->policyTokenSignatureV4([ - 'expire' => 30*60, // 30 minutes - 'conditions' => [ - [ - 'eq', '$success_action_status', '200', - ], - [ - 'content-length-range', 1, 1048576000, // file size between 1 byte and 1GB - ] - ] - ]); + $result = $adapter->policyTokenSignatureV4([ + 'expire' => 30 * 60, // 30 minutes + 'conditions' => [ + [ + 'eq', '$success_action_status', '200', + ], + [ + 'content-length-range', 1, 1048576000, // file size between 1 byte and 1GB + ], + ], + ]); - print_r($result); + print_r($result); - $this->assertNotEmpty($result['policy_token']); - $this->assertNotEmpty($result['policy_token_json']); + $this->assertNotEmpty($result['policy_token']); + $this->assertNotEmpty($result['policy_token_json']); } }