up.
This commit is contained in:
parent
ca6abacb5d
commit
36dbbc8cb3
@ -5,6 +5,7 @@ namespace app\service;
|
||||
use app\entity\SysFileRecord;
|
||||
use app\entity\SysFileRule;
|
||||
use app\entity\UploadFile;
|
||||
use app\service\file\FilesystemUploadService;
|
||||
use think\File;
|
||||
use think\Service;
|
||||
|
||||
@ -18,7 +19,7 @@ class FileService extends Service
|
||||
/*
|
||||
* 注册到全局容器中
|
||||
*/
|
||||
$this->app->bind('filesystem', $this);
|
||||
$this->app->bind('filesystem', FilesystemUploadService::class);
|
||||
}
|
||||
|
||||
public function upload(string $contextId, File $file, SysFileRule $rule): UploadFile
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
namespace app\service\file;
|
||||
|
||||
use app\BaseService;
|
||||
use app\entity\SysFileRule;
|
||||
use app\entity\SysUserFile;
|
||||
use think\facade\Event;
|
||||
@ -17,7 +16,12 @@ use think\helper\Str;
|
||||
|
||||
class FilesystemUploadService
|
||||
{
|
||||
public App $app;
|
||||
protected App $app;
|
||||
|
||||
public function __construct(App $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UploadedFile $file 上传的文件
|
||||
@ -30,44 +34,35 @@ class FilesystemUploadService
|
||||
{
|
||||
$rule_id = 1;
|
||||
if ($directory_id > 0) {
|
||||
// 上传到某个目录下
|
||||
$directory = SysUserFile::where('is_directory', 1)->find($directory_id);
|
||||
if (empty($directory)) {
|
||||
throw new ValidateException('Dir: 目录不存在');
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 文件记录实体
|
||||
*/
|
||||
$filesystem_model = $this->app->config->get('filesystem.model');
|
||||
if (empty($filesystem_model)) {
|
||||
throw new FileException('File记录模型未定义');
|
||||
}
|
||||
/**
|
||||
* @var SysFileRule $fileRule
|
||||
*/
|
||||
$fileRule = SysFileRule::find($rule_id);
|
||||
if (empty($fileRule) || $fileRule['is_close']) {
|
||||
if (empty($fileRule)) {
|
||||
throw new ValidateException('Rule: 上传规则不存在');
|
||||
}
|
||||
if ($fileRule['is_close']) {
|
||||
throw new ValidateException('Rule: 已关闭上传权限');
|
||||
}
|
||||
|
||||
/*
|
||||
* 验证文件信息
|
||||
* 根据规则校验上传的文件是否符合要求
|
||||
*/
|
||||
if ($fileRule->rule_file_size_limit_min > 0) {
|
||||
if ($file->getSize() < $fileRule->rule_file_size_limit_min * 1024) {
|
||||
throw new ValidateException('文件限制:文件太小了');
|
||||
}
|
||||
}
|
||||
$validateRule = [];
|
||||
if ($fileRule->rule_file_size_limit_max > 0) {
|
||||
$validateRule[] = 'fileSize:' . ($fileRule->rule_file_size_limit_max * 1024);
|
||||
}
|
||||
if ($fileRule->rule_file_ext !== '') {
|
||||
$validateRule[] = "fileExt:$fileRule->rule_file_ext";
|
||||
}
|
||||
if ($fileRule->rule_file_mime !== '') {
|
||||
$validateRule[] = "fileMime:$fileRule->rule_file_mime";
|
||||
}
|
||||
if ($fileRule->rule_image !== '') {
|
||||
$validateRule[] = "image:$fileRule->rule_image";
|
||||
}
|
||||
if ($validateRule) {
|
||||
validate(['file' => implode('|', $validateRule)])
|
||||
->check(['file' => $file]);
|
||||
}
|
||||
$this->checkFileRule($fileRule, $file);
|
||||
|
||||
|
||||
$putDisk = $fileRule->disk;
|
||||
$putUrl = $fileRule->url;
|
||||
$putPath = $fileRule->path_rule;
|
||||
@ -110,8 +105,7 @@ class FilesystemUploadService
|
||||
$fileSaveName = pathinfo($uploadPath, PATHINFO_BASENAME);
|
||||
// dump([$diskUrl, $uploadPath]);
|
||||
// dump(['#4', $uploadSavePath, $fileSaveName]);
|
||||
$filesystem_model = $this->app->config->get('filesystem.model');
|
||||
if (empty($filesystem_model)) $filesystem_model = SysFileRecord::class;
|
||||
|
||||
|
||||
$sysFileRecord = $this->app->invokeClass($filesystem_model);
|
||||
$sysFileRecord->save([
|
||||
@ -148,8 +142,6 @@ class FilesystemUploadService
|
||||
'length' => $file->getSize(),
|
||||
'content_type' => $file->getOriginalMime(),
|
||||
'deleted' => 0,
|
||||
// 租户
|
||||
'tenant_id' => $this->app->request->tenantId,
|
||||
// 磁盘
|
||||
'disk' => $putDisk,
|
||||
// 存储规则
|
||||
@ -178,7 +170,7 @@ class FilesystemUploadService
|
||||
Event::trigger('upload', ['results' => $results, 'file' => $file, 'rule' => $fileRuleData, 'model' => $sysFileRecordData, 'directory_id' => $directory_id]);
|
||||
// 触发磁盘上传事件
|
||||
Event::trigger("upload.$putDisk", ['results' => $results, 'file' => $file, 'rule' => $fileRuleData, 'model' => $sysFileRecordData, 'directory_id' => $directory_id]);
|
||||
// 触发磁盘某个行为上传事件
|
||||
// 触发磁盘某个规则的上传事件
|
||||
Event::trigger("upload.rule_$rule_id", ['results' => $results, 'file' => $file, 'rule' => $fileRuleData, 'model' => $sysFileRecordData, 'directory_id' => $directory_id]);
|
||||
return $results;
|
||||
}
|
||||
@ -220,4 +212,33 @@ class FilesystemUploadService
|
||||
}
|
||||
return str_replace(array_keys($array), array_values($array), $pathname);
|
||||
}
|
||||
|
||||
private function checkFileRule(SysFileRule|\app\model\SysFileRule $fileRule, UploadedFile $file): void
|
||||
{
|
||||
/*
|
||||
* 验证文件信息
|
||||
*/
|
||||
if ($fileRule->rule_file_size_limit_min > 0) {
|
||||
if ($file->getSize() < $fileRule->rule_file_size_limit_min * 1024) {
|
||||
throw new ValidateException('上传限制:文件太小了');
|
||||
}
|
||||
}
|
||||
$validateRule = [];
|
||||
if ($fileRule->rule_file_size_limit_max > 0) {
|
||||
$validateRule[] = 'fileSize:' . ($fileRule->rule_file_size_limit_max * 1024);
|
||||
}
|
||||
if ($fileRule->rule_file_ext !== '') {
|
||||
$validateRule[] = "fileExt:$fileRule->rule_file_ext";
|
||||
}
|
||||
if ($fileRule->rule_file_mime !== '') {
|
||||
$validateRule[] = "fileMime:$fileRule->rule_file_mime";
|
||||
}
|
||||
if ($fileRule->rule_image !== '') {
|
||||
$validateRule[] = "image:$fileRule->rule_image";
|
||||
}
|
||||
if ($validateRule) {
|
||||
validate(['file' => implode('|', $validateRule)])
|
||||
->check(['file' => $file]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -21,4 +21,5 @@ return [
|
||||
],
|
||||
// 更多的磁盘配置信息
|
||||
],
|
||||
'model' => \app\entity\SysFileRecord::class
|
||||
];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user