hyperf-chat/app/Controller/Api/V1/UploadController.php

117 lines
3.4 KiB
PHP
Raw Normal View History

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
*
2022-01-19 22:02:00 +08:00
* @RequestMapping(path="avatar", methods="post")
2021-09-11 22:50:57 +08:00
* @param Filesystem $filesystem
2021-04-22 16:14:34 +08:00
* @return ResponseInterface
2020-11-21 22:47:21 +08:00
*/
2022-01-22 12:48:28 +08:00
public function avatar(Filesystem $filesystem): ResponseInterface
2020-11-21 22:47:21 +08:00
{
2022-01-19 22:02:00 +08:00
$file = $this->request->file("file");
if (!$file->isValid()) {
2021-06-30 19:27:49 +08:00
return $this->response->fail();
}
2020-11-21 22:47:21 +08:00
2022-01-19 22:02:00 +08:00
$path = 'public/media/images/avatar/' . date('Ymd') . '/' . create_random_filename('png');
2021-06-30 19:27:49 +08:00
try {
2022-01-19 22:02:00 +08:00
$filesystem->write($path, file_get_contents($file->getRealPath()));
2021-06-30 19:27:49 +08:00
} 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
*
2022-01-19 22:02:00 +08:00
* @RequestMapping(path="multipart/initiate", methods="post")
2020-11-21 19:53:01 +08:00
*/
2022-01-19 22:02:00 +08:00
public function initiateMultipart(): ResponseInterface
2020-11-21 19:53:01 +08:00
{
$params = $this->request->inputs(['file_name', 'file_size']);
2022-01-22 12:48:28 +08:00
2020-11-21 19:53:01 +08:00
$this->validate($params, [
'file_name' => "required",
'file_size' => 'required|integer'
]);
$data = $this->splitUploadService->create($this->uid(), $params['file_name'], $params['file_size']);
2022-01-19 22:02:00 +08:00
$data['hash_name'] = $data["upload_id"];
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
*
2022-01-19 22:02:00 +08:00
* @RequestMapping(path="multipart", 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
{
2022-01-19 22:02:00 +08:00
$params = $this->request->inputs(['upload_id', 'split_index', 'split_num']);
2022-01-22 12:48:28 +08:00
2020-11-21 19:53:01 +08:00
$this->validate($params, [
2022-01-19 22:02:00 +08:00
'upload_id' => '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
]);
2022-01-22 12:48:28 +08:00
$file = $this->request->file('file');
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();
2022-01-19 22:02:00 +08:00
$uploadRes = $this->splitUploadService->upload($user_id, $file, $params['upload_id'], intval($params['split_index']));
2020-11-21 19:53:01 +08:00
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']) {
2022-01-19 22:02:00 +08:00
$fileInfo = $this->splitUploadService->merge($user_id, $params['upload_id']);
2020-11-21 19:53:01 +08:00
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,
2022-01-22 12:54:36 +08:00
'hash' => $params['upload_id']
2020-11-21 19:53:01 +08:00
]);
}
2022-01-22 12:55:01 +08:00
2020-11-21 19:53:01 +08:00
return $this->response->success(['is_file_merge' => false]);
}
}