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

74 lines
2.5 KiB
PHP
Raw Normal View History

2020-11-21 19:53:01 +08:00
<?php
namespace App\Controller\Api\V1;
2021-07-28 23:08:07 +08:00
use App\Service\Group\GroupMemberService;
2020-11-21 19:53:01 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
2020-11-28 19:59:10 +08:00
use App\Model\Article\ArticleAnnex;
2021-07-09 22:57:19 +08:00
use App\Model\Talk\TalkRecords;
use App\Model\Talk\TalkRecordsFile;
2020-11-28 19:59:10 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2021-06-30 19:27:49 +08:00
use League\Flysystem\Filesystem;
2020-11-21 19:53:01 +08:00
/**
* Class DownloadController
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/download")
2020-12-26 21:33:40 +08:00
* @Middleware(JWTAuthMiddleware::class)
2020-11-21 19:53:01 +08:00
*
* @package App\Controller\Api\V1
*/
class DownloadController extends CController
{
2021-06-30 19:27:49 +08:00
private function getFilePath(string $path)
{
2021-07-23 21:34:29 +08:00
return di()->get(Filesystem::class)->getConfig()->get('root') . '/' . $path;
2021-06-30 19:27:49 +08:00
}
2020-11-21 19:53:01 +08:00
/**
* 下载用户聊天文件
* @RequestMapping(path="user-chat-file", methods="get")
*
2020-11-28 19:59:10 +08:00
* @param ResponseInterface $response
2021-06-30 19:27:49 +08:00
* @param Filesystem $filesystem
2020-11-28 19:59:10 +08:00
* @return \Psr\Http\Message\ResponseInterface
2020-11-21 19:53:01 +08:00
*/
2021-06-30 19:27:49 +08:00
public function userChatFile(ResponseInterface $response, Filesystem $filesystem)
2020-11-21 19:53:01 +08:00
{
2020-11-28 19:59:10 +08:00
$params = $this->request->inputs(['cr_id']);
$this->validate($params, [
2020-11-29 17:39:24 +08:00
'cr_id' => 'required|integer'
2020-11-28 19:59:10 +08:00
]);
2020-11-21 19:53:01 +08:00
2021-07-05 21:52:44 +08:00
$recordsInfo = TalkRecords::select(['msg_type', 'talk_type', 'user_id', 'receiver_id'])->where('id', $params['cr_id'])->first();
2020-11-21 19:53:01 +08:00
if (!$recordsInfo) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('文件不存在!');
2020-11-21 19:53:01 +08:00
}
2020-11-28 19:59:10 +08:00
$user_id = $this->uid();
2021-03-16 20:39:31 +08:00
// 判断消息是否是当前用户发送(如果是则跳过权限验证)
2020-11-28 19:59:10 +08:00
if ($recordsInfo->user_id != $user_id) {
2021-07-05 21:52:44 +08:00
if ($recordsInfo->talk_type == 1) {
if ($recordsInfo->receiver_id != $user_id) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('非法请求!');
2020-11-21 19:53:01 +08:00
}
} else {
2021-07-28 23:08:07 +08:00
if (!di()->get(GroupMemberService::class)->isMember($recordsInfo->receiver_id, $user_id)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('非法请求!');
2020-11-21 19:53:01 +08:00
}
}
}
2021-07-05 21:52:44 +08:00
$info = TalkRecordsFile::select(['save_dir', 'original_name'])->where('record_id', $params['cr_id'])->first();
2021-06-30 19:27:49 +08:00
if (!$info || !$filesystem->has($info->save_dir)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('文件不存在或没有下载权限!');
2020-11-21 19:53:01 +08:00
}
2021-06-30 19:27:49 +08:00
return $response->download($this->getFilePath($info->save_dir), $info->original_name);
2020-11-21 19:53:01 +08:00
}
}