refactor: 修改上传限制可配置
This commit is contained in:
3
.env.dev
3
.env.dev
@@ -24,3 +24,6 @@ WHITE_LIST[] = v1/user/captcha
|
|||||||
# 不需记录日志的接口
|
# 不需记录日志的接口
|
||||||
[ADMIN_API]
|
[ADMIN_API]
|
||||||
IGNORE_LOGGING_LIST[] = v1/OperateLog/index
|
IGNORE_LOGGING_LIST[] = v1/OperateLog/index
|
||||||
|
MAX_IMAGE_SIZE = 5mb # 图片上传最大限制
|
||||||
|
MAX_VIDEO_SIZE = 150mb # 视频上传最大限制
|
||||||
|
MAX_ATTACHMENT_SIZE = 100mb # 附件上传最大限制
|
||||||
|
|||||||
@@ -94,4 +94,42 @@ if (!function_exists('xlsx_writer')) {
|
|||||||
]);
|
]);
|
||||||
return $response;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -27,9 +27,10 @@ class Upload
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$max_size = strtobytes(env('ADMIN_API.MAX_IMAGE_SIZE', '1mb'));
|
||||||
$validate = validate([
|
$validate = validate([
|
||||||
'module' => 'require|max:64',
|
'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])) {
|
if (!$validate->check(['module' => $param['module'], 'image' => $file])) {
|
||||||
return error($validate->getError());
|
return error($validate->getError());
|
||||||
@@ -94,9 +95,10 @@ class Upload
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$max_size = strtobytes(env('ADMIN_API.MAX_VIDEO_SIZE', '100mb'));
|
||||||
$validate = validate([
|
$validate = validate([
|
||||||
'module' => 'require|max:64',
|
'module' => 'require|max:64',
|
||||||
'video' => 'fileSize:157286400|fileExt:mp4'
|
'video' => "fileSize:$max_size|fileExt:mp4"
|
||||||
]);
|
]);
|
||||||
if (!$validate->check(['module' => $param['module'], 'video' => $file])) {
|
if (!$validate->check(['module' => $param['module'], 'video' => $file])) {
|
||||||
return error($validate->getError());
|
return error($validate->getError());
|
||||||
@@ -148,8 +150,9 @@ class Upload
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$max_size = strtobytes(env('ADMIN_API.MAX_ATTACHMENT_SIZE', '100mb'));
|
||||||
$validate = validate([
|
$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])) {
|
if (!$validate->check(['attachment' => $file])) {
|
||||||
return error($validate->getError());
|
return error($validate->getError());
|
||||||
|
|||||||
Reference in New Issue
Block a user