This commit is contained in:
@@ -3,7 +3,6 @@ namespace addon\theme_configurator\controller;
|
||||
|
||||
use app\event\controller\PluginAdminBaseController;
|
||||
use think\Response;
|
||||
use think\facade\Filesystem;
|
||||
|
||||
/**
|
||||
* 后台上传控制器
|
||||
@@ -30,33 +29,57 @@ class UploadController extends PluginAdminBaseController
|
||||
]);
|
||||
}
|
||||
|
||||
// 验证文件
|
||||
$validate = [
|
||||
'size' => 10 * 1024 * 1024, // 10MB
|
||||
'ext' => 'jpg,jpeg,png,gif,webp,svg',
|
||||
];
|
||||
|
||||
try {
|
||||
validate($validate)->check(['file' => $file]);
|
||||
} catch (\Exception $e) {
|
||||
// 验证文件类型和大小
|
||||
$fileExt = strtolower($file->extension());
|
||||
$fileSize = $file->getSize();
|
||||
|
||||
$allowedExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
|
||||
$maxSize = 10 * 1024 * 1024; // 10MB
|
||||
|
||||
if (!in_array($fileExt, $allowedExts)) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . $e->getMessage(),
|
||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . '仅支持 jpg, jpeg, png, gif, webp, svg 格式',
|
||||
]);
|
||||
}
|
||||
|
||||
if ($fileSize > $maxSize) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . '文件大小不能超过 10MB',
|
||||
]);
|
||||
}
|
||||
|
||||
// 上传到本地服务器
|
||||
$saveName = Filesystem::disk('public')->putFile('theme', $file);
|
||||
// 构建保存路径
|
||||
$uploadPath = root_path('public') . 'upload' . DIRECTORY_SEPARATOR . 'theme';
|
||||
$dateDir = date('Ymd');
|
||||
$savePath = $uploadPath . DIRECTORY_SEPARATOR . $dateDir;
|
||||
|
||||
if (!$saveName) {
|
||||
// 创建目录
|
||||
if (!is_dir($savePath)) {
|
||||
if (!mkdir($savePath, 0755, true)) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_save_failed') . ': 无法创建上传目录',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
$fileName = md5(uniqid() . microtime()) . '.' . $fileExt;
|
||||
$fullPath = $savePath . DIRECTORY_SEPARATOR . $fileName;
|
||||
|
||||
// 移动文件
|
||||
if (!$file->move($savePath, $fileName)) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_save_failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
// 构建完整的URL路径
|
||||
$url = '/upload/' . str_replace('\\', '/', $saveName);
|
||||
|
||||
// 构建URL路径
|
||||
$saveName = 'theme/' . $dateDir . '/' . $fileName;
|
||||
$url = '/upload/' . $saveName;
|
||||
|
||||
return json([
|
||||
'status' => 200,
|
||||
|
||||
Reference in New Issue
Block a user