refactor: 上传相关优化
This commit is contained in:
@@ -171,8 +171,25 @@ class SiteConfig
|
|||||||
if (!empty($configs)) {
|
if (!empty($configs)) {
|
||||||
$configs_map = [];
|
$configs_map = [];
|
||||||
foreach ($configs as $cfg) {
|
foreach ($configs as $cfg) {
|
||||||
$configs_map[$cfg['name']] = $cfg;
|
$current = &$configs_map;
|
||||||
|
|
||||||
|
// 根据name中"."拆分为多维数组
|
||||||
|
$parts = explode('.', $cfg['name']);
|
||||||
|
foreach ($parts as $part) {
|
||||||
|
if (!isset($current[$part])) {
|
||||||
|
$current[$part] = [];
|
||||||
}
|
}
|
||||||
|
$current = &$current[$part];
|
||||||
|
}
|
||||||
|
$current = [
|
||||||
|
'id' => $cfg['id'],
|
||||||
|
'title' => $cfg['title'],
|
||||||
|
'name' => $cfg['name'],
|
||||||
|
'value' => $cfg['value']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
unset($current);
|
||||||
|
|
||||||
return $configs_map;
|
return $configs_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,9 +42,17 @@ class Upload
|
|||||||
$filemd5 = $file->md5();
|
$filemd5 = $file->md5();
|
||||||
$filesha1 = $file->sha1();
|
$filesha1 = $file->sha1();
|
||||||
|
|
||||||
$image_model = SysImageUploadRecordModel::md5($filemd5)->find();
|
// 获取图片上传配置
|
||||||
|
list(
|
||||||
|
'filename_keep' => $filename_keep,
|
||||||
|
'filemd5_unique' => $filemd5_unique
|
||||||
|
) = $this->getUploadOptions('upload_image');
|
||||||
|
// 是否需要根据文件MD5值检查文件是否已存在
|
||||||
|
$image_model = $filemd5_unique ? SysImageUploadRecordModel::md5($filemd5)->find() : null;
|
||||||
if (is_null($image_model)) {
|
if (is_null($image_model)) {
|
||||||
$filename = Filesystem::disk('image')->putFile($param['module'], $file);
|
// 检查是否需要保留原文件名生成器
|
||||||
|
$name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null;
|
||||||
|
$filename = Filesystem::disk('image')->putFile($param['module'], $file, $name_rule());
|
||||||
// 处理图片
|
// 处理图片
|
||||||
$image_manager = ImageManager::imagick();
|
$image_manager = ImageManager::imagick();
|
||||||
$image = $image_manager->read('.' . $storage . '/' . $filename);
|
$image = $image_manager->read('.' . $storage . '/' . $filename);
|
||||||
@@ -146,6 +154,33 @@ class Upload
|
|||||||
|
|
||||||
return error('上传失败');
|
return error('上传失败');
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 文件名生成回调
|
||||||
|
*
|
||||||
|
* @param \think\file\UploadedFile $file
|
||||||
|
* @return callable
|
||||||
|
*/
|
||||||
|
private function filenameGenerator(\think\file\UploadedFile $file): callable
|
||||||
|
{
|
||||||
|
return fn() => date('Ymd') . '/' . pathinfo($file->getOriginalName(), PATHINFO_FILENAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取上传配置
|
||||||
|
*
|
||||||
|
* @param string $module
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getUploadOptions($module)
|
||||||
|
{
|
||||||
|
$config_model = new \app\admin\controller\v1\SiteConfig;
|
||||||
|
$config = $config_model->getByGroupUniqueLabel('upload');
|
||||||
|
$options = data_get($config, $module, []);
|
||||||
|
throw_if(empty($options), '上传配置错误');
|
||||||
|
return [
|
||||||
|
'filename_keep' => (int)data_get($options, 'filename_keep.value', 0) == 1,
|
||||||
|
'filemd5_unique' => (int)data_get($options, 'filemd5_unique.value', 0) == 1,
|
||||||
|
];
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 获取水印配置
|
* 获取水印配置
|
||||||
*
|
*
|
||||||
@@ -260,9 +295,17 @@ class Upload
|
|||||||
$filemd5 = $file->md5();
|
$filemd5 = $file->md5();
|
||||||
$filesha1 = $file->sha1();
|
$filesha1 = $file->sha1();
|
||||||
|
|
||||||
$video = SysVideoUploadRecordModel::md5($filemd5)->find();
|
// 获取视频上传配置
|
||||||
|
list(
|
||||||
|
'filename_keep' => $filename_keep,
|
||||||
|
'filemd5_unique' => $filemd5_unique
|
||||||
|
) = $this->getUploadOptions('upload_video');
|
||||||
|
// 是否需要根据文件MD5值检查文件是否已存在
|
||||||
|
$video = $filemd5_unique ? SysVideoUploadRecordModel::md5($filemd5)->find() : null;
|
||||||
if (is_null($video)) {
|
if (is_null($video)) {
|
||||||
$filename = Filesystem::disk('video')->putFile($param['module'], $file);
|
// 检查是否需要保留原文件名
|
||||||
|
$name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null;
|
||||||
|
$filename = Filesystem::disk('video')->putFile($param['module'], $file, $name_rule());
|
||||||
|
|
||||||
// 保存视频
|
// 保存视频
|
||||||
$video = new SysVideoUploadRecordModel();
|
$video = new SysVideoUploadRecordModel();
|
||||||
@@ -312,9 +355,17 @@ class Upload
|
|||||||
$filemd5 = $file->md5();
|
$filemd5 = $file->md5();
|
||||||
$filesha1 = $file->sha1();
|
$filesha1 = $file->sha1();
|
||||||
|
|
||||||
$attachment = SysAttachmentUploadRecordModel::md5($filemd5)->find();
|
// 获取视频上传配置
|
||||||
|
list(
|
||||||
|
'filename_keep' => $filename_keep,
|
||||||
|
'filemd5_unique' => $filemd5_unique
|
||||||
|
) = $this->getUploadOptions('upload_attachment');
|
||||||
|
// 是否需要根据文件MD5值检查文件是否已存在
|
||||||
|
$attachment = $filemd5_unique ? SysAttachmentUploadRecordModel::md5($filemd5)->find() : null;
|
||||||
if (is_null($attachment)) {
|
if (is_null($attachment)) {
|
||||||
$filename = Filesystem::disk('public')->putFile('attachments', $file);
|
// 检查是否需要保留原文件名
|
||||||
|
$name_rule = fn() => $filename_keep ? $this->filenameGenerator($file) : null;
|
||||||
|
$filename = Filesystem::disk('public')->putFile('attachments', $file, $name_rule());
|
||||||
|
|
||||||
// 保存视频
|
// 保存视频
|
||||||
$attachment = new SysAttachmentUploadRecordModel();
|
$attachment = new SysAttachmentUploadRecordModel();
|
||||||
|
|||||||
Reference in New Issue
Block a user