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