2020-11-21 19:53:01 +08:00
|
|
|
<?php
|
2020-12-26 21:33:40 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This is my open source code, please do not use it for commercial applications.
|
|
|
|
*
|
|
|
|
* For the full copyright and license information,
|
|
|
|
* please view the LICENSE file that was distributed with this source code
|
|
|
|
*
|
|
|
|
* @author Yuandong<837215079@qq.com>
|
|
|
|
* @link https://github.com/gzydong/hyperf-chat
|
|
|
|
*/
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
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;
|
2021-03-25 17:32:36 +08:00
|
|
|
use App\Model\Group\Group;
|
2020-11-28 19:59:10 +08:00
|
|
|
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-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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 下载用户聊天文件
|
|
|
|
*
|
|
|
|
* @RequestMapping(path="user-chat-file", methods="get")
|
|
|
|
*
|
2020-11-28 19:59:10 +08:00
|
|
|
* @param ResponseInterface $response
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param UploadService $uploadService
|
|
|
|
*
|
2020-11-28 19:59:10 +08:00
|
|
|
* @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, [
|
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
|
|
|
|
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();
|
|
|
|
|
2021-03-16 20:39:31 +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 {
|
2021-03-25 17:32:36 +08:00
|
|
|
if (!Group::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();
|
2021-03-16 20:39:31 +08:00
|
|
|
if (!$fileInfo || !file_exists($uploadService->driver($fileInfo->save_dir))) {
|
2020-11-21 19:53:01 +08:00
|
|
|
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
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param UploadService $uploadService
|
|
|
|
*
|
2020-11-28 19:59:10 +08:00
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
*/
|
|
|
|
public function articleAnnex(ResponseInterface $response, UploadService $uploadService)
|
|
|
|
{
|
|
|
|
$params = $this->request->inputs(['annex_id']);
|
|
|
|
$this->validate($params, [
|
2020-11-29 17:39:24 +08:00
|
|
|
'annex_id' => 'required|integer'
|
2020-11-28 19:59:10 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$info = ArticleAnnex::select(['save_dir', 'original_name'])
|
|
|
|
->where('id', $params['annex_id'])
|
|
|
|
->where('user_id', $this->uid())
|
|
|
|
->first();
|
|
|
|
|
2021-03-16 20:39:31 +08:00
|
|
|
if (!$info || !file_exists($uploadService->driver($info->save_dir))) {
|
2020-11-28 19:59:10 +08:00
|
|
|
return $this->response->fail('文件不存在或没有下载权限...');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->download($uploadService->driver($info->save_dir), $info->original_name);
|
|
|
|
}
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|