dev #4

Merged
weiz merged 2 commits from dev into master 2023-12-04 16:41:27 +08:00
4 changed files with 74 additions and 1 deletions
Showing only changes of commit 1205579a17 - Show all commits

View File

@ -59,5 +59,16 @@ class UploadController extends BaseAdminController
return $this->fail($e->getMessage()); return $this->fail($e->getMessage());
} }
} }
public function file()
{
try {
$cid = $this->request->post('cid', 0);
$result = UploadService::file($cid);
return $this->success('上传成功', $result);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
} }

View File

@ -160,5 +160,62 @@ class UploadService
throw new Exception($e->getMessage()); throw new Exception($e->getMessage());
} }
} }
public static function file($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/file')
{
try {
$config = [
'default' => ConfigService::get('storage', 'default', 'local'),
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
];
// 2、执行文件上传
$StorageDriver = new StorageDriver($config);
$StorageDriver->setUploadFile('file');
$fileName = $StorageDriver->getFileName();
$fileInfo = $StorageDriver->getFileInfo();
// 校验上传文件后缀
if (!in_array(strtolower($fileInfo['ext']), config('project.file_file'))) {
throw new Exception("上传文件不允许上传". $fileInfo['ext'] . "文件");
}
// 上传文件
$saveDir = $saveDir . '/' . date('Ymd');
if (!$StorageDriver->upload($saveDir)) {
throw new Exception($StorageDriver->getError());
}
// 3、处理文件名称
if (strlen($fileInfo['name']) > 128) {
$name = substr($fileInfo['name'], 0, 123);
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
$fileInfo['name'] = $name . $nameEnd;
}
// 4、写入数据库中
$file = File::create([
'cid' => $cid,
'type' => FileEnum::VIDEO_TYPE,
'name' => $fileInfo['name'],
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
'source' => $source,
'source_id' => $sourceId,
'create_time' => time(),
]);
// 5、返回结果
return [
'id' => $file['id'],
'cid' => $file['cid'],
'type' => $file['type'],
'name' => $file['name'],
'uri' => FileService::getFileUrl($file['uri']),
'url' => $file['uri']
];
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
} }

View File

@ -42,7 +42,7 @@ abstract class Server
} }
// 校验上传文件后缀 // 校验上传文件后缀
$limit = array_merge(config('project.file_image'), config('project.file_video')); $limit = array_merge(config('project.file_image'), config('project.file_video'),config('project.file_file'));
if (!in_array(strtolower($this->file->extension()), $limit)) { if (!in_array(strtolower($this->file->extension()), $limit)) {
throw new Exception('不允许上传' . $this->file->extension() . '后缀文件'); throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
} }

View File

@ -73,6 +73,11 @@ return [
'file_video' => [ 'file_video' => [
'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv' 'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv'
], ],
// 文件上传限制 (文件)
'file_file' => [
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt','apk','ipa','wgt'
],
// 登录设置 // 登录设置
'login' => [ 'login' => [