feat: #20220802 文件上传接口

This commit is contained in:
赵世界 2022-08-02 18:56:04 +08:00
parent e83421ea5b
commit 1ae21f041f
6 changed files with 461 additions and 2 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Controllers;
use App\Models\Log as LogModel;
use App\Utils\UploadUtils;
use Illuminate\Http\Request;
class UploadController extends Controller
{
public function __construct(Request $request)
{
$this->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);
}
}

123
app/Utils/UploadUtils.php Normal file
View File

@ -0,0 +1,123 @@
<?php
namespace App\Utils;
use Illuminate\Http\UploadedFile;
use Intervention\Image\Facades\Image;
use OSS\Core\OssException;
use OSS\OssClient;
use Illuminate\Support\Str;
class UploadUtils
{
private static $ossClient;
private static $bucket = 'ct-upimg';
private static $prefix = 'ju8hn6/erp/shop/';
public static function init()
{
if (static::$ossClient === null) {
static::$ossClient = new OssClient(config('filesystems.disks.aliyun.access_id'), config('filesystems.disks.aliyun.access_key'), config('filesystems.disks.aliyun.endpoint'));
}
if (static::$bucket === null) {
static::$bucket = config('filesystems.disks.aliyun.bucket');
}
}
public static function putForUploadedResizeImage($bucketPath, UploadedFile $upload, $width = 800, $extension = null)
{
if ($upload->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;
}
}

View File

@ -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",

290
composer.lock generated
View File

@ -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",

View File

@ -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
],
],
];

View File

@ -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');