OSS功能提交

This commit is contained in:
wong
2025-06-12 11:00:49 +08:00
parent 0bbd0d7b9b
commit 2c1286f7ac
4 changed files with 87 additions and 20 deletions

View File

@@ -28,8 +28,8 @@ class Attachment extends Controller
$validate = \think\facade\Validate::rule([
'file' => [
'fileSize' => 10485760, // 10MB
'fileExt' => 'jpg,jpeg,png,gif,doc,docx,pdf,zip,rar',
'fileMime' => 'image/jpeg,image/png,image/gif,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,application/zip,application/x-rar-compressed'
'fileExt' => 'jpg,jpeg,png,gif,doc,docx,pdf,zip,rar,mp4,mp3',
'fileMime' => 'image/jpeg,image/png,image/gif,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,application/zip,application/x-rar-compressed,video/mp4,audio/mp3'
]
]);
@@ -56,13 +56,16 @@ class Attachment extends Controller
]
]);
}
// 生成OSS对象名称
$objectName = AliyunOSS::generateObjectName($file->getOriginalName());
$objectName = AliyunOSS::generateObjectName($file->getInfo('name'));
// 上传到OSS
$result = AliyunOSS::uploadFile($file->getRealPath(), $objectName);
if (!$result['success']) {
return json([
'code' => 500,
@@ -72,17 +75,17 @@ class Attachment extends Controller
// 保存到数据库
$attachmentData = [
'name' => Request::param('name') ?: $file->getOriginalName(),
'name' => Request::param('name') ?: $file->getInfo('name'),
'hash_key' => $hashKey,
'server' => 'aliyun_oss',
'source' => $result['url'],
'size' => $result['size'],
'suffix' => pathinfo($file->getOriginalName(), PATHINFO_EXTENSION)
'suffix' => pathinfo($file->getInfo('name'), PATHINFO_EXTENSION)
];
$attachmentId = AttachmentModel::addAttachment($attachmentData);
$attachment = AttachmentModel::addAttachment($attachmentData);
if (!$attachmentId) {
if (!$attachment) {
return json([
'code' => 500,
'msg' => '保存附件信息失败'
@@ -93,7 +96,7 @@ class Attachment extends Controller
'code' => 200,
'msg' => '上传成功',
'data' => [
'id' => $attachmentId,
'id' => $attachment->id,
'name' => $attachmentData['name'],
'url' => $attachmentData['source']
]

View File

@@ -14,4 +14,10 @@ class Attachment extends Model
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
protected $defaultSoftDelete = 0;
public static function addAttachment($attachmentData)
{
return self::create($attachmentData);
}
}

View File

@@ -12,6 +12,7 @@ class AliyunOSS
const ACCESS_KEY_SECRET = '0WUo8r6BT4I8ZVUQxflmD8rLHrFNHO';
const ENDPOINT = 'oss-cn-shenzhen.aliyuncs.com';
const BUCKET = 'karuosiyujzk';
const ossUrl = 'https://res.quwanzhi.com';
/**
* 获取OSS客户端实例