From 099813d0cdb0a702cd862a0a5083e2668b7b5459 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Wed, 19 Feb 2025 16:33:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/v1/Upload.php | 59 +++++++++++++++++++ app/admin/controller/v1/Video.php | 12 ++++ app/admin/model/v1/SysVideoModel.php | 19 ++++++ app/admin/route/v1.php | 6 ++ app/common/model/SysVideoBaseModel.php | 33 +++++++++++ config/filesystem.php | 10 ++++ .../20250219080834_create_sys_video.php | 43 ++++++++++++++ 7 files changed, 182 insertions(+) create mode 100644 app/admin/controller/v1/Video.php create mode 100644 app/admin/model/v1/SysVideoModel.php create mode 100644 app/common/model/SysVideoBaseModel.php create mode 100644 database/migrations/20250219080834_create_sys_video.php diff --git a/app/admin/controller/v1/Upload.php b/app/admin/controller/v1/Upload.php index e828b158..b85f068a 100644 --- a/app/admin/controller/v1/Upload.php +++ b/app/admin/controller/v1/Upload.php @@ -4,6 +4,7 @@ declare (strict_types = 1); namespace app\admin\controller\v1; use app\admin\model\v1\SysImageModel; +use app\admin\model\v1\SysVideoModel; use Intervention\Image\ImageManager; use think\facade\Filesystem; @@ -76,4 +77,62 @@ class Upload return error('上传失败'); } + + /** + * 上传视频 + */ + public function video() + { + $param = request()->param(['module' => 'unknown']); + if (is_null($param)) { + return error('请确定请求参数正确'); + } + $file = request()->file('video'); + if (is_null($file)) { + return error('请确定上传对象或字段是否正确'); + } + + try { + $validate = validate([ + 'module' => 'require|max:64', + 'image' => 'fileSize:52428800|fileExt:mp4' + ]); + if (!$validate->check(['module' => $param['module'], 'image' => $file])) { + return error($validate->getError()); + } + + $storage = config('filesystem.disks.video.url'); + + $filemd5 = $file->md5(); + $filesha1 = $file->sha1(); + + $video = SysVideoModel::md5($filemd5)->find(); + if (is_null($video)) { + $filename = Filesystem::disk('video')->putFile($param['module'], $file); + + // 保存视频 + $video = new SysVideoModel(); + $video->language_id = $param['language_id']; + $video->module = $param['module']; + $video->video_path = $filename; + $video->video_size = $file->getSize(); + $video->video_type = $file->getOriginalMime(); + $video->video_md5 = $filemd5; + $video->video_sha1 = $filesha1; + if (!$video->save()) { + return error('上传失败'); + } + } + + return success('上传成功', [ + 'url' => $storage . '/' . $video->video_path, + 'file_md5' => $video->file_md5, + 'file_sha1' => $video->file_sha1 + ]); + } catch (\Throwable $th) { + return error($th->getMessage()); + } + + return error('上传失败'); + } } diff --git a/app/admin/controller/v1/Video.php b/app/admin/controller/v1/Video.php new file mode 100644 index 00000000..9bebe770 --- /dev/null +++ b/app/admin/controller/v1/Video.php @@ -0,0 +1,12 @@ +where('video_md5', '=', $value); + } +} diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index 001a553d..46c5fb57 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -41,6 +41,12 @@ Route::group('v1', function () { Route::post('/:module/upload', 'Upload/image'); }); + // 视频管理 + Route::group('video', function () { + // 视频上传 + Route::post('/:module/upload', 'Upload/video'); + }); + // 文章模块 Route::group('article', function () { // 文章列表 diff --git a/app/common/model/SysVideoBaseModel.php b/app/common/model/SysVideoBaseModel.php new file mode 100644 index 00000000..68cea675 --- /dev/null +++ b/app/common/model/SysVideoBaseModel.php @@ -0,0 +1,33 @@ + 'int', + 'language_id' => 'int', + 'module' => 'string', + 'video_path' => 'string', + 'video_size' => 'int', + 'video_type' => 'string', + 'video_md5' => 'string', + 'video_sha1' => 'string', + 'created_at' => 'datetime', + 'deleted_at' => 'datetime', + ]; +} diff --git a/config/filesystem.php b/config/filesystem.php index 1c681a6e..67e5f44a 100644 --- a/config/filesystem.php +++ b/config/filesystem.php @@ -29,6 +29,16 @@ return [ // 可见性 'visibility' => 'public', ], + 'video' => [ + // 磁盘类型 + 'type' => 'local', + // 磁盘路径 + 'root' => app()->getRootPath() . 'public/storage/videos', + // 磁盘路径对应的外部URL路径 + 'url' => '/storage/videos', + // 可见性 + 'visibility' => 'public', + ], // 更多的磁盘配置信息 ], ]; diff --git a/database/migrations/20250219080834_create_sys_video.php b/database/migrations/20250219080834_create_sys_video.php new file mode 100644 index 00000000..9d5252ad --- /dev/null +++ b/database/migrations/20250219080834_create_sys_video.php @@ -0,0 +1,43 @@ +table('sys_video', ['engine' => 'MyISAM', 'comment' => '视频上传表']); + $table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID']) + ->addColumn('module', 'string', ['limit' => 64, 'null' => false, 'comment' => '视频所属模块']) + ->addColumn('video_path', 'string', ['limit' => 125, 'null' => false, 'comment' => '视频路径']) + ->addColumn('video_size', 'integer', ['null' => false, 'comment' => '视频大小']) + ->addColumn('video_type', 'string', ['limit' => 125, 'null' => false, 'comment' => '视频类型']) + ->addColumn('video_md5', 'string', ['limit' => 32, 'null' => false, 'comment' => '视频md5值']) + ->addColumn('video_sha1', 'string', ['limit' => 40, 'null' => false, 'comment' => '视频sha1值']) + ->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间']) + ->addColumn('deleted_at', 'timestamp', ['null' => true, 'comment' => '删除时间']) + ->create(); + } +}