feat: 新增附件上传接口

This commit is contained in:
2025-02-28 14:46:47 +08:00
parent 013416ba8e
commit ea8d58c03f
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\SysImageUploadRecordModel;
use app\admin\model\v1\SysVideoUploadRecordModel; use app\admin\model\v1\SysVideoUploadRecordModel;
use app\admin\model\v1\SysAttachmentUploadRecordModel;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use think\facade\Filesystem; use think\facade\Filesystem;
@@ -135,4 +136,55 @@ class Upload
return error('上传失败'); 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::group('attachment', function () {
// 附件(下载管理)上传
Route::post('/upload', 'Upload/attachment');
// 附件(下载管理)列表 // 附件(下载管理)列表
Route::get('index', 'Attachment/index'); 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',
];
}

View File

@@ -0,0 +1,42 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class CreateSysAttachmentUploadRecord 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_attachment_upload_record', ['engine' => 'MyISAM', 'comment' => '附件上传表']);
$table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID'])
->addColumn('attachment_path', 'string', ['limit' => 125, 'null' => false, 'comment' => '附件路径'])
->addColumn('file_size', 'integer', ['null' => false, 'comment' => '附件大小'])
->addColumn('file_type', 'string', ['limit' => 125, 'null' => false, 'comment' => '附件类型'])
->addColumn('file_md5', 'string', ['limit' => 32, 'null' => false, 'comment' => '附件md5值'])
->addColumn('file_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();
}
}