2023-07-06 10:23:43 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
|
"fusenapi/utils/check"
|
|
|
|
|
"fusenapi/utils/format"
|
2023-07-07 06:49:02 +00:00
|
|
|
|
"log"
|
2023-07-06 10:23:43 +00:00
|
|
|
|
"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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileBackend, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
|
if userinfo.IsOnlooker() {
|
|
|
|
|
return resp.SetStatus(basic.CodeUnAuth)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uid int64
|
|
|
|
|
var keytype format.TypeFormatS3KeyName
|
|
|
|
|
if userinfo.IsGuest() {
|
|
|
|
|
uid = userinfo.GuestId
|
|
|
|
|
keytype = format.TypeS3KeyGuest
|
|
|
|
|
} else {
|
|
|
|
|
uid = userinfo.UserId
|
|
|
|
|
keytype = format.TypeS3KeyUser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
|
|
|
|
svc := s3.New(l.svcCtx.AwsSession)
|
|
|
|
|
|
|
|
|
|
if !check.CheckCategory(req.Category) {
|
|
|
|
|
return resp.SetStatus(basic.CodeS3CategoryErr)
|
|
|
|
|
}
|
|
|
|
|
|
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-07 06:49:02 +00:00
|
|
|
|
category := format.TypeCategory(req.Category)
|
|
|
|
|
ObjectKey := aws.String(format.FormatS3KeyName(
|
|
|
|
|
keytype,
|
|
|
|
|
uid,
|
|
|
|
|
now,
|
|
|
|
|
l.svcCtx.Config.Env,
|
|
|
|
|
category,
|
|
|
|
|
req.File.Filename,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
switch category {
|
|
|
|
|
case format.TCategoryRenderMegre:
|
|
|
|
|
|
|
|
|
|
lifecycleConfiguration := &s3.BucketLifecycleConfiguration{
|
|
|
|
|
Rules: []*s3.LifecycleRule{
|
|
|
|
|
{
|
|
|
|
|
Status: aws.String("Enabled"),
|
|
|
|
|
// Status: aws.String("Disabled"),
|
|
|
|
|
Filter: &s3.LifecycleRuleFilter{
|
|
|
|
|
Prefix: ObjectKey,
|
|
|
|
|
},
|
|
|
|
|
Expiration: &s3.LifecycleExpiration{
|
|
|
|
|
// Date: aws.Time(time.Now().UTC().Add(time.Minute)),
|
|
|
|
|
Days: aws.Int64(1), // 设置过期天数,例如:30天
|
|
|
|
|
},
|
|
|
|
|
// ID: aws.String("ExpireAfter01Days"), // 设置规则的唯一标识符
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s3req, _ = svc.PutBucketLifecycleConfigurationRequest(
|
|
|
|
|
&s3.PutBucketLifecycleConfigurationInput{
|
|
|
|
|
Bucket: basic.StorageBucketName,
|
|
|
|
|
LifecycleConfiguration: lifecycleConfiguration,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
default:
|
|
|
|
|
s3req, _ = svc.PutObjectRequest(
|
|
|
|
|
&s3.PutObjectInput{
|
|
|
|
|
Bucket: basic.StorageBucketName,
|
|
|
|
|
Key: ObjectKey,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
}
|
2023-07-06 10:23:43 +00:00
|
|
|
|
|
|
|
|
|
s3req.SetBufferBody(req.File.Data)
|
|
|
|
|
err := s3req.Send()
|
|
|
|
|
if err != nil {
|
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-07 06:49:02 +00:00
|
|
|
|
log.Println(s3req.HTTPRequest.URL)
|
2023-07-06 10:23:43 +00:00
|
|
|
|
|
|
|
|
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
|
|
|
|
"upload_url": s3req.HTTPRequest.URL.String(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|