From 927d993419ad15a9972bd36f2393c9808b5be2f9 Mon Sep 17 00:00:00 2001 From: evoxwht <442384644@qq.com> Date: Mon, 19 Jun 2023 12:19:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E7=BF=BC=E4=BA=91=E5=92=8C=E5=8D=8E?= =?UTF-8?q?=E4=B8=BA=E4=BA=91=E8=A7=86=E9=A2=91=E4=B8=8A=E4=BC=A0=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/storage/Obs.php | 55 +++++++++++++++-- crmeb/crmeb/services/upload/storage/Tyoss.php | 59 ++++++++++++++++--- 2 files changed, 102 insertions(+), 12 deletions(-) diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index a8e881be3..c759a7591 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -6,6 +6,7 @@ use crmeb\exceptions\AdminException; use crmeb\exceptions\UploadException; use crmeb\services\upload\extend\obs\Client as TyClient; use crmeb\services\upload\BaseUpload; +use DateTimeInterface; use GuzzleHttp\Psr7\Utils; class Obs extends BaseUpload @@ -236,8 +237,8 @@ class Obs extends BaseUpload 'AllowedHeader' => ['*'], 'AllowedMethod' => ['PUT', 'GET', 'POST', 'DELETE', 'HEAD'], 'AllowedOrigin' => ['*'], - 'ExposeHeader' => ['ETag', 'Content-Length', 'x-cos-request-id'], - 'MaxAgeSeconds' => 100 + 'ExposeHeader' => ['ETag'], + 'MaxAgeSeconds' => 0 ]); return true; } catch (\Throwable $e) { @@ -249,15 +250,59 @@ class Obs extends BaseUpload * @return array * @date 2023/6/13 */ - public function getTempKeys() + public function getTempKeys($callbackUrl = '', $dir = '') { +// return [ +// 'access_key' => $this->accessKey, +// 'secret_key' => $this->secretKey, +// 'type' => 'OBS' +// ]; + // TODO: Implement getTempKeys() method. + $base64CallbackBody = base64_encode(json_encode([ + 'callbackUrl' => $callbackUrl, + 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}', + 'callbackBodyType' => "application/x-www-form-urlencoded" + ])); + + $policy = json_encode([ + 'expiration' => $this->gmtIso8601(time() + 300), + 'conditions' => + [ + [0 => 'content-length-range', 1 => 0, 2 => 1048576000], + ['bucket' => $this->storageName], + [0 => 'starts-with', 1 => '$key', 2 => $dir], + ] + ]); + $base64Policy = base64_encode($policy); + $signature = base64_encode(hash_hmac('sha1', $base64Policy, $this->secretKey, true)); return [ - 'access_key' => $this->accessKey, - 'secret_key' => $this->secretKey, + 'accessid' => $this->accessKey, + 'host' => $this->uploadUrl, + 'policy' => $base64Policy, + 'signature' => $signature, + 'expire' => time() + 30, + 'callback' => $base64CallbackBody, + 'cdn' => $this->cdn, 'type' => 'OBS' ]; } + /** + * 获取ISO时间格式 + * @param $time + * @return string + * @throws \Exception + */ + protected function gmtIso8601($time): string + { + $dtStr = date("c", $time); + $myDateTime = new \DateTime($dtStr); + $expiration = $myDateTime->format(DateTimeInterface::ISO8601); + $pos = strpos($expiration, '+'); + $expiration = substr($expiration, 0, $pos); + return $expiration . "Z"; + } + /** * 缩略图 * @param string $filePath diff --git a/crmeb/crmeb/services/upload/storage/Tyoss.php b/crmeb/crmeb/services/upload/storage/Tyoss.php index 7df3be61d..999976760 100644 --- a/crmeb/crmeb/services/upload/storage/Tyoss.php +++ b/crmeb/crmeb/services/upload/storage/Tyoss.php @@ -5,6 +5,7 @@ namespace crmeb\services\upload\storage; use crmeb\exceptions\AdminException; use crmeb\services\upload\extend\obs\Client as TyClient; use crmeb\services\upload\BaseUpload; +use DateTimeInterface; use GuzzleHttp\Psr7\Utils; class Tyoss extends BaseUpload @@ -364,8 +365,8 @@ class Tyoss extends BaseUpload 'AllowedHeader' => ['*'], 'AllowedMethod' => ['PUT', 'GET', 'POST', 'DELETE', 'HEAD'], 'AllowedOrigin' => ['*'], - 'ExposeHeader' => ['ETag', 'Content-Length', 'x-cos-request-id'], - 'MaxAgeSeconds' => 100 + 'ExposeHeader' => ['ETag'], + 'MaxAgeSeconds' => 0 ]); return true; } catch (\Throwable $e) { @@ -374,18 +375,62 @@ class Tyoss extends BaseUpload } /** + * @param string $callbackUrl + * @param string $dir * @return array - * @date 2023/6/13 + * @throws \Exception + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/06/19 */ - public function getTempKeys() + public function getTempKeys($callbackUrl = '', $dir = '') { + // TODO: Implement getTempKeys() method. + $base64CallbackBody = base64_encode(json_encode([ + 'callbackUrl' => $callbackUrl, + 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}', + 'callbackBodyType' => "application/x-www-form-urlencoded" + ])); + + $policy = json_encode([ + 'expiration' => $this->gmtIso8601(time() + 300), + 'conditions' => + [ + [0 => 'content-length-range', 1 => 0, 2 => 1048576000], + ['bucket' => $this->storageName], + [0 => 'starts-with', 1 => '$key', 2 => $dir], + ] + ]); + $base64Policy = base64_encode($policy); + $signature = base64_encode(hash_hmac('sha1', $base64Policy, $this->secretKey, true)); return [ - 'access_key' => $this->accessKey, - 'secret_key' => $this->secretKey, - 'type' => 'TYOSS' + 'accessid' => $this->accessKey, + 'host' => $this->uploadUrl, + 'policy' => $base64Policy, + 'signature' => $signature, + 'expire' => time() + 30, + 'callback' => $base64CallbackBody, + 'cdn' => $this->cdn, + 'type' => 'OBS' ]; } + /** + * 获取ISO时间格式 + * @param $time + * @return string + * @throws \Exception + */ + protected function gmtIso8601($time): string + { + $dtStr = date("c", $time); + $myDateTime = new \DateTime($dtStr); + $expiration = $myDateTime->format(DateTimeInterface::ISO8601); + $pos = strpos($expiration, '+'); + $expiration = substr($expiration, 0, $pos); + return $expiration . "Z"; + } + /** * 缩略图 * @param string $filePath