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

99 lines
3.2 KiB
PHP
Raw Normal View History

2020-11-21 19:53:01 +08:00
<?php
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;
use App\Middleware\JWTAuthMiddleware;
2020-11-28 19:59:10 +08:00
use App\Model\Article\ArticleAnnex;
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsFile;
use App\Model\Group\UsersGroup;
use App\Service\UploadService;
use Hyperf\HttpServer\Contract\ResponseInterface;
2020-11-21 19:53:01 +08:00
/**
* Class DownloadController
*
* @Controller(path="/api/v1/download")
2020-11-28 19:59:10 +08:00
* @Middleware(JWTAuthMiddleware::class)
2020-11-21 19:53:01 +08:00
*
* @package App\Controller\Api\V1
*/
class DownloadController extends CController
{
/**
* 下载用户聊天文件
*
* @RequestMapping(path="user-chat-file", methods="get")
*
2020-11-28 19:59:10 +08:00
* @param ResponseInterface $response
* @param UploadService $uploadService
* @return \Psr\Http\Message\ResponseInterface
2020-11-21 19:53:01 +08:00
*/
public function userChatFile(ResponseInterface $response, UploadService $uploadService)
{
2020-11-28 19:59:10 +08:00
$params = $this->request->inputs(['cr_id']);
$this->validate($params, [
'cr_id' => 'required|integer',
]);
2020-11-21 19:53:01 +08:00
2020-11-28 19:59:10 +08:00
$recordsInfo = ChatRecord::select(['msg_type', 'source', 'user_id', 'receive_id'])->where('id', $params['cr_id'])->first();
2020-11-21 19:53:01 +08:00
if (!$recordsInfo) {
return $this->response->fail('文件不存在...');
}
2020-11-28 19:59:10 +08:00
$user_id = $this->uid();
2020-11-21 19:53:01 +08:00
//判断消息是否是当前用户发送(如果是则跳过权限验证)
2020-11-28 19:59:10 +08:00
if ($recordsInfo->user_id != $user_id) {
2020-11-21 19:53:01 +08:00
if ($recordsInfo->source == 1) {
2020-11-28 19:59:10 +08:00
if ($recordsInfo->receive_id != $user_id) {
2020-11-21 19:53:01 +08:00
return $this->response->fail('非法请求...');
}
} else {
2020-11-28 19:59:10 +08:00
if (!UsersGroup::isMember($recordsInfo->receive_id, $user_id)) {
2020-11-21 19:53:01 +08:00
return $this->response->fail('非法请求...');
}
}
}
2020-11-28 19:59:10 +08:00
$fileInfo = ChatRecordsFile::select(['save_dir', 'original_name'])->where('record_id', $params['cr_id'])->first();
2020-11-21 19:53:01 +08:00
if (!$fileInfo) {
return $this->response->fail('文件不存在或没有下载权限...');
}
return $response->download($uploadService->driver($fileInfo->save_dir), $fileInfo->original_name);
}
2020-11-28 19:59:10 +08:00
/**
* 下载笔记附件
*
* @RequestMapping(path="article-annex", methods="get")
*
* @param ResponseInterface $response
* @param UploadService $uploadService
* @return \Psr\Http\Message\ResponseInterface
*/
public function articleAnnex(ResponseInterface $response, UploadService $uploadService)
{
$params = $this->request->inputs(['annex_id']);
$this->validate($params, [
'annex_id' => 'required|integer',
]);
$info = ArticleAnnex::select(['save_dir', 'original_name'])
->where('id', $params['annex_id'])
->where('user_id', $this->uid())
->first();
if (!$info) {
return $this->response->fail('文件不存在或没有下载权限...');
}
return $response->download($uploadService->driver($info->save_dir), $info->original_name);
}
2020-11-21 19:53:01 +08:00
}