perf: 优化文件分享链接

This commit is contained in:
kuaifan 2023-02-26 22:26:22 +08:00
parent a21c3d640f
commit 16ae0bd323
4 changed files with 34 additions and 7 deletions

View File

@ -140,7 +140,12 @@ class FileController extends AbstractController
$fileLink = FileLink::whereCode($id)->first(); $fileLink = FileLink::whereCode($id)->first();
$file = $fileLink?->file; $file = $fileLink?->file;
if (empty($file)) { if (empty($file)) {
return Base::retError('链接不存在'); $msg = '文件链接不存在';
$data = File::code2IdName($id);
if ($data) {
$msg = "{$data->name}{$msg}";
}
return Base::retError($msg, $data);
} }
} else { } else {
return Base::retError('参数错误'); return Base::retError('参数错误');
@ -518,7 +523,12 @@ class FileController extends AbstractController
$fileLink = FileLink::whereCode($id)->first(); $fileLink = FileLink::whereCode($id)->first();
$file = $fileLink?->file; $file = $fileLink?->file;
if (empty($file)) { if (empty($file)) {
return Base::retError('链接不存在'); $msg = '文件链接不存在';
$data = File::code2IdName($id);
if ($data) {
$msg = "{$data->name}{$msg}";
}
return Base::retError($msg, $data);
} }
} else { } else {
return Base::retError('参数错误'); return Base::retError('参数错误');

View File

@ -387,6 +387,23 @@ class File extends AbstractModel
return $array; 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);
}
/** /**
* 处理返回图片地址 * 处理返回图片地址

View File

@ -51,12 +51,12 @@ class FileLink extends AbstractModel
$fileLink = FileLink::createInstance([ $fileLink = FileLink::createInstance([
'file_id' => $fileId, 'file_id' => $fileId,
'userid' => $userid, 'userid' => $userid,
'code' => Base::generatePassword(64), 'code' => base64_encode("{$fileId},{$userid}," . Base::generatePassword()),
]); ]);
$fileLink->save(); $fileLink->save();
} else { } else {
if ($refresh == 'yes') { if ($refresh == 'yes') {
$fileLink->code = Base::generatePassword(64); $fileLink->code = base64_encode("{$fileId},{$userid}," . Base::generatePassword());
$fileLink->save(); $fileLink->save();
} }
} }

View File

@ -686,7 +686,7 @@ class WebSocketDialogMsg extends AbstractModel
$keyId = $fileLink['code']; $keyId = $fileLink['code'];
} else { } else {
preg_match("/\/single\/file\/(.*?)$/i", $keyId, $match); preg_match("/\/single\/file\/(.*?)$/i", $keyId, $match);
if ($match && strlen($match[1]) >= 32) { if ($match && strlen($match[1]) >= 8) {
$keyId = $match[1]; $keyId = $match[1];
} else { } else {
throw new ApiException('文件分享错误'); throw new ApiException('文件分享错误');
@ -701,7 +701,7 @@ class WebSocketDialogMsg extends AbstractModel
$herf = $matchs[2][$key]; $herf = $matchs[2][$key];
$title = $matchs[3][$key] ?: $herf; $title = $matchs[3][$key] ?: $herf;
preg_match("/\/single\/file\/(.*?)$/i", strip_tags($title), $match); 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(); $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) { if ($file && $file->name) {
$name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name; $name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name;
@ -718,7 +718,7 @@ class WebSocketDialogMsg extends AbstractModel
if ($matchs) { if ($matchs) {
foreach ($matchs[0] as $str) { foreach ($matchs[0] as $str) {
preg_match("/\/single\/file\/(.*?)$/i", $str, $match); 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(); $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) { if ($file && $file->name) {
$name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name; $name = $file->ext ? "{$file->name}.{$file->ext}" : $file->name;