feat: 添加视频上传接口

This commit is contained in:
2025-02-19 16:33:36 +08:00
parent df0c545b90
commit 5c15fb83e1
7 changed files with 182 additions and 0 deletions

View File

@@ -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('上传失败');
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
/**
* 视频管理控制器
*/
class Video
{
}

View File

@@ -0,0 +1,19 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\SysVideoBaseModel;
/**
* 视频上传模型
* @mixin \think\Model
*/
class SysVideoModel extends SysVideoBaseModel
{
// 根据md5查询
public function scopeMd5($query, $value)
{
$query->where('video_md5', '=', $value);
}
}

View File

@@ -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 () {
// 文章列表

View File

@@ -0,0 +1,33 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
/**
* 视频上传基础模型
* @mixin \think\Model
*/
class SysVideoBaseModel extends Model
{
// 表名
protected $name = 'sys_video';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => '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',
];
}

View File

@@ -29,6 +29,16 @@ return [
// 可见性
'visibility' => 'public',
],
'video' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/storage/videos',
// 磁盘路径对应的外部URL路径
'url' => '/storage/videos',
// 可见性
'visibility' => 'public',
],
// 更多的磁盘配置信息
],
];

View File

@@ -0,0 +1,43 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class CreateSysVideo extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}