From 87e63575b2ef5fadefdbe80b27253f7e223adeef Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Tue, 6 Jun 2023 11:42:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=AC=E4=B8=9C?= =?UTF-8?q?=E4=BA=91=E9=A9=B1=E5=8A=A8=20=E6=B7=BB=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/storage/Jdoss.php | 195 +++++++++++++++++- 1 file changed, 184 insertions(+), 11 deletions(-) diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index dddf086a3..057753eb5 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -2,9 +2,12 @@ namespace crmeb\services\upload\storage; +use Aws\Acm\Exception\AcmException; use Aws\S3\S3Client; +use crmeb\exceptions\AdminException; use crmeb\exceptions\UploadException; use crmeb\services\upload\BaseUpload; +use Guzzle\Http\EntityBody; /** * 京东云COS文件上传 @@ -121,17 +124,82 @@ class Jdoss extends BaseUpload public function move(string $file = 'file') { - // TODO: Implement move() method. + $fileHandle = app()->request->file($file); + if (!$fileHandle) { + return $this->setError('上传的文件不存在'); + } + if ($this->validate) { + if (!in_array(pathinfo($fileHandle->getOriginalName(), PATHINFO_EXTENSION), $this->validate['fileExt'])) { + return $this->setError('不合法的文件后缀'); + } + if (filesize($fileHandle) > $this->validate['filesize']) { + return $this->setError('文件过大'); + } + if (!in_array($fileHandle->getOriginalMime(), $this->validate['fileMime'])) { + return $this->setError('不合法的文件类型'); + } + } + $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension()); + $key = $this->getUploadPath($key); + try { + $uploadInfo = $this->app()->putObject([ + 'Bucket' => $this->storageName, + 'Key' => $key, + 'SourceFile' => $fileHandle->getRealPath() + ]); + if (!isset($uploadInfo['info']['url'])) { + return $this->setError('Upload failure'); + } + $this->fileInfo->uploadInfo = $uploadInfo; + $this->fileInfo->realName = $fileHandle->getOriginalName(); + $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key; + $this->fileInfo->fileName = $key; + $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath); + $this->authThumb && $this->thumb($this->fileInfo->filePath); + return $this->fileInfo; + } catch (UploadException $e) { + return $this->setError($e->getMessage()); + } } public function stream($fileContent, string $key = null) { - // TODO: Implement stream() method. + try { + if (!$key) { + $key = $this->saveFileName(); + } + $key = $this->getUploadPath($key); + $fileContent = (string)EntityBody::factory($fileContent); + $uploadInfo = $this->app()->putObject([ + 'Bucket' => $this->storageName, + 'Key' => $key, + 'Body' => $fileContent + ]); + if (!isset($uploadInfo['info']['url'])) { + return $this->setError('Upload failure'); + } + $this->fileInfo->uploadInfo = $uploadInfo; + $this->fileInfo->realName = $key; + $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key; + $this->fileInfo->fileName = $key; + $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath); + $this->thumb($this->fileInfo->filePath); + return $this->fileInfo; + } catch (UploadException $e) { + return $this->setError($e->getMessage()); + } } - public function delete(string $filePath) + public function delete(string $key) { - // TODO: Implement delete() method. + try { + return $this->app()->deleteObject([ + 'Bucket' => $this->storageName, + 'Key' => $key + ]); + } catch (\Throwable $e) { + return $this->setError($e->getMessage()); + } } @@ -206,7 +274,20 @@ class Jdoss extends BaseUpload public function deleteBucket(string $name) { - // TODO: Implement deleteBucket() method. + try { + $this->app()->deleteBucket([ + 'bucketName' => $name, // REQUIRED + 'forceDelete' => false + ]); + return true; + } catch (AcmException $e) { + return $this->setError($e->getMessage()); + } + } + + public function getDomian(string $name, string $region = null) + { + } public function bindDomian(string $name, string $domain, string $region = null) @@ -216,21 +297,113 @@ class Jdoss extends BaseUpload public function setBucketCors(string $name, string $region) { - // TODO: Implement setBucketCors() method. + $this->storageRegion = $region; + try { + $this->app()->putBucketCors([ + 'Bucket' => $name, // REQUIRED + 'CORSConfiguration' => [ // REQUIRED + 'CORSRules' => [ // REQUIRED + [ + 'AllowedHeaders' => ['Authorization'], + 'AllowedMethods' => ['POST', 'GET', 'PUT'], // REQUIRED + 'AllowedOrigins' => ['*'], // REQUIRED + 'ExposeHeaders' => [], + 'MaxAgeSeconds' => 3000 + ], + ], + ] + ]); + return true; + } catch (AcmException $e) { + return $this->setError($e->getMessage()); + } } - public function getTempKeys() + /** + * 获取OSS上传密钥 + * @return mixed|void + */ + public function getTempKeys($key = '', $path = '', $expires = '+10 minutes') { - // TODO: Implement getTempKeys() method. + try { + $app = $this->app(); + $cmd = $app->getCommand([ + 'PutObject', [ + 'Bucket' => $this->storageName, + 'Key' => $key, + 'SourceFile' => $path + ] + ]); + $request = $app->createPresignedRequest($cmd, $expires); + return (string)$request->getUri(); + } catch (AcmException $e) { + return $this->setError($e->getMessage()); + } } - public function thumb(string $filePath = '') + /** + * 缩略图 + * @param string $filePath + * @param string $fileName + * @param string $type + * @return array|mixed + */ + public function thumb(string $filePath = '', string $fileName = '', string $type = 'all') { - // TODO: Implement thumb() method. + $filePath = $this->getFilePath($filePath); + $data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath]; + $this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath; + if ($filePath) { + $config = $this->thumbConfig; + foreach ($this->thumb as $v) { + if ($type == 'all' || $type == $v) { + $height = 'thumb_' . $v . '_height'; + $width = 'thumb_' . $v . '_width'; + $key = 'filePath' . ucfirst($v); + if (sys_config('image_thumbnail_status', 1) && isset($config[$height]) && isset($config[$width]) && $config[$height] && $config[$width]) { + $this->fileInfo->$key = $filePath . '?x-oss-process=image/resize,h_' . $config[$height] . ',w_' . $config[$width]; + $this->fileInfo->$key = $this->water($this->fileInfo->$key); + $data[$v] = $this->fileInfo->$key; + } else { + $this->fileInfo->$key = $this->water($this->fileInfo->$key); + $data[$v] = $this->fileInfo->$key; + } + } + } + } + return $data; } + /** + * 水印 + * @param string $filePath + * @return mixed|string + */ public function water(string $filePath = '') { - // TODO: Implement water() method. + $filePath = $this->getFilePath($filePath); + $waterConfig = $this->waterConfig; + $waterPath = $filePath; + if ($waterConfig['image_watermark_status'] && $filePath) { + if (strpos($filePath, '?x-oss-process') === false) { + $filePath .= '?x-oss-process=image'; + } + switch ($waterConfig['watermark_type']) { + case 1://图片 + if (!$waterConfig['watermark_image']) { + throw new AdminException(400722); + } + $waterPath = $filePath .= '/watermark,image_' . base64_encode($waterConfig['watermark_image']) . ',t_' . $waterConfig['watermark_opacity'] . ',g_' . ($this->position[$waterConfig['watermark_position']] ?? 'nw') . ',x_' . $waterConfig['watermark_x'] . ',y_' . $waterConfig['watermark_y']; + break; + case 2://文字 + if (!$waterConfig['watermark_text']) { + throw new AdminException(400723); + } + $waterConfig['watermark_text_color'] = str_replace('#', '', $waterConfig['watermark_text_color']); + $waterPath = $filePath .= '/watermark,text_' . base64_encode($waterConfig['watermark_text']) . ',color_' . $waterConfig['watermark_text_color'] . ',size_' . $waterConfig['watermark_text_size'] . ',g_' . ($this->position[$waterConfig['watermark_position']] ?? 'nw') . ',x_' . $waterConfig['watermark_x'] . ',y_' . $waterConfig['watermark_y']; + break; + } + } + return $waterPath; } } From fb162f59058ed50d1d7a8bf626567228ac589d3c Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Tue, 6 Jun 2023 14:11:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=AC=E4=B8=9C?= =?UTF-8?q?=E4=BA=91=E7=BB=91=E5=AE=9A=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/storage/Jdoss.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index 057753eb5..b0efc4297 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -292,7 +292,20 @@ class Jdoss extends BaseUpload public function bindDomian(string $name, string $domain, string $region = null) { - // TODO: Implement bindDomian() method. + try { + $this->app()->putBucketWebsite([ + 'Bucket' => $name, + 'WebsiteConfiguration' => [ + 'RedirectAllRequestsTo' => [ + 'HostName' => $domain, + 'Protocol' => 'http' + ] + ] + ]); + return true; + } catch (\Throwable $e) { + return $this->setError($e->getMessage()); + } } public function setBucketCors(string $name, string $region) @@ -314,7 +327,7 @@ class Jdoss extends BaseUpload ] ]); return true; - } catch (AcmException $e) { + } catch (\Throwable $e) { return $this->setError($e->getMessage()); } } @@ -336,7 +349,7 @@ class Jdoss extends BaseUpload ]); $request = $app->createPresignedRequest($cmd, $expires); return (string)$request->getUri(); - } catch (AcmException $e) { + } catch (\Throwable $e) { return $this->setError($e->getMessage()); } } From 91347642278ee7247a0efa495832092db6f66327 Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Tue, 6 Jun 2023 14:17:37 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=AC=E4=B8=9C?= =?UTF-8?q?=E4=BA=91=E8=8E=B7=E5=8F=96=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/storage/Jdoss.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index b0efc4297..1bacc7354 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -287,7 +287,14 @@ class Jdoss extends BaseUpload public function getDomian(string $name, string $region = null) { - + try { + $res = $this->app()->getBucketWebsite([ + 'Bucket' => $name + ]); + return $res['DomainName'] ?? []; + } catch (\Throwable $e) { + return $this->setError($e->getMessage()); + } } public function bindDomian(string $name, string $domain, string $region = null) From 215aa9acedc25ddc697c75d0001f5b713862d30d Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Tue, 6 Jun 2023 14:56:56 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=AC=E4=B8=9C?= =?UTF-8?q?=E4=BA=91=E8=8E=B7=E5=8F=96=E5=9F=9F=E5=90=8D=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20=20=E6=B7=BB=E5=8A=A0=E6=8A=A5=E9=94=99=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/Upload.php | 1 + crmeb/crmeb/services/upload/storage/Jdoss.php | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crmeb/crmeb/services/upload/Upload.php b/crmeb/crmeb/services/upload/Upload.php index d3e3b3e46..6b26ab177 100644 --- a/crmeb/crmeb/services/upload/Upload.php +++ b/crmeb/crmeb/services/upload/Upload.php @@ -20,6 +20,7 @@ use think\facade\Config; * @mixin \crmeb\services\upload\storage\OSS * @mixin \crmeb\services\upload\storage\COS * @mixin \crmeb\services\upload\storage\Qiniu + * @mixin \crmeb\services\upload\storage\Jdoss */ class Upload extends BaseManager { diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index 1bacc7354..e4663634e 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -272,9 +272,10 @@ class Jdoss extends BaseUpload ]; } - public function deleteBucket(string $name) + public function deleteBucket(string $name, string $region = '') { try { + $this->storageRegion = $region; $this->app()->deleteBucket([ 'bucketName' => $name, // REQUIRED 'forceDelete' => false @@ -288,6 +289,7 @@ class Jdoss extends BaseUpload public function getDomian(string $name, string $region = null) { try { + $this->storageRegion = $region; $res = $this->app()->getBucketWebsite([ 'Bucket' => $name ]); @@ -300,6 +302,7 @@ class Jdoss extends BaseUpload public function bindDomian(string $name, string $domain, string $region = null) { try { + $this->storageRegion = $region; $this->app()->putBucketWebsite([ 'Bucket' => $name, 'WebsiteConfiguration' => [ @@ -426,4 +429,33 @@ class Jdoss extends BaseUpload } return $waterPath; } + + public function setError(?string $error = null) + { + $this->error = $error ?: '未知错误'; + if (env('APP_DEBUG')) { + throw new UploadException($this->xmlToArr($this->error)); + } else { + return false; + } + } + + function xmlToArr($errorXml) + { + + $pattern = '/(\d+)<\/statusCode>(.*?)<\/Code>(.*?)<\/Message>(.*?)<\/Resource>(.*?)<\/RequestId>/'; + + preg_match($pattern, $errorXml, $matches); + + $error = [ + 'statusCode' => $matches[1], + 'Code' => $matches[2], + 'Message' => $matches[3], + 'Resource' => $matches[4], + 'RequestId' => $matches[5] + ]; + + return $error; + + } }