feat: 新增附件上传接口

This commit is contained in:
2025-02-28 14:46:47 +08:00
parent ca40dc3cc8
commit 084b003abf
5 changed files with 149 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ namespace app\admin\controller\v1;
use app\admin\model\v1\SysImageUploadRecordModel;
use app\admin\model\v1\SysVideoUploadRecordModel;
use app\admin\model\v1\SysAttachmentUploadRecordModel;
use Intervention\Image\ImageManager;
use think\facade\Filesystem;
@@ -135,4 +136,55 @@ class Upload
return error('上传失败');
}
/**
* 附件上传
*/
public function attachment()
{
$file = request()->file('attachment');
if (is_null($file)) {
return error('请确定上传对象或字段是否正确');
}
try {
$validate = validate([
'attachment' => 'fileSize:104857600|fileExt:zip,rar,7z,doc,docx,xls,xlsx,csv,ppt,pptx,pdf,txt'
]);
if (!$validate->check(['attachment' => $file])) {
return error($validate->getError());
}
$filemd5 = $file->md5();
$filesha1 = $file->sha1();
$attachment = SysAttachmentUploadRecordModel::md5($filemd5)->find();
if (is_null($attachment)) {
$filename = Filesystem::disk('public')->putFile('attachments', $file);
// 保存视频
$attachment = new SysAttachmentUploadRecordModel();
$attachment->language_id = request()->lang_id;
$attachment->attachment_path = $filename;
$attachment->file_size = $file->getSize();
$attachment->file_type = $file->getOriginalMime();
$attachment->file_md5 = $filemd5;
$attachment->file_sha1 = $filesha1;
if (!$attachment->save()) {
return error('上传失败');
}
}
$storage = config('filesystem.disks.public.url');
return success('上传成功', [
'path' => $storage . '/' . $attachment->attachment_path,
'file_md5' => $attachment->file_md5,
'file_sha1' => $attachment->file_sha1
]);
} catch (\Throwable $th) {
return error($th->getMessage());
}
return error('上传失败');
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\SysAttachmentUploadRecordBaseModel;
/**
* 附件上传模型
* @mixin \think\Model
*/
class SysAttachmentUploadRecordModel extends SysAttachmentUploadRecordBaseModel
{
// 根据md5获取
public function scopeMd5($query, $md5)
{
$query->where('file_md5', '=', $md5);
}
}

View File

@@ -277,6 +277,9 @@ Route::group('v1', function () {
// 附件(下载管理)
Route::group('attachment', function () {
// 附件(下载管理)上传
Route::post('/upload', 'Upload/attachment');
// 附件(下载管理)列表
Route::get('index', 'Attachment/index');

View File

@@ -0,0 +1,33 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
/**
* 附件上传基础模型
* @mixin \think\Model
*/
class SysAttachmentUploadRecordBaseModel extends Model
{
// 表名
protected $name = 'sys_attachment_upload_record';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'module' => 'string',
'attachment_path' => 'string',
'file_size' => 'int',
'file_type' => 'string',
'file_md5' => 'string',
'file_sha1' => 'string',
'created_at' => 'datetime',
'deleted_at' => 'datetime',
];
}