2023-07-06 10:23:43 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
|
"fusenapi/utils/check"
|
|
|
|
|
"fusenapi/utils/format"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/upload/internal/svc"
|
|
|
|
|
"fusenapi/server/upload/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2023-07-07 06:49:02 +00:00
|
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
2023-07-06 10:23:43 +00:00
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UploadFileBackendLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUploadFileBackendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadFileBackendLogic {
|
|
|
|
|
return &UploadFileBackendLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// UploadFileBackend 这个函数接收一个文件上传请求和用户信息,处理文件上传,并返回响应
|
2023-07-06 10:23:43 +00:00
|
|
|
|
func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileBackend, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
2023-07-06 10:23:43 +00:00
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
|
// userinfo 传入值时, 一定不为null
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 检查用户是否是旁观者,旁观者没有文件上传权限
|
2023-07-06 10:23:43 +00:00
|
|
|
|
if userinfo.IsOnlooker() {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 如果是,返回未授权的错误码
|
2023-07-06 10:23:43 +00:00
|
|
|
|
return resp.SetStatus(basic.CodeUnAuth)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 定义用户ID和S3键名格式
|
2023-07-06 10:23:43 +00:00
|
|
|
|
var uid int64
|
|
|
|
|
var keytype format.TypeFormatS3KeyName
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 检查用户是否是游客
|
2023-07-06 10:23:43 +00:00
|
|
|
|
if userinfo.IsGuest() {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 如果是,使用游客ID和游客键名格式
|
2023-07-06 10:23:43 +00:00
|
|
|
|
uid = userinfo.GuestId
|
|
|
|
|
keytype = format.TypeS3KeyGuest
|
|
|
|
|
} else {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 否则,使用用户ID和用户键名格式
|
2023-07-06 10:23:43 +00:00
|
|
|
|
uid = userinfo.UserId
|
|
|
|
|
keytype = format.TypeS3KeyUser
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 设置AWS会话的区域
|
2023-07-06 10:23:43 +00:00
|
|
|
|
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 创建新的S3服务实例
|
2023-07-06 10:23:43 +00:00
|
|
|
|
svc := s3.New(l.svcCtx.AwsSession)
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 检查类别是否合法
|
2023-07-06 10:23:43 +00:00
|
|
|
|
if !check.CheckCategory(req.Category) {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 如果不合法,返回类别错误的错误码
|
2023-07-06 10:23:43 +00:00
|
|
|
|
return resp.SetStatus(basic.CodeS3CategoryErr)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 定义S3请求和当前时间
|
2023-07-07 06:49:02 +00:00
|
|
|
|
var s3req *request.Request
|
2023-07-06 10:23:43 +00:00
|
|
|
|
now := time.Now()
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 格式化类别
|
2023-07-07 06:49:02 +00:00
|
|
|
|
category := format.TypeCategory(req.Category)
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 格式化S3对象键名
|
2023-07-07 06:49:02 +00:00
|
|
|
|
ObjectKey := aws.String(format.FormatS3KeyName(
|
|
|
|
|
keytype,
|
|
|
|
|
uid,
|
|
|
|
|
now,
|
|
|
|
|
l.svcCtx.Config.Env,
|
|
|
|
|
category,
|
|
|
|
|
req.File.Filename,
|
|
|
|
|
))
|
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 定义存储桶名称
|
2023-07-07 09:49:12 +00:00
|
|
|
|
var bucketName *string
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 根据类别选择存储桶
|
2023-07-07 06:49:02 +00:00
|
|
|
|
switch category {
|
|
|
|
|
case format.TCategoryRenderMegre:
|
2023-07-07 09:49:12 +00:00
|
|
|
|
bucketName = basic.TempfileBucketName
|
2023-07-07 06:49:02 +00:00
|
|
|
|
default:
|
2023-07-07 09:49:12 +00:00
|
|
|
|
bucketName = basic.StorageBucketName
|
2023-07-07 06:49:02 +00:00
|
|
|
|
}
|
2023-07-06 10:23:43 +00:00
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 创建S3对象存储请求
|
2023-07-07 09:49:12 +00:00
|
|
|
|
s3req, _ = svc.PutObjectRequest(
|
|
|
|
|
&s3.PutObjectInput{
|
|
|
|
|
Bucket: bucketName,
|
|
|
|
|
Key: ObjectKey,
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 设置请求体为文件数据
|
2023-07-06 10:23:43 +00:00
|
|
|
|
s3req.SetBufferBody(req.File.Data)
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 发送请求
|
2023-07-06 10:23:43 +00:00
|
|
|
|
err := s3req.Send()
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 检查是否有错误
|
2023-07-06 10:23:43 +00:00
|
|
|
|
if err != nil {
|
2023-07-18 05:04:29 +00:00
|
|
|
|
// 如果有,打印错误并返回错误码
|
2023-07-07 06:49:02 +00:00
|
|
|
|
logx.Error(err)
|
2023-07-06 10:23:43 +00:00
|
|
|
|
return resp.SetStatus(basic.CodeS3PutObjectRequestErr)
|
|
|
|
|
}
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 打印请求URL
|
2023-07-07 09:49:12 +00:00
|
|
|
|
logx.Info(s3req.HTTPRequest.URL.String())
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 返回成功的响应和上传URL
|
2023-07-06 10:23:43 +00:00
|
|
|
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
|
|
|
|
"upload_url": s3req.HTTPRequest.URL.String(),
|
|
|
|
|
})
|
|
|
|
|
}
|