2020-11-21 19:53:01 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller\Api\V1;
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
|
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
|
|
|
use Hyperf\HttpServer\Annotation\Middleware;
|
2020-11-24 23:23:12 +08:00
|
|
|
use App\Middleware\JWTAuthMiddleware;
|
2020-11-29 14:44:11 +08:00
|
|
|
use App\Service\SplitUploadService;
|
2021-06-30 19:27:49 +08:00
|
|
|
use League\Flysystem\Filesystem;
|
2020-12-26 21:33:40 +08:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
/**
|
2021-06-30 19:27:49 +08:00
|
|
|
* 上传文件控制器
|
2021-07-05 21:52:44 +08:00
|
|
|
* @Controller(prefix="/api/v1/upload")
|
2020-11-21 19:53:01 +08:00
|
|
|
* @Middleware(JWTAuthMiddleware::class)
|
|
|
|
*
|
|
|
|
* @package App\Controller\Api\V1
|
|
|
|
*/
|
|
|
|
class UploadController extends CController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inject
|
|
|
|
* @var SplitUploadService
|
|
|
|
*/
|
|
|
|
private $splitUploadService;
|
|
|
|
|
2020-11-21 22:47:21 +08:00
|
|
|
/**
|
|
|
|
* 图片文件流上传接口
|
2021-04-22 16:14:34 +08:00
|
|
|
*
|
2021-09-11 22:50:57 +08:00
|
|
|
* @RequestMapping(path="file-stream", methods="post")
|
|
|
|
* @param Filesystem $filesystem
|
2021-04-22 16:14:34 +08:00
|
|
|
* @return ResponseInterface
|
2020-11-21 22:47:21 +08:00
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function fileStream(Filesystem $filesystem): ResponseInterface
|
2020-11-21 22:47:21 +08:00
|
|
|
{
|
|
|
|
$fileStream = $this->request->post('fileStream', '');
|
2021-06-30 19:27:49 +08:00
|
|
|
if (empty($fileStream)) {
|
|
|
|
return $this->response->fail();
|
|
|
|
}
|
2020-11-21 22:47:21 +08:00
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
$path = 'media/images/avatar/' . date('Ymd') . '/' . create_random_filename('png');
|
|
|
|
try {
|
|
|
|
$filesystem->write($path, base64_decode(str_replace(['data:image/png;base64,', ' '], ['', '+'], $fileStream)));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $this->response->fail();
|
|
|
|
}
|
2021-05-13 18:01:34 +08:00
|
|
|
|
2020-11-21 22:47:21 +08:00
|
|
|
return $this->response->success(['avatar' => get_media_url($path)]);
|
|
|
|
}
|
2020-11-24 23:23:12 +08:00
|
|
|
|
2020-11-21 19:53:01 +08:00
|
|
|
/**
|
|
|
|
* 获取拆分文件信息
|
2021-04-22 16:14:34 +08:00
|
|
|
*
|
2021-09-11 22:50:57 +08:00
|
|
|
* @RequestMapping(path="get-file-split-info", methods="get")
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function getFileSplitInfo(): ResponseInterface
|
2020-11-21 19:53:01 +08:00
|
|
|
{
|
|
|
|
$params = $this->request->inputs(['file_name', 'file_size']);
|
|
|
|
$this->validate($params, [
|
|
|
|
'file_name' => "required",
|
|
|
|
'file_size' => 'required|integer'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$data = $this->splitUploadService->create($this->uid(), $params['file_name'], $params['file_size']);
|
|
|
|
|
2021-05-13 18:01:34 +08:00
|
|
|
return $data ? $this->response->success($data) : $this->response->fail('获取文件拆分信息失败!');
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-29 17:39:24 +08:00
|
|
|
* 文件拆分上传接口
|
2021-04-22 16:14:34 +08:00
|
|
|
*
|
2021-09-11 22:50:57 +08:00
|
|
|
* @RequestMapping(path="file-subarea-upload", methods="post")
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function fileSubareaUpload(): ResponseInterface
|
2020-11-21 19:53:01 +08:00
|
|
|
{
|
2021-04-20 16:30:57 +08:00
|
|
|
$file = $this->request->file('file');
|
2020-11-21 19:53:01 +08:00
|
|
|
$params = $this->request->inputs(['name', 'hash', 'ext', 'size', 'split_index', 'split_num']);
|
|
|
|
$this->validate($params, [
|
2021-04-20 16:30:57 +08:00
|
|
|
'name' => "required",
|
|
|
|
'hash' => 'required',
|
|
|
|
'ext' => 'required',
|
|
|
|
'size' => 'required',
|
2020-11-21 19:53:01 +08:00
|
|
|
'split_index' => 'required',
|
2021-04-20 16:30:57 +08:00
|
|
|
'split_num' => 'required'
|
2020-11-21 19:53:01 +08:00
|
|
|
]);
|
|
|
|
|
2021-05-28 18:38:10 +08:00
|
|
|
if (!$file || !$file->isValid()) {
|
2020-11-21 19:53:01 +08:00
|
|
|
return $this->response->fail();
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:30:57 +08:00
|
|
|
$user_id = $this->uid();
|
2020-11-21 19:53:01 +08:00
|
|
|
$uploadRes = $this->splitUploadService->upload($user_id, $file, $params['hash'], intval($params['split_index']), intval($params['size']));
|
|
|
|
if (!$uploadRes) {
|
2021-07-01 19:11:11 +08:00
|
|
|
return $this->response->fail('上传文件失败!');
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (($params['split_index'] + 1) == $params['split_num']) {
|
|
|
|
$fileInfo = $this->splitUploadService->merge($user_id, $params['hash']);
|
|
|
|
if (!$fileInfo) {
|
2021-05-13 18:01:34 +08:00
|
|
|
return $this->response->fail('上传文件失败!');
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->success([
|
|
|
|
'is_file_merge' => true,
|
2021-04-20 16:30:57 +08:00
|
|
|
'hash' => $params['hash']
|
2020-11-21 19:53:01 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->success(['is_file_merge' => false]);
|
|
|
|
}
|
|
|
|
}
|