13 changed files with 381 additions and 4 deletions
-
6app/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
-
3config/autoload/dependencies.php
-
1config/autoload/exceptions.php
-
4config/autoload/file.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); |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue