From 1ae21f041f170d63aad8a01d3974facf4936ff59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Tue, 2 Aug 2022 18:56:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#20220802=20=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/UploadController.php | 33 +++ app/Utils/UploadUtils.php | 123 +++++++++ composer.json | 2 + composer.lock | 290 +++++++++++++++++++++- config/filesystems.php | 13 +- routes/api.php | 2 + 6 files changed, 461 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/UploadController.php create mode 100644 app/Utils/UploadUtils.php diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php new file mode 100644 index 0000000..59773ce --- /dev/null +++ b/app/Http/Controllers/UploadController.php @@ -0,0 +1,33 @@ +log = new LogModel([ + 'module' => 'file', + 'action' => $request->getMethod(), + 'target_type' => 'upload', + ]); + } + + public function store(Request $request) + { + if (!$request->hasFile('uploadFile')) { + $this->res = [ + 'httpCode' => 404, + 'errorCode' => 404404, + 'errorMessage' => 'not found file', + ]; + } + $this->addLog(0, 'add'); + + return UploadUtils::putForUploadedFile('image', $request->uploadFile); + } +} diff --git a/app/Utils/UploadUtils.php b/app/Utils/UploadUtils.php new file mode 100644 index 0000000..caaba2c --- /dev/null +++ b/app/Utils/UploadUtils.php @@ -0,0 +1,123 @@ +extension() != 'gif') { + // 压缩图片 + $originalName = static::randomFileName($extension ?: $upload->getClientOriginalExtension()); + $storagePath = storage_path('app/public/file/') . $originalName; + $img = Image::make($upload->getRealPath()); + + // 未传宽度 大于800压缩至800 否则按照原宽度 + $width = $img->width() >= 800 ? 800 : $img->width(); + // 高度自适应 + $img->resize($width, null, function ($constraint) { + $constraint->aspectRatio(); + }); + $img->save($storagePath); + $upload = new UploadedFile($storagePath, $originalName); + } + + // 上传并删除压缩图片 + try { + $filePath = static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension()); + } catch (OssException $e) { + throw $e; + } finally { + if (isset($storagePath)) { + unlink($storagePath); + } + } + + return $filePath; + } + + //上传图片 + public static function putForUploadedFile($bucketPath, UploadedFile $upload) + { + return static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension()); + } + + public static function put($bucketPath, $filepath, $ext) + { + static::init(); + try { + $filePath = static::$prefix . $bucketPath . '/' . static::randomFileName($ext); + static::$ossClient->uploadFile(static::$bucket, $filePath, $filepath); + } catch (OssException $e) { + throw $e; + } + return $filePath; + } + + public static function buildFilePath() + { + return date('Y') . '/' . date('m') . '/' . date('d'); + } + + public static function randomFileName($ext, $len = 40) + { + return Str::random($len) . '.' . $ext; + } + + //删除修改前,上传修改后图片 + public static function delAndUploadImage($file, $filePath, UploadedFile $upload) + { + static::delFile($file); + return static::putForUploadedFile($filePath, $upload); + } + + //删除修改前,上传修改后视频 + public static function delAndUploadVideo($file, $filePath, UploadedFile $upload) + { + static::delFile($file); + return static::putForUploadedFile($filePath, $upload); + } + + public static function delFile($file) + { + static::init(); + if (static::exist($file)) { + return static::$ossClient->deleteObject(static::$bucket, $file); + } + return false; + } + + public static function exist($file) + { + static::init(); + return static::$ossClient->doesObjectExist(static::$bucket, $file); + } + + public static function getFullImgUrl($filePath) + { + return config('filesystems.disks.aliyun.url') . $filePath; + } + +} diff --git a/composer.json b/composer.json index 63a511a..e442ecc 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ "license": "MIT", "require": { "php": "^7.2.5|^8.0", + "aliyuncs/oss-sdk-php": "^2.5", "fideloper/proxy": "^4.4", + "intervention/image": "^2.7", "laravel/framework": "^6.20.26", "laravel/tinker": "^2.5", "maatwebsite/excel": "^3.1", diff --git a/composer.lock b/composer.lock index 6c4e88e..b66292d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,53 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "486f251784470138e7bd835f6ea84ce3", + "content-hash": "87b78d5c6e1dfc1285abb501c7eb8372", "packages": [ + { + "name": "aliyuncs/oss-sdk-php", + "version": "v2.5.0", + "source": { + "type": "git", + "url": "https://github.com/aliyun/aliyun-oss-php-sdk.git", + "reference": "f0413667d765855eb0aaa728b596801464ffdb06" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/aliyun/aliyun-oss-php-sdk/zipball/f0413667d765855eb0aaa728b596801464ffdb06", + "reference": "f0413667d765855eb0aaa728b596801464ffdb06", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpunit/phpunit": "*", + "satooshi/php-coveralls": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "OSS\\": "src/OSS" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Aliyuncs", + "homepage": "http://www.aliyun.com" + } + ], + "description": "Aliyun OSS SDK for PHP", + "homepage": "http://www.aliyun.com/product/oss/", + "support": { + "issues": "https://github.com/aliyun/aliyun-oss-php-sdk/issues", + "source": "https://github.com/aliyun/aliyun-oss-php-sdk/tree/v2.5.0" + }, + "time": "2022-05-13T07:41:28+00:00" + }, { "name": "doctrine/inflector", "version": "2.0.4", @@ -414,6 +459,205 @@ }, "time": "2022-02-09T13:33:34+00:00" }, + { + "name": "guzzlehttp/psr7", + "version": "2.4.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "13388f00956b1503577598873fffb5ae994b5737" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/13388f00956b1503577598873fffb5ae994b5737", + "reference": "13388f00956b1503577598873fffb5ae994b5737", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0", + "psr/http-factory": "^1.0", + "psr/http-message": "^1.0", + "ralouphie/getallheaders": "^3.0" + }, + "provide": { + "psr/http-factory-implementation": "1.0", + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "http-interop/http-factory-tests": "^0.9", + "phpunit/phpunit": "^8.5.8 || ^9.3.10" + }, + "suggest": { + "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://sagikazarmark.hu" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "support": { + "issues": "https://github.com/guzzle/psr7/issues", + "source": "https://github.com/guzzle/psr7/tree/2.4.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", + "type": "tidelift" + } + ], + "time": "2022-06-20T21:43:11+00:00" + }, + { + "name": "intervention/image", + "version": "2.7.2", + "source": { + "type": "git", + "url": "https://github.com/Intervention/image.git", + "reference": "04be355f8d6734c826045d02a1079ad658322dad" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Intervention/image/zipball/04be355f8d6734c826045d02a1079ad658322dad", + "reference": "04be355f8d6734c826045d02a1079ad658322dad", + "shasum": "" + }, + "require": { + "ext-fileinfo": "*", + "guzzlehttp/psr7": "~1.1 || ^2.0", + "php": ">=5.4.0" + }, + "require-dev": { + "mockery/mockery": "~0.9.2", + "phpunit/phpunit": "^4.8 || ^5.7 || ^7.5.15" + }, + "suggest": { + "ext-gd": "to use GD library based image processing.", + "ext-imagick": "to use Imagick based image processing.", + "intervention/imagecache": "Caching extension for the Intervention Image library" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + }, + "laravel": { + "providers": [ + "Intervention\\Image\\ImageServiceProvider" + ], + "aliases": { + "Image": "Intervention\\Image\\Facades\\Image" + } + } + }, + "autoload": { + "psr-4": { + "Intervention\\Image\\": "src/Intervention/Image" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Oliver Vogel", + "email": "oliver@intervention.io", + "homepage": "https://intervention.io/" + } + ], + "description": "Image handling and manipulation library with support for Laravel integration", + "homepage": "http://image.intervention.io/", + "keywords": [ + "gd", + "image", + "imagick", + "laravel", + "thumbnail", + "watermark" + ], + "support": { + "issues": "https://github.com/Intervention/image/issues", + "source": "https://github.com/Intervention/image/tree/2.7.2" + }, + "funding": [ + { + "url": "https://paypal.me/interventionio", + "type": "custom" + }, + { + "url": "https://github.com/Intervention", + "type": "github" + } + ], + "time": "2022-05-21T17:30:32+00:00" + }, { "name": "laravel/framework", "version": "v6.20.44", @@ -2131,6 +2375,50 @@ }, "time": "2022-07-07T13:49:11+00:00" }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "support": { + "issues": "https://github.com/ralouphie/getallheaders/issues", + "source": "https://github.com/ralouphie/getallheaders/tree/develop" + }, + "time": "2019-03-08T08:55:37+00:00" + }, { "name": "ramsey/uuid", "version": "3.9.6", diff --git a/config/filesystems.php b/config/filesystems.php index 220c010..9df9941 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -64,7 +64,18 @@ return [ 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), ], - + 'aliyun' => [ + 'driver' => 'oss', + 'access_id' => 'LTAI6do39W9jINpe', + 'access_key' => 'XLhp5w4WnRcqji204GYOm7hZsoXSB6', + 'bucket' => 'ct-upimg', + 'endpoint' => 'oss-cn-hangzhou.aliyuncs.com', // OSS 外网节点或自定义外部域名 +// 'cdnDomain' => 'cdn-xiangma.yx090.com', // 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn + 'url' => 'https://ct-upimg.yx090.com/', + 'ssl' => true, + 'isCName' => false, // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url + 'debug' => true + ], ], ]; diff --git a/routes/api.php b/routes/api.php index e97d2a3..2ce75fd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,7 @@ use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Role\RolesController; +use App\Http\Controllers\UploadController; /* |-------------------------------------------------------------------------- @@ -37,6 +38,7 @@ Route::middleware('auth:api')->group(function () { Route::resource('menus', 'Menu\MenusController', ['only' => ['index', // 'store', 'show', 'update', 'destroy' ]]); + Route::post('upload', [UploadController::class, 'store'])->name('upload.file'); }); Route::post('/auth/login', [LoginController::class, 'login'])->name('auth.login');