From 36f4e317cf45a1abbd5e3ba6bca5b69da8304551 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Fri, 11 Apr 2025 09:41:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E9=99=90=E5=88=B6=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev | 3 +++ app/admin/common.php | 38 ++++++++++++++++++++++++++++++ app/admin/controller/v1/Upload.php | 9 ++++--- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/.env.dev b/.env.dev index 04e48005..7766d317 100644 --- a/.env.dev +++ b/.env.dev @@ -24,3 +24,6 @@ WHITE_LIST[] = v1/user/captcha # 不需记录日志的接口 [ADMIN_API] IGNORE_LOGGING_LIST[] = v1/OperateLog/index +MAX_IMAGE_SIZE = 5mb # 图片上传最大限制 +MAX_VIDEO_SIZE = 150mb # 视频上传最大限制 +MAX_ATTACHMENT_SIZE = 100mb # 附件上传最大限制 diff --git a/app/admin/common.php b/app/admin/common.php index 0e40ab3f..3152587f 100644 --- a/app/admin/common.php +++ b/app/admin/common.php @@ -94,4 +94,42 @@ if (!function_exists('xlsx_writer')) { ]); return $response; } +} + +if (!function_exists('strtobytes')) { + + /** + * 字符串转字节 + * @param string $str_size 字符串 例:'1MB' '2GB' + * @return int + */ + function strtobytes(string $str_size): int + { + preg_match('/(\d+)(\w+)/', $str_size, $matches); + if (!isset($matches[1])) { + throw new \Exception('请确认size大小'); + } + if (!isset($matches[2])) { + throw new \Exception('请确认单位'); + } + + $size = intval($matches[1]); + $unit = $matches[2]; + switch (strtolower($unit)) { + case 'kb': + $size *= 1024; + break; + case 'mb': + $size *= 1024 * 1024; + break; + case 'gb': + $size *= 1024 * 1024 * 1024; + break; + case 'tb': + $size *= 1024 * 1024 * 1024 * 1024; + default: + throw new \Exception('不支持[' . $unit . ']单位'); + } + return $size; + } } \ No newline at end of file diff --git a/app/admin/controller/v1/Upload.php b/app/admin/controller/v1/Upload.php index 762e729b..1e06ece7 100644 --- a/app/admin/controller/v1/Upload.php +++ b/app/admin/controller/v1/Upload.php @@ -27,9 +27,10 @@ class Upload } try { + $max_size = strtobytes(env('ADMIN_API.MAX_IMAGE_SIZE', '1mb')); $validate = validate([ 'module' => 'require|max:64', - 'image' => 'fileSize:1048576|fileExt:jpg,jpeg,png,gif' + 'image' => "fileSize:$max_size|fileExt:jpg,jpeg,png,gif" ]); if (!$validate->check(['module' => $param['module'], 'image' => $file])) { return error($validate->getError()); @@ -94,9 +95,10 @@ class Upload } try { + $max_size = strtobytes(env('ADMIN_API.MAX_VIDEO_SIZE', '100mb')); $validate = validate([ 'module' => 'require|max:64', - 'video' => 'fileSize:157286400|fileExt:mp4' + 'video' => "fileSize:$max_size|fileExt:mp4" ]); if (!$validate->check(['module' => $param['module'], 'video' => $file])) { return error($validate->getError()); @@ -148,8 +150,9 @@ class Upload } try { + $max_size = strtobytes(env('ADMIN_API.MAX_ATTACHMENT_SIZE', '100mb')); $validate = validate([ - 'attachment' => 'fileSize:104857600|fileExt:zip,rar,7z,doc,docx,xls,xlsx,csv,ppt,pptx,pdf,txt' + 'attachment' => "fileSize:$max_size|fileExt:zip,rar,7z,doc,docx,xls,xlsx,csv,ppt,pptx,pdf,txt" ]); if (!$validate->check(['attachment' => $file])) { return error($validate->getError());