From 0351f61038c71997f1a4e71af4d4cce3c1f04cfa Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 21 Feb 2023 17:16:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=98=BF=E9=87=8C=20?= =?UTF-8?q?oss=20=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 ++ .../Http/Controllers/UploadController.php | 13 +++ modules/Common/Support/Upload/OssUpload.php | 101 ++++++++++++++++++ modules/Common/config/upload.php | 18 ++++ modules/Common/routes/route.php | 10 +- .../admin/components/admin/upload/oss.vue | 59 ++++++++++ 6 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 modules/Common/Support/Upload/OssUpload.php create mode 100644 modules/Common/config/upload.php create mode 100644 resources/admin/components/admin/upload/oss.vue diff --git a/.env.example b/.env.example index ccaef34..c6255b2 100644 --- a/.env.example +++ b/.env.example @@ -58,3 +58,9 @@ VITE_PUSHER_PORT="${PUSHER_PORT}" VITE_PUSHER_SCHEME="${PUSHER_SCHEME}" VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" VITE_BASE_URL=${APP_URL}/api/ + +ALIOSS_BUCKET= +ALIOSS_ACCESS_ID= +ALIOSS_ACCESS_SECRET= +ALIOSS_ENDPOINT= +ALIOSS_UPLOAD_DIR= diff --git a/modules/Common/Http/Controllers/UploadController.php b/modules/Common/Http/Controllers/UploadController.php index f5391cc..a9616e5 100644 --- a/modules/Common/Http/Controllers/UploadController.php +++ b/modules/Common/Http/Controllers/UploadController.php @@ -3,6 +3,7 @@ namespace Modules\Common\Http\Controllers; use Illuminate\Http\Request; +use Modules\Common\Support\Upload\OssUpload; use Modules\Common\Support\Upload\Uploader; /** @@ -31,4 +32,16 @@ class UploadController { return $uploader->upload($request->file('image')); } + + + /** + * oss upload + * + * @param OssUpload $ossUpload + * @return array + */ + public function oss(OssUpload $ossUpload): array + { + return $ossUpload->config(); + } } diff --git a/modules/Common/Support/Upload/OssUpload.php b/modules/Common/Support/Upload/OssUpload.php new file mode 100644 index 0000000..f828d16 --- /dev/null +++ b/modules/Common/Support/Upload/OssUpload.php @@ -0,0 +1,101 @@ +accessKeySecret = config('common.upload.oss.access_secret'); + + $this->accessKeyId = config('common.upload.oss.access_id'); + + $this->dir = date('Y-m-d') . '/'; + + $this->endpoint = config('common.upload.oss.endpoint'); + + $this->maxSize = config('common.upload.max_size'); + + $this->bucket = config('common.upload.oss.bucket'); + } + + /** + * config + * + * @return array + */ + public function config(): array + { + return [ + 'bucket' => $this->bucket, + + 'accessKeyId' => $this->accessKeyId, + + 'host' => sprintf('https://%s.%s', $this->bucket, $this->endpoint), + + 'policy' => $this->policy(), + + 'signature' => $this->signature(), + + 'expire' => $this->getExpiration(), + + 'dir' => $this->dir, + + 'url' => $this->endpoint . $this->dir + ]; + } + + /** + * + * @return string + */ + protected function policy(): string + { + return base64_encode(json_encode([ + 'expiration' => $this->getExpiration(), + 'conditions' => [ + ['starts-with', '$key', $this->dir], + ['content-length-range', 0, $this->maxSize] + ] + ])); + } + + /** + * + * @return string + */ + protected function signature(): string + { + return base64_encode( + \hash_hmac('sha1', $this->policy(), $this->accessKeySecret, true) + ); + } + + /** + * + * @return string + */ + protected function getExpiration(): string + { + return date('Y-m-d\TH:i:s\Z', time() + $this->expire); + } +} diff --git a/modules/Common/config/upload.php b/modules/Common/config/upload.php new file mode 100644 index 0000000..a39de15 --- /dev/null +++ b/modules/Common/config/upload.php @@ -0,0 +1,18 @@ + 10 * 1024 * 1024, + + // oss 配置 + 'oss' => [ + 'bucket' => env('ALIOSS_BUCKET'), + + 'access_id' => env('ALIOSS_ACCESS_ID'), + + 'access_secret' => env('ALIOSS_ACCESS_SECRET'), + + 'endpoint' => env('ALIOSS_ENDPOINT'), + + 'dir' => env('ALIOSS_UPLOAD_DIR') + ], +]; diff --git a/modules/Common/routes/route.php b/modules/Common/routes/route.php index 4a324b7..54c07ee 100644 --- a/modules/Common/routes/route.php +++ b/modules/Common/routes/route.php @@ -17,6 +17,12 @@ use Modules\Common\Http\Controllers\UploadController; Route::get('options/{name}', [OptionController::class, 'index']); -Route::post('upload/file', [UploadController::class, 'file']); +Route::controller(UploadController::class)->group(function (){ + Route::post('upload/file', 'file'); + Route::post('upload/image', 'image'); + // get oss signature + Route::get('upload/oss', 'oss'); +}); + + -Route::post('upload/image', [UploadController::class, 'image']); diff --git a/resources/admin/components/admin/upload/oss.vue b/resources/admin/components/admin/upload/oss.vue new file mode 100644 index 0000000..632c390 --- /dev/null +++ b/resources/admin/components/admin/upload/oss.vue @@ -0,0 +1,59 @@ + + +