14 changed files with 475 additions and 5 deletions
-
9app/Constants/ErrorCode.php
-
68app/Controller/AttachmentController.php
-
35app/Exception/Handler/FilesystemExceptionHandler.php
-
30app/Listener/ValidatorFactoryResolvedListener.php
-
47app/Request/AttachmentRequest.php
-
46app/Request/ImageBase64Request.php
-
46app/Request/ImageRequest.php
-
73app/Service/AttachmentService.php
-
23app/Service/AttachmentServiceInterface.php
-
2composer.json
-
1config/autoload/exceptions.php
-
94config/autoload/file.php
-
3config/autoload/server.php
-
3config/routes.php
@ -0,0 +1,68 @@ |
|||
<?php |
|||
|
|||
namespace App\Controller; |
|||
|
|||
use App\Request\AttachmentRequest; |
|||
use App\Request\ImageBase64Request; |
|||
use App\Service\AttachmentServiceInterface; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
use App\Request\ImageRequest; |
|||
use League\Flysystem\Filesystem; |
|||
|
|||
class AttachmentController extends BaseController |
|||
{ |
|||
/** |
|||
* @Inject |
|||
* @var AttachmentServiceInterface |
|||
*/ |
|||
protected $attachmentService; |
|||
|
|||
/** |
|||
* 单文件表单上传 |
|||
* @param AttachmentRequest $request |
|||
* @param Filesystem $filesystem |
|||
* @return \Psr\Http\Message\ResponseInterface |
|||
*/ |
|||
public function upload(AttachmentRequest $request, Filesystem $filesystem) |
|||
{ |
|||
$file = $this->request->file('upload'); |
|||
$type = $this->request->input('type', ''); |
|||
|
|||
$fileName = $this->attachmentService->formUpload($file, $type, $filesystem, 'file'); |
|||
|
|||
return $this->success(['file_path' => $fileName]); |
|||
} |
|||
|
|||
/** |
|||
* 单图表单上传 |
|||
* @param ImageRequest $request |
|||
* @param Filesystem $filesystem |
|||
* @return \Psr\Http\Message\ResponseInterface |
|||
*/ |
|||
public function uploadImage(ImageRequest $request, Filesystem $filesystem) |
|||
{ |
|||
$file = $this->request->file('upload'); |
|||
$type = $this->request->input('type', ''); |
|||
|
|||
$fileName = $this->attachmentService->formUpload($file, $type, $filesystem); |
|||
|
|||
return $this->success(['file_path' => $fileName]); |
|||
} |
|||
|
|||
/** |
|||
* 单图base64上传 |
|||
* @param ImageBase64Request $request |
|||
* @param Filesystem $filesystem |
|||
* @return \Psr\Http\Message\ResponseInterface |
|||
*/ |
|||
public function uploadImageByBase64(ImageBase64Request $request, Filesystem $filesystem) |
|||
{ |
|||
$base64Code = $this->request->input('upload'); |
|||
$type = $this->request->input('type', ''); |
|||
|
|||
$fileName = $this->attachmentService->base64Upload($base64Code, $type, $filesystem); |
|||
|
|||
return $this->success(['file_path' => $fileName]); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
<?php |
|||
|
|||
namespace App\Exception\Handler; |
|||
|
|||
use App\Constants\ErrorCode; |
|||
use Hyperf\ExceptionHandler\ExceptionHandler; |
|||
use Hyperf\HttpMessage\Stream\SwooleStream; |
|||
use League\Flysystem\FilesystemException; |
|||
use Psr\Http\Message\ResponseInterface; |
|||
use Throwable; |
|||
|
|||
class FilesystemExceptionHandler extends ExceptionHandler |
|||
{ |
|||
|
|||
public function handle(Throwable $throwable, ResponseInterface $response) |
|||
{ |
|||
$this->stopPropagation(); |
|||
|
|||
$content = json_encode([ |
|||
"status" => 'error', |
|||
"code" => ErrorCode::UPLOAD_INVALID, |
|||
"result" => [], |
|||
"message" => $throwable->getMessage() |
|||
]); |
|||
|
|||
return $response->withHeader('Content-Type', 'application/json') |
|||
->withStatus($throwable->status) |
|||
->withBody(new SwooleStream($content)); |
|||
} |
|||
|
|||
public function isValid(Throwable $throwable): bool |
|||
{ |
|||
return $throwable instanceof FilesystemException; |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Request; |
|||
|
|||
use Hyperf\Validation\Request\FormRequest; |
|||
|
|||
class AttachmentRequest extends FormRequest |
|||
{ |
|||
/** |
|||
* Determine if the user is authorized to make this request. |
|||
*/ |
|||
public function authorize(): bool |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* Get the validation rules that apply to the request. |
|||
*/ |
|||
public function rules(): array |
|||
{ |
|||
return [ |
|||
'upload' => 'required|nonempty|file|ext_not_in', |
|||
'type' => 'nonempty|alpha' |
|||
]; |
|||
} |
|||
|
|||
public function messages(): array |
|||
{ |
|||
return [ |
|||
'upload.required' => '未选择上传的文件', |
|||
'upload.nonempty' => '文件异常', |
|||
'upload.file' => '文件上传不成功', |
|||
'upload.ext_not_in' => '文件不允许上传', |
|||
'type.nonempty' => '文件类型参数异常', |
|||
]; |
|||
} |
|||
|
|||
public function attributes(): array |
|||
{ |
|||
return [ |
|||
'upload' => '文件' |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Request; |
|||
|
|||
use Hyperf\Validation\Request\FormRequest; |
|||
|
|||
class ImageBase64Request extends FormRequest |
|||
{ |
|||
/** |
|||
* Determine if the user is authorized to make this request. |
|||
*/ |
|||
public function authorize(): bool |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* Get the validation rules that apply to the request. |
|||
*/ |
|||
public function rules(): array |
|||
{ |
|||
return [ |
|||
'upload' => 'required|nonempty|base64:image', |
|||
'type' => 'nonempty|alpha' |
|||
]; |
|||
} |
|||
|
|||
public function messages(): array |
|||
{ |
|||
return [ |
|||
'upload.required' => '未选择上传的文件', |
|||
'upload.base64' => '文件不是正常的图片', |
|||
'upload.nonempty' => '文件异常', |
|||
'type.nonempty' => '图片类型参数异常', |
|||
]; |
|||
} |
|||
|
|||
public function attributes(): array |
|||
{ |
|||
return [ |
|||
'upload' => '图片' |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Request; |
|||
|
|||
use Hyperf\Validation\Request\FormRequest; |
|||
|
|||
class ImageRequest extends FormRequest |
|||
{ |
|||
/** |
|||
* Determine if the user is authorized to make this request. |
|||
*/ |
|||
public function authorize(): bool |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* Get the validation rules that apply to the request. |
|||
*/ |
|||
public function rules(): array |
|||
{ |
|||
return [ |
|||
'upload' => 'required|nonempty|file|image', |
|||
'type' => 'nonempty|alpha' |
|||
]; |
|||
} |
|||
|
|||
public function messages(): array |
|||
{ |
|||
return [ |
|||
'upload.required' => '未选择上传的文件', |
|||
'upload.image' => '文件不是正常的图片', |
|||
'upload.nonempty' => '文件异常', |
|||
'type.nonempty' => '图片类型参数异常', |
|||
]; |
|||
} |
|||
|
|||
public function attributes(): array |
|||
{ |
|||
return [ |
|||
'upload' => '图片' |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Service; |
|||
|
|||
use App\Constants\ErrorCode; |
|||
use League\Flysystem\FilesystemNotFoundException; |
|||
|
|||
class AttachmentService implements AttachmentServiceInterface |
|||
{ |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image') |
|||
{ |
|||
|
|||
$fileRealPath = $file->getRealPath(); |
|||
$fileHash = md5_file($fileRealPath); |
|||
|
|||
$path = $this->getBasePath($path, $attachmenttype); |
|||
$fileName = $path . '/' . $fileHash . '.' . $file->getExtension(); |
|||
|
|||
$stream = fopen($fileRealPath, 'r+'); |
|||
$filesystem->writeStream($fileName, $stream); |
|||
fclose($stream); |
|||
|
|||
return $fileName; |
|||
} |
|||
|
|||
/** |
|||
* @inheritDoc |
|||
*/ |
|||
public function base64Upload($contents, $path, $filesystem) |
|||
{ |
|||
|
|||
preg_match('/^(data:\s*image\/(\w+);base64,)/', $contents, $result); |
|||
if (empty($result)) { |
|||
throw new FilesystemNotFoundException(ErrorCode::getMessage(ErrorCode::UPLOAD_INVALID),ErrorCode::UPLOAD_INVALID); |
|||
} |
|||
|
|||
$contents = base64_decode(str_replace($result[1], '', $contents)); |
|||
|
|||
$fileHash = md5($contents); |
|||
$path = $this->getBasePath($path); |
|||
$fileName = $path . '/' . $fileHash . '.' . $result[2]; |
|||
|
|||
$filesystem->write($fileName, $contents); |
|||
|
|||
return $fileName; |
|||
} |
|||
|
|||
protected function getBasePath($path, $attachmenttype = 'image') |
|||
{ |
|||
switch ($attachmenttype) { |
|||
case 'image': |
|||
$baseDir = env('IMAGE_BASE', '/attachment/images'); |
|||
break; |
|||
|
|||
case 'file': |
|||
$baseDir = env('FILES_BASE', '/attachment/files'); |
|||
break; |
|||
|
|||
default: |
|||
$baseDir = env('FILES_BASE', '/attachment'); |
|||
break; |
|||
} |
|||
|
|||
$path = $path ? '/'.$path : ''; |
|||
$path .= '/'.date('Y').'/'.date('m').'/'.date('d'); |
|||
return $baseDir.$path; |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
<?php |
|||
|
|||
namespace App\Service; |
|||
|
|||
interface AttachmentServiceInterface |
|||
{ |
|||
/** |
|||
* 表单上传,单文件 |
|||
* @param $file |
|||
* @param $path |
|||
* @param $filesystem |
|||
* @param string $attachmenttype |
|||
*/ |
|||
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image'); |
|||
|
|||
/** |
|||
* base64code上传,单文件 |
|||
* @param $contents |
|||
* @param $path |
|||
* @param $filesystem |
|||
*/ |
|||
public function base64Upload($contents, $path, $filesystem); |
|||
} |
|||
@ -0,0 +1,94 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
/** |
|||
* This file is part of Hyperf. |
|||
* |
|||
* @link https://www.hyperf.io |
|||
* @document https://hyperf.wiki |
|||
* @contact group@hyperf.io |
|||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
|||
*/ |
|||
return [ |
|||
'default' => 'oss', |
|||
'storage' => [ |
|||
'local' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\LocalAdapterFactory::class, |
|||
'root' => BASE_PATH . '/attachment', |
|||
], |
|||
'ftp' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\FtpAdapterFactory::class, |
|||
'host' => 'ftp.example.com', |
|||
'username' => 'username', |
|||
'password' => 'password', |
|||
// 'port' => 21,
|
|||
// 'root' => '/path/to/root',
|
|||
// 'passive' => true,
|
|||
// 'ssl' => true,
|
|||
// 'timeout' => 30,
|
|||
// 'ignorePassiveAddress' => false,
|
|||
], |
|||
'memory' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\MemoryAdapterFactory::class, |
|||
], |
|||
's3' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\S3AdapterFactory::class, |
|||
'credentials' => [ |
|||
'key' => env('S3_KEY'), |
|||
'secret' => env('S3_SECRET'), |
|||
], |
|||
'region' => env('S3_REGION'), |
|||
'version' => 'latest', |
|||
'bucket_endpoint' => false, |
|||
'use_path_style_endpoint' => false, |
|||
'endpoint' => env('S3_ENDPOINT'), |
|||
'bucket_name' => env('S3_BUCKET'), |
|||
], |
|||
'minio' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\S3AdapterFactory::class, |
|||
'credentials' => [ |
|||
'key' => env('S3_KEY'), |
|||
'secret' => env('S3_SECRET'), |
|||
], |
|||
'region' => env('S3_REGION'), |
|||
'version' => 'latest', |
|||
'bucket_endpoint' => false, |
|||
'use_path_style_endpoint' => true, |
|||
'endpoint' => env('S3_ENDPOINT'), |
|||
'bucket_name' => env('S3_BUCKET'), |
|||
], |
|||
'oss' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\AliyunOssAdapterFactory::class, |
|||
'accessId' => env('OSS_ACCESS_ID'), |
|||
'accessSecret' => env('OSS_ACCESS_SECRET'), |
|||
'bucket' => env('OSS_BUCKET'), |
|||
'endpoint' => env('OSS_ENDPOINT'), |
|||
// 'timeout' => 3600,
|
|||
// 'connectTimeout' => 10,
|
|||
// 'isCName' => false,
|
|||
// 'token' => '',
|
|||
], |
|||
'qiniu' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\QiniuAdapterFactory::class, |
|||
'accessKey' => env('QINIU_ACCESS_KEY'), |
|||
'secretKey' => env('QINIU_SECRET_KEY'), |
|||
'bucket' => env('QINIU_BUCKET'), |
|||
'domain' => env('QINBIU_DOMAIN'), |
|||
], |
|||
'cos' => [ |
|||
'driver' => \Hyperf\Filesystem\Adapter\CosAdapterFactory::class, |
|||
'region' => env('COS_REGION'), |
|||
'credentials' => [ |
|||
'appId' => env('COS_APPID'), |
|||
'secretId' => env('COS_SECRET_ID'), |
|||
'secretKey' => env('COS_SECRET_KEY'), |
|||
], |
|||
'bucket' => env('COS_BUCKET'), |
|||
'read_from_cdn' => false, |
|||
// 'timeout' => 60,
|
|||
// 'connect_timeout' => 60,
|
|||
// 'cdn' => '',
|
|||
// 'scheme' => 'https',
|
|||
], |
|||
], |
|||
]; |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue