diff --git a/app/adminapi/controller/UploadController.php b/app/adminapi/controller/UploadController.php index 1bee7dd..f45d518 100644 --- a/app/adminapi/controller/UploadController.php +++ b/app/adminapi/controller/UploadController.php @@ -59,5 +59,16 @@ class UploadController extends BaseAdminController 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()); + } + } } \ No newline at end of file diff --git a/app/api/controller/AppUpdateController.php b/app/api/controller/AppUpdateController.php new file mode 100644 index 0000000..e04a884 --- /dev/null +++ b/app/api/controller/AppUpdateController.php @@ -0,0 +1,13 @@ +where('')->order('id desc')->findOrEmpty(); + return $this->success('请求成功',$data->toArray()); + } + } \ No newline at end of file diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 32e2bf4..e403a75 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -160,5 +160,62 @@ class UploadService 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()); + } + } } \ No newline at end of file diff --git a/app/common/service/storage/engine/Server.php b/app/common/service/storage/engine/Server.php index 07bf0d5..77f6e72 100644 --- a/app/common/service/storage/engine/Server.php +++ b/app/common/service/storage/engine/Server.php @@ -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)) { throw new Exception('不允许上传' . $this->file->extension() . '后缀文件'); } diff --git a/config/project.php b/config/project.php index 3ba585d..b34e9c1 100644 --- a/config/project.php +++ b/config/project.php @@ -73,6 +73,11 @@ return [ 'file_video' => [ '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' => [