From 16ae0bd32356525bb460adebed4ff9c72252decd Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sun, 26 Feb 2023 22:26:22 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=86=E4=BA=AB=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/FileController.php | 14 ++++++++++++-- app/Models/File.php | 17 +++++++++++++++++ app/Models/FileLink.php | 4 ++-- app/Models/WebSocketDialogMsg.php | 6 +++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index 41de9c038..b5ede8183 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -140,7 +140,12 @@ class FileController extends AbstractController $fileLink = FileLink::whereCode($id)->first(); $file = $fileLink?->file; if (empty($file)) { - return Base::retError('链接不存在'); + $msg = '文件链接不存在'; + $data = File::code2IdName($id); + if ($data) { + $msg = "【{$data->name}】 {$msg}"; + } + return Base::retError($msg, $data); } } else { return Base::retError('参数错误'); @@ -518,7 +523,12 @@ class FileController extends AbstractController $fileLink = FileLink::whereCode($id)->first(); $file = $fileLink?->file; if (empty($file)) { - return Base::retError('链接不存在'); + $msg = '文件链接不存在'; + $data = File::code2IdName($id); + if ($data) { + $msg = "【{$data->name}】 {$msg}"; + } + return Base::retError($msg, $data); } } else { return Base::retError('参数错误'); diff --git a/app/Models/File.php b/app/Models/File.php index 36ae70102..48830485f 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -387,6 +387,23 @@ class File extends AbstractModel return $array; } + /** + * code获取文件ID、名称 + * @param $code + * @return File + */ + public static function code2IdName($code) { + $arr = explode(",", base64_decode($code)); + if (empty($arr)) { + return null; + } + $fileId = intval($arr[0]); + if (empty($fileId)) { + return null; + } + return File::select(['id', 'name'])->find($fileId); + } + /** * 处理返回图片地址 diff --git a/app/Models/FileLink.php b/app/Models/FileLink.php index cc2bdb8e9..027b6f660 100644 --- a/app/Models/FileLink.php +++ b/app/Models/FileLink.php @@ -51,12 +51,12 @@ class FileLink extends AbstractModel $fileLink = FileLink::createInstance([ 'file_id' => $fileId, 'userid' => $userid, - 'code' => Base::generatePassword(64), + 'code' => base64_encode("{$fileId},{$userid}," . Base::generatePassword()), ]); $fileLink->save(); } else { if ($refresh == 'yes') { - $fileLink->code = Base::generatePassword(64); + $fileLink->code = base64_encode("{$fileId},{$userid}," . Base::generatePassword()); $fileLink->save(); } } diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 355e4e77e..8f3f502dc 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -686,7 +686,7 @@ class WebSocketDialogMsg extends AbstractModel $keyId = $fileLink['code']; } else { preg_match("/\/single\/file\/(.*?)$/i", $keyId, $match); - if ($match && strlen($match[1]) >= 32) { + if ($match && strlen($match[1]) >= 8) { $keyId = $match[1]; } else { throw new ApiException('文件分享错误'); @@ -701,7 +701,7 @@ class WebSocketDialogMsg extends AbstractModel $herf = $matchs[2][$key]; $title = $matchs[3][$key] ?: $herf; preg_match("/\/single\/file\/(.*?)$/i", strip_tags($title), $match); - if ($match && strlen($match[1]) >= 32) { + if ($match && strlen($match[1]) >= 8) { $file = File::select(['files.id', 'files.name', 'files.ext'])->join('file_links as L', 'files.id', '=', 'L.file_id')->where('L.code', $match[1])->first(); if ($file && $file->name) { $name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name; @@ -718,7 +718,7 @@ class WebSocketDialogMsg extends AbstractModel if ($matchs) { foreach ($matchs[0] as $str) { preg_match("/\/single\/file\/(.*?)$/i", $str, $match); - if ($match && strlen($match[1]) >= 32) { + if ($match && strlen($match[1]) >= 8) { $file = File::select(['files.id', 'files.name', 'files.ext'])->join('file_links as L', 'files.id', '=', 'L.file_id')->where('L.code', $match[1])->first(); if ($file && $file->name) { $name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name;