优化代码
parent
4481ba558a
commit
4590728cb1
|
@ -24,6 +24,7 @@ use App\Model\Group\GroupMember;
|
||||||
use App\Service\TalkService;
|
use App\Service\TalkService;
|
||||||
use Hyperf\Command\Command as HyperfCommand;
|
use Hyperf\Command\Command as HyperfCommand;
|
||||||
use Hyperf\Command\Annotation\Command;
|
use Hyperf\Command\Annotation\Command;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -173,5 +174,7 @@ class TestCommand extends HyperfCommand
|
||||||
//var_dump(FriendRemark::getInstance());
|
//var_dump(FriendRemark::getInstance());
|
||||||
|
|
||||||
//var_dump(Group::isManager(2054,116));
|
//var_dump(Group::isManager(2054,116));
|
||||||
|
|
||||||
|
var_dump(pathinfo('spring-boot相关',PATHINFO_EXTENSION));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,7 @@ use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
use Hyperf\HttpServer\Annotation\Middleware;
|
use Hyperf\HttpServer\Annotation\Middleware;
|
||||||
use App\Middleware\JWTAuthMiddleware;
|
use App\Middleware\JWTAuthMiddleware;
|
||||||
use App\Service\ArticleService;
|
use App\Service\ArticleService;
|
||||||
use App\Service\UploadService;
|
use League\Flysystem\Filesystem;
|
||||||
use Hyperf\Utils\Str;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use App\Cache\Repository\LockRedis;
|
use App\Cache\Repository\LockRedis;
|
||||||
|
|
||||||
|
@ -36,12 +35,6 @@ class ArticleController extends CController
|
||||||
*/
|
*/
|
||||||
private $articleService;
|
private $articleService;
|
||||||
|
|
||||||
/**
|
|
||||||
* @inject
|
|
||||||
* @var UploadService
|
|
||||||
*/
|
|
||||||
private $uploadService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取笔记分类列表
|
* 获取笔记分类列表
|
||||||
* @RequestMapping(path="article-class", methods="get")
|
* @RequestMapping(path="article-class", methods="get")
|
||||||
|
@ -180,7 +173,6 @@ class ArticleController extends CController
|
||||||
|
|
||||||
$lockKey = "article:sort_{$params['class_id']}_{$params['sort_type']}";
|
$lockKey = "article:sort_{$params['class_id']}_{$params['sort_type']}";
|
||||||
|
|
||||||
// 获取Redis锁
|
|
||||||
$lock = LockRedis::getInstance();
|
$lock = LockRedis::getInstance();
|
||||||
if ($lock->lock($lockKey, 3, 500)) {
|
if ($lock->lock($lockKey, 3, 500)) {
|
||||||
$isTrue = $this->articleService->articleClassSort($this->uid(), (int)$params['class_id'], (int)$params['sort_type']);
|
$isTrue = $this->articleService->articleClassSort($this->uid(), (int)$params['class_id'], (int)$params['sort_type']);
|
||||||
|
@ -332,9 +324,10 @@ class ArticleController extends CController
|
||||||
* 笔记图片上传接口
|
* 笔记图片上传接口
|
||||||
* @RequestMapping(path="upload-article-image", methods="post")
|
* @RequestMapping(path="upload-article-image", methods="post")
|
||||||
*
|
*
|
||||||
|
* @param Filesystem $filesystem
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function uploadArticleImage()
|
public function uploadArticleImage(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$file = $this->request->file('image');
|
$file = $this->request->file('image');
|
||||||
if (!$file || !$file->isValid()) {
|
if (!$file || !$file->isValid()) {
|
||||||
|
@ -346,10 +339,10 @@ class ArticleController extends CController
|
||||||
return $this->response->fail('图片格式错误,目前仅支持jpg、png、jpeg、gif和webp');
|
return $this->response->fail('图片格式错误,目前仅支持jpg、png、jpeg、gif和webp');
|
||||||
}
|
}
|
||||||
|
|
||||||
$imgInfo = getimagesize($file->getRealPath());
|
try {
|
||||||
|
$path = 'media/images/notes/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
|
||||||
$path = $this->uploadService->media($file, 'media/images/notes/', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
||||||
if (!$path) {
|
} catch (\Exception $e) {
|
||||||
return $this->response->fail();
|
return $this->response->fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,7 +449,7 @@ class ArticleController extends CController
|
||||||
*
|
*
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function uploadArticleAnnex()
|
public function uploadArticleAnnex(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['article_id']);
|
$params = $this->request->inputs(['article_id']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
|
@ -469,23 +462,22 @@ class ArticleController extends CController
|
||||||
}
|
}
|
||||||
|
|
||||||
$annex = [
|
$annex = [
|
||||||
'file_suffix' => $file->getExtension(),
|
'file_suffix' => pathinfo($file->getClientFilename(), PATHINFO_EXTENSION),
|
||||||
'file_size' => $file->getSize(),
|
'file_size' => $file->getSize(),
|
||||||
'save_dir' => '',
|
'save_dir' => '',
|
||||||
'original_name' => $file->getClientFilename()
|
'original_name' => $file->getClientFilename()
|
||||||
];
|
];
|
||||||
|
|
||||||
$path = $this->uploadService->media($file,
|
try {
|
||||||
'files/notes/' . date('Ymd'),
|
$path = 'files/notes/' . date('Ymd') . '/' . "[{$annex['file_suffix']}]" . create_random_filename('tmp');
|
||||||
"[{$annex['file_suffix']}]" . uniqid() . Str::random(16) . '.' . 'tmp'
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
||||||
);
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
if (!$path) {
|
|
||||||
return $this->response->fail();
|
return $this->response->fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
$annex['save_dir'] = $path;
|
$annex['save_dir'] = $path;
|
||||||
$annex['id'] = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex);
|
$annex['id'] = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex);
|
||||||
|
|
||||||
if (!$annex['id']) {
|
if (!$annex['id']) {
|
||||||
return $this->response->fail('附件上传失败,请稍后再试!');
|
return $this->response->fail('附件上传失败,请稍后再试!');
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,8 @@ use App\Model\Article\ArticleAnnex;
|
||||||
use App\Model\Chat\ChatRecord;
|
use App\Model\Chat\ChatRecord;
|
||||||
use App\Model\Chat\ChatRecordsFile;
|
use App\Model\Chat\ChatRecordsFile;
|
||||||
use App\Model\Group\Group;
|
use App\Model\Group\Group;
|
||||||
use App\Service\UploadService;
|
|
||||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DownloadController
|
* Class DownloadController
|
||||||
|
@ -31,15 +31,20 @@ use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||||
*/
|
*/
|
||||||
class DownloadController extends CController
|
class DownloadController extends CController
|
||||||
{
|
{
|
||||||
|
private function getFilePath(string $path)
|
||||||
|
{
|
||||||
|
return container()->get(Filesystem::class)->getConfig()->get('root') . '/' . $path;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载用户聊天文件
|
* 下载用户聊天文件
|
||||||
* @RequestMapping(path="user-chat-file", methods="get")
|
* @RequestMapping(path="user-chat-file", methods="get")
|
||||||
*
|
*
|
||||||
* @param ResponseInterface $response
|
* @param ResponseInterface $response
|
||||||
* @param UploadService $uploadService
|
* @param Filesystem $filesystem
|
||||||
* @return \Psr\Http\Message\ResponseInterface
|
* @return \Psr\Http\Message\ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function userChatFile(ResponseInterface $response, UploadService $uploadService)
|
public function userChatFile(ResponseInterface $response, Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['cr_id']);
|
$params = $this->request->inputs(['cr_id']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
|
@ -66,12 +71,12 @@ class DownloadController extends CController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$fileInfo = ChatRecordsFile::select(['save_dir', 'original_name'])->where('record_id', $params['cr_id'])->first();
|
$info = ChatRecordsFile::select(['save_dir', 'original_name'])->where('record_id', $params['cr_id'])->first();
|
||||||
if (!$fileInfo || !file_exists($uploadService->driver($fileInfo->save_dir))) {
|
if (!$info || !$filesystem->has($info->save_dir)) {
|
||||||
return $this->response->fail('文件不存在或没有下载权限!');
|
return $this->response->fail('文件不存在或没有下载权限!');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response->download($uploadService->driver($fileInfo->save_dir), $fileInfo->original_name);
|
return $response->download($this->getFilePath($info->save_dir), $info->original_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,10 +84,10 @@ class DownloadController extends CController
|
||||||
* @RequestMapping(path="article-annex", methods="get")
|
* @RequestMapping(path="article-annex", methods="get")
|
||||||
*
|
*
|
||||||
* @param ResponseInterface $response
|
* @param ResponseInterface $response
|
||||||
* @param UploadService $uploadService
|
* @param Filesystem $filesystem
|
||||||
* @return \Psr\Http\Message\ResponseInterface
|
* @return \Psr\Http\Message\ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function articleAnnex(ResponseInterface $response, UploadService $uploadService)
|
public function articleAnnex(ResponseInterface $response, Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['annex_id']);
|
$params = $this->request->inputs(['annex_id']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
|
@ -94,10 +99,10 @@ class DownloadController extends CController
|
||||||
->where('user_id', $this->uid())
|
->where('user_id', $this->uid())
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$info || !file_exists($uploadService->driver($info->save_dir))) {
|
if (!$info || !$filesystem->has($info->save_dir)) {
|
||||||
return $this->response->fail('文件不存在或没有下载权限!');
|
return $this->response->fail('文件不存在或没有下载权限!');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response->download($uploadService->driver($info->save_dir), $info->original_name);
|
return $response->download($this->getFilePath($info->save_dir), $info->original_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ use App\Constants\ResponseCode;
|
||||||
use App\Model\Emoticon;
|
use App\Model\Emoticon;
|
||||||
use App\Model\EmoticonDetail;
|
use App\Model\EmoticonDetail;
|
||||||
use App\Service\EmoticonService;
|
use App\Service\EmoticonService;
|
||||||
use App\Service\UploadService;
|
use League\Flysystem\Filesystem;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,10 +143,10 @@ class EmoticonController extends CController
|
||||||
* 自定义上传表情包
|
* 自定义上传表情包
|
||||||
* @RequestMapping(path="upload-emoticon", methods="post")
|
* @RequestMapping(path="upload-emoticon", methods="post")
|
||||||
*
|
*
|
||||||
* @param UploadService $uploadService
|
* @param Filesystem $filesystem
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function uploadEmoticon(UploadService $uploadService)
|
public function uploadEmoticon(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$file = $this->request->file('emoticon');
|
$file = $this->request->file('emoticon');
|
||||||
if (!$file->isValid()) {
|
if (!$file->isValid()) {
|
||||||
|
@ -164,20 +164,16 @@ class EmoticonController extends CController
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取图片信息
|
try {
|
||||||
$imgInfo = @getimagesize($file->getRealPath());
|
$path = 'media/images/emoticon/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
|
||||||
if (!$imgInfo) {
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
||||||
return $this->response->fail('表情包上传失败!');
|
} catch (\Exception $e) {
|
||||||
}
|
|
||||||
|
|
||||||
$save_path = $uploadService->media($file, 'media/images/emoticon', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
|
|
||||||
if (!$save_path) {
|
|
||||||
return $this->response->fail('图片上传失败!');
|
return $this->response->fail('图片上传失败!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = EmoticonDetail::create([
|
$result = EmoticonDetail::create([
|
||||||
'user_id' => $this->uid(),
|
'user_id' => $this->uid(),
|
||||||
'url' => $save_path,
|
'url' => $path,
|
||||||
'file_suffix' => $ext,
|
'file_suffix' => $ext,
|
||||||
'file_size' => $file->getSize(),
|
'file_size' => $file->getSize(),
|
||||||
'created_at' => time()
|
'created_at' => time()
|
||||||
|
|
|
@ -17,7 +17,6 @@ use Hyperf\HttpServer\Annotation\Controller;
|
||||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
use Hyperf\HttpServer\Annotation\Middleware;
|
use Hyperf\HttpServer\Annotation\Middleware;
|
||||||
use App\Middleware\JWTAuthMiddleware;
|
use App\Middleware\JWTAuthMiddleware;
|
||||||
use Hyperf\Utils\Str;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use App\Model\EmoticonDetail;
|
use App\Model\EmoticonDetail;
|
||||||
use App\Model\FileSplitUpload;
|
use App\Model\FileSplitUpload;
|
||||||
|
@ -26,9 +25,9 @@ use App\Model\UsersChatList;
|
||||||
use App\Model\UsersFriend;
|
use App\Model\UsersFriend;
|
||||||
use App\Model\Group\Group;
|
use App\Model\Group\Group;
|
||||||
use App\Service\TalkService;
|
use App\Service\TalkService;
|
||||||
use App\Service\UploadService;
|
|
||||||
use App\Amqp\Producer\ChatMessageProducer;
|
use App\Amqp\Producer\ChatMessageProducer;
|
||||||
use App\Constants\SocketConstants;
|
use App\Constants\SocketConstants;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class TalkController
|
* Class TalkController
|
||||||
|
@ -475,10 +474,10 @@ class TalkController extends CController
|
||||||
* 上传聊天对话图片(待优化)
|
* 上传聊天对话图片(待优化)
|
||||||
* @RequestMapping(path="send-image", methods="post")
|
* @RequestMapping(path="send-image", methods="post")
|
||||||
*
|
*
|
||||||
* @param UploadService $uploadService
|
* @param Filesystem $filesystem
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function sendImage(UploadService $uploadService)
|
public function sendImage(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['source', 'receive_id']);
|
$params = $this->request->inputs(['source', 'receive_id']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
|
@ -496,11 +495,10 @@ class TalkController extends CController
|
||||||
return $this->response->fail('图片格式错误,目前仅支持jpg、png、jpeg、gif和webp');
|
return $this->response->fail('图片格式错误,目前仅支持jpg、png、jpeg、gif和webp');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取图片信息
|
try {
|
||||||
$imgInfo = getimagesize($file->getRealPath());
|
$path = 'media/images/talks/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
|
||||||
|
$filesystem->write($path, file_get_contents($file->getRealPath()));
|
||||||
$path = $uploadService->media($file, 'media/images/talks', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
|
} catch (\Exception $e) {
|
||||||
if (!$path) {
|
|
||||||
return $this->response->fail();
|
return $this->response->fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,10 +591,10 @@ class TalkController extends CController
|
||||||
* 发送文件消息
|
* 发送文件消息
|
||||||
* @RequestMapping(path="send-file", methods="post")
|
* @RequestMapping(path="send-file", methods="post")
|
||||||
*
|
*
|
||||||
* @param UploadService $uploadService
|
* @param Filesystem $filesystem
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function sendFile(UploadService $uploadService)
|
public function sendFile(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['hash_name', 'receive_id', 'source']);
|
$params = $this->request->inputs(['hash_name', 'receive_id', 'source']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
|
@ -612,12 +610,13 @@ class TalkController extends CController
|
||||||
return $this->response->fail('文件不存在...');
|
return $this->response->fail('文件不存在...');
|
||||||
}
|
}
|
||||||
|
|
||||||
$file_hash_name = uniqid() . Str::random(10) . '.' . $file->file_ext;
|
$save_dir = "files/talks/" . date('Ymd') . '/' . create_random_filename($file->file_ext);
|
||||||
$save_dir = "files/talks/" . date('Ymd') . '/' . $file_hash_name;
|
|
||||||
|
|
||||||
$uploadService->makeDirectory($uploadService->driver("files/talks/" . date('Ymd')));
|
try {
|
||||||
|
$filesystem->copy($file->save_dir, $save_dir);
|
||||||
@copy($uploadService->driver($file->save_dir), $uploadService->driver($save_dir));
|
} catch (\Exception $e) {
|
||||||
|
return $this->response->fail('文件不存在...');
|
||||||
|
}
|
||||||
|
|
||||||
$record_id = $this->talkService->createFileMessage([
|
$record_id = $this->talkService->createFileMessage([
|
||||||
'source' => $params['source'],
|
'source' => $params['source'],
|
||||||
|
|
|
@ -17,12 +17,11 @@ use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
use Hyperf\HttpServer\Annotation\Middleware;
|
use Hyperf\HttpServer\Annotation\Middleware;
|
||||||
use App\Middleware\JWTAuthMiddleware;
|
use App\Middleware\JWTAuthMiddleware;
|
||||||
use App\Service\SplitUploadService;
|
use App\Service\SplitUploadService;
|
||||||
use App\Service\UploadService;
|
use League\Flysystem\Filesystem;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传控制器
|
* 上传文件控制器
|
||||||
* Class UploadController
|
|
||||||
* @Controller(path="/api/v1/upload")
|
* @Controller(path="/api/v1/upload")
|
||||||
* @Middleware(JWTAuthMiddleware::class)
|
* @Middleware(JWTAuthMiddleware::class)
|
||||||
*
|
*
|
||||||
|
@ -30,12 +29,6 @@ use Psr\Http\Message\ResponseInterface;
|
||||||
*/
|
*/
|
||||||
class UploadController extends CController
|
class UploadController extends CController
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @inject
|
|
||||||
* @var UploadService
|
|
||||||
*/
|
|
||||||
private $uploadService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inject
|
* @inject
|
||||||
* @var SplitUploadService
|
* @var SplitUploadService
|
||||||
|
@ -48,14 +41,19 @@ class UploadController extends CController
|
||||||
*
|
*
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function fileStream()
|
public function fileStream(Filesystem $filesystem)
|
||||||
{
|
{
|
||||||
$fileStream = $this->request->post('fileStream', '');
|
$fileStream = $this->request->post('fileStream', '');
|
||||||
$data = base64_decode(str_replace(['data:image/png;base64,', ' '], ['', '+'], $fileStream));
|
if (empty($fileStream)) {
|
||||||
|
return $this->response->fail();
|
||||||
|
}
|
||||||
|
|
||||||
$path = '/media/images/avatar/' . date('Ymd') . '/' . uniqid() . date('His') . '.png';
|
$path = 'media/images/avatar/' . date('Ymd') . '/' . create_random_filename('png');
|
||||||
$this->uploadService->makeDirectory($this->uploadService->driver('/media/images/avatar/' . date('Ymd') . '/'));
|
try {
|
||||||
@file_put_contents($this->uploadService->driver($path), $data);
|
$filesystem->write($path, base64_decode(str_replace(['data:image/png;base64,', ' '], ['', '+'], $fileStream)));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->response->fail();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->response->success(['avatar' => get_media_url($path)]);
|
return $this->response->success(['avatar' => get_media_url($path)]);
|
||||||
}
|
}
|
||||||
|
@ -105,7 +103,7 @@ class UploadController extends CController
|
||||||
$user_id = $this->uid();
|
$user_id = $this->uid();
|
||||||
$uploadRes = $this->splitUploadService->upload($user_id, $file, $params['hash'], intval($params['split_index']), intval($params['size']));
|
$uploadRes = $this->splitUploadService->upload($user_id, $file, $params['hash'], intval($params['split_index']), intval($params['size']));
|
||||||
if (!$uploadRes) {
|
if (!$uploadRes) {
|
||||||
return $this->response->fail('上传文件失败!');
|
return $this->response->fail('上传文件失败111!');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($params['split_index'] + 1) == $params['split_num']) {
|
if (($params['split_index'] + 1) == $params['split_num']) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Service;
|
||||||
use App\Model\FileSplitUpload;
|
use App\Model\FileSplitUpload;
|
||||||
use Hyperf\Utils\Str;
|
use Hyperf\Utils\Str;
|
||||||
use Hyperf\HttpMessage\Upload\UploadedFile;
|
use Hyperf\HttpMessage\Upload\UploadedFile;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件拆分上传服务
|
* 文件拆分上传服务
|
||||||
|
@ -63,18 +64,12 @@ class SplitUploadService
|
||||||
->where([['user_id', '=', $user_id], ['hash_name', '=', $hashName], ['file_type', '=', 1]])
|
->where([['user_id', '=', $user_id], ['hash_name', '=', $hashName], ['file_type', '=', 1]])
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$fileInfo) {
|
if (!$fileInfo) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 保存文件名及保存文件相对目录
|
$save_path = "tmp/{$hashName}/" . "{$hashName}_{$split_index}_{$fileInfo->file_ext}.tmp";
|
||||||
$fileName = "{$hashName}_{$split_index}_{$fileInfo->file_ext}.tmp";
|
try {
|
||||||
$uploadService = make(UploadService::class);
|
container()->get(Filesystem::class)->write($save_path, file_get_contents($file->getRealPath()));
|
||||||
|
} catch (\Exception $e) {
|
||||||
$uploadService->makeDirectory($uploadService->driver("tmp/{$hashName}"));
|
|
||||||
$file->moveTo(sprintf('%s/%s', $uploadService->driver("tmp/{$hashName}"), $fileName));
|
|
||||||
|
|
||||||
if (!$file->isMoved()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +82,7 @@ class SplitUploadService
|
||||||
'original_name' => $fileInfo->original_name,
|
'original_name' => $fileInfo->original_name,
|
||||||
'split_index' => $split_index,
|
'split_index' => $split_index,
|
||||||
'split_num' => $fileInfo->split_num,
|
'split_num' => $fileInfo->split_num,
|
||||||
'save_dir' => sprintf('%s/%s', "tmp/{$hashName}", $fileName),
|
'save_dir' => $save_path,
|
||||||
'file_ext' => $fileInfo->file_ext,
|
'file_ext' => $fileInfo->file_ext,
|
||||||
'file_size' => $fileSize,
|
'file_size' => $fileSize,
|
||||||
'upload_at' => time(),
|
'upload_at' => time(),
|
||||||
|
@ -112,9 +107,7 @@ class SplitUploadService
|
||||||
->where('file_type', 1)
|
->where('file_type', 1)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$fileInfo) {
|
if (!$fileInfo) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$files = FileSplitUpload::where('user_id', $user_id)
|
$files = FileSplitUpload::where('user_id', $user_id)
|
||||||
->where('hash_name', $hash_name)
|
->where('hash_name', $hash_name)
|
||||||
|
@ -122,30 +115,27 @@ class SplitUploadService
|
||||||
->orderBy('split_index', 'asc')
|
->orderBy('split_index', 'asc')
|
||||||
->get(['split_index', 'save_dir'])->toArray();
|
->get(['split_index', 'save_dir'])->toArray();
|
||||||
|
|
||||||
if (!$files) {
|
if (!$files || count($files) != $fileInfo->split_num) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($files) != $fileInfo->split_num) {
|
$file_merge = "tmp/{$hash_name}/{$fileInfo->original_name}.tmp";
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$fileMerge = "tmp/{$hash_name}/{$fileInfo->original_name}.tmp";
|
$filesystem = container()->get(Filesystem::class);
|
||||||
$uploadService = make(UploadService::class);
|
$root_path = $filesystem->getConfig()->get('root');
|
||||||
|
|
||||||
// 文件合并
|
|
||||||
$merge_save_path = $uploadService->driver($fileMerge);
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
file_put_contents($merge_save_path, file_get_contents($uploadService->driver($file['save_dir'])), FILE_APPEND);
|
file_put_contents(
|
||||||
|
"{$root_path}/{$file_merge}",
|
||||||
|
$filesystem->read($file['save_dir']),
|
||||||
|
FILE_APPEND
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSplitUpload::select(['id', 'original_name', 'split_num', 'file_ext', 'file_size'])
|
FileSplitUpload::select(['id', 'original_name', 'split_num', 'file_ext', 'file_size'])
|
||||||
->where('user_id', $user_id)->where('hash_name', $hash_name)
|
->where('user_id', $user_id)->where('hash_name', $hash_name)
|
||||||
->where('file_type', 1)
|
->where('file_type', 1)
|
||||||
->update(['save_dir' => $fileMerge]);
|
->update(['save_dir' => $file_merge]);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'path' => $fileMerge,
|
'path' => $file_merge,
|
||||||
'tmp_file_name' => "{$fileInfo->original_name}.tmp",
|
'tmp_file_name' => "{$fileInfo->original_name}.tmp",
|
||||||
'original_name' => $fileInfo->original_name,
|
'original_name' => $fileInfo->original_name,
|
||||||
'file_size' => $fileInfo->file_size
|
'file_size' => $fileInfo->file_size
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Service;
|
|
||||||
|
|
||||||
use Hyperf\HttpMessage\Upload\UploadedFile;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传服务
|
|
||||||
*
|
|
||||||
* @package App\Service
|
|
||||||
*/
|
|
||||||
class UploadService extends BaseService
|
|
||||||
{
|
|
||||||
public function driver($dir)
|
|
||||||
{
|
|
||||||
return sprintf('%s/%s', rtrim(config('upload_dir'), '/'), trim($dir, '/'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建文件夹
|
|
||||||
*
|
|
||||||
* @param string $dir 文件夹路径
|
|
||||||
*/
|
|
||||||
public function makeDirectory(string $dir)
|
|
||||||
{
|
|
||||||
if (!file_exists($dir)) @mkdir($dir, 0777, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传媒体图片
|
|
||||||
*
|
|
||||||
* @param UploadedFile $file
|
|
||||||
* @param string $dir 文件夹路径
|
|
||||||
* @param string $filename 文件名称
|
|
||||||
* @return bool|string
|
|
||||||
*/
|
|
||||||
public function media(UploadedFile $file, string $dir, string $filename)
|
|
||||||
{
|
|
||||||
$save_dir = $this->driver($dir);
|
|
||||||
|
|
||||||
$this->makeDirectory($save_dir);
|
|
||||||
|
|
||||||
$file->moveTo(sprintf('%s/%s', $save_dir, $filename));
|
|
||||||
|
|
||||||
if ($file->isMoved()) {
|
|
||||||
@chmod(sprintf('%s/%s', $save_dir, $filename), 0644);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $file->isMoved() ? sprintf('%s/%s', trim($dir, '/'), $filename) : false;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -49,7 +49,8 @@ class MailerTemplate
|
||||||
public function errorNotice(Throwable $throwable)
|
public function errorNotice(Throwable $throwable)
|
||||||
{
|
{
|
||||||
return $this->view(config('view.engine'), 'emails.error-notice', [
|
return $this->view(config('view.engine'), 'emails.error-notice', [
|
||||||
'throwable' => $throwable->getTraceAsString()
|
'throwable' => $throwable->getTraceAsString(),
|
||||||
|
'message' => $throwable->getMessage()
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,13 +181,12 @@ function get_media_url(string $path)
|
||||||
* 随机生成图片名
|
* 随机生成图片名
|
||||||
*
|
*
|
||||||
* @param string $ext 图片后缀名
|
* @param string $ext 图片后缀名
|
||||||
* @param int $width 图片宽度
|
* @param array $filesize 图片文件大小信息
|
||||||
* @param int $height 图片高度
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function create_image_name(string $ext, int $width, int $height)
|
function create_image_name(string $ext, array $filesize)
|
||||||
{
|
{
|
||||||
return uniqid() . Str::random() . '_' . $width . 'x' . $height . '.' . $ext;
|
return uniqid() . Str::random() . '_' . $filesize[0] . 'x' . $filesize[1] . '.' . $ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -255,4 +254,14 @@ function push_amqp(ProducerMessage $message, bool $confirm = false, int $timeout
|
||||||
return container()->get(Producer::class)->produce($message, $confirm, $timeout);
|
return container()->get(Producer::class)->produce($message, $confirm, $timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机文件名
|
||||||
|
*
|
||||||
|
* @param string $ext 文件后缀名
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function create_random_filename(string $ext)
|
||||||
|
{
|
||||||
|
$ext = $ext ? '.' . $ext : '';
|
||||||
|
return Str::random(10) . uniqid() . $ext;
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ return [
|
||||||
'storage' => [
|
'storage' => [
|
||||||
'local' => [
|
'local' => [
|
||||||
'driver' => \Hyperf\Filesystem\Adapter\LocalAdapterFactory::class,
|
'driver' => \Hyperf\Filesystem\Adapter\LocalAdapterFactory::class,
|
||||||
'root' => __DIR__ . '/../../runtime',
|
'root' => env('UPLOAD_PATH', __DIR__ . '/../../runtime'),
|
||||||
],
|
],
|
||||||
'ftp' => [
|
'ftp' => [
|
||||||
'driver' => \Hyperf\Filesystem\Adapter\FtpAdapterFactory::class,
|
'driver' => \Hyperf\Filesystem\Adapter\FtpAdapterFactory::class,
|
||||||
|
|
|
@ -21,12 +21,10 @@ return [
|
||||||
|
|
||||||
// 域名相关配置
|
// 域名相关配置
|
||||||
'domain' => [
|
'domain' => [
|
||||||
'web_url' => env('WEB_URL', ''),//Web 端首页地址
|
'web_url' => env('WEB_URL', ''),// Web 端首页地址
|
||||||
'img_url' => env('IMG_URL', ''),//设置文件图片访问的域名
|
'img_url' => env('IMG_URL', ''),// 设置文件图片访问的域名
|
||||||
],
|
],
|
||||||
|
|
||||||
'upload_dir' => env('UPLOAD_PATH', ''),
|
|
||||||
|
|
||||||
// 管理员邮箱
|
// 管理员邮箱
|
||||||
'admin_email' => env('ADMIN_EMAIL', ''),
|
'admin_email' => env('ADMIN_EMAIL', ''),
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@
|
||||||
</strong>
|
</strong>
|
||||||
</div>
|
</div>
|
||||||
<div style="margin-bottom:30px;">
|
<div style="margin-bottom:30px;">
|
||||||
|
<p>{{$message}}</p>
|
||||||
<pre>{{$throwable}}</pre>
|
<pre>{{$throwable}}</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue