2023-07-06 10:23:43 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
2023-08-10 07:23:28 +00:00
|
|
|
|
"fusenapi/utils/file"
|
|
|
|
|
"fusenapi/utils/hash"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2023-11-20 06:48:00 +00:00
|
|
|
|
"time"
|
2023-07-06 10:23:43 +00:00
|
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/upload/internal/svc"
|
|
|
|
|
"fusenapi/server/upload/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UploadFileBackendLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
2023-08-10 07:23:28 +00:00
|
|
|
|
r *http.Request
|
2023-07-06 10:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
func NewUploadFileBackendLogic(r *http.Request, svcCtx *svc.ServiceContext) *UploadFileBackendLogic {
|
2023-07-06 10:23:43 +00:00
|
|
|
|
return &UploadFileBackendLogic{
|
2023-08-10 07:23:28 +00:00
|
|
|
|
Logger: logx.WithContext(r.Context()),
|
|
|
|
|
ctx: r.Context(),
|
2023-07-06 10:23:43 +00:00
|
|
|
|
svcCtx: svcCtx,
|
2023-08-10 07:23:28 +00:00
|
|
|
|
r: r,
|
2023-07-06 10:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
|
// func (l *UploadFileBackendLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// }
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
|
// func (l *UploadFileBackendLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
func (l *UploadFileBackendLogic) UploadFileBackend(req *types.UploadFileBackendReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-07-06 10:23:43 +00:00
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
|
// userinfo 传入值时, 一定不为null
|
2023-08-15 08:14:12 +00:00
|
|
|
|
var userId int64 = 0
|
|
|
|
|
var guestId int64 = 0
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
|
|
|
|
// 检查用户是否是游客
|
2023-08-15 08:14:12 +00:00
|
|
|
|
if userinfo != nil {
|
|
|
|
|
if userinfo.IsGuest() {
|
|
|
|
|
// 如果是,使用游客ID和游客键名格式
|
|
|
|
|
guestId = userinfo.GuestId
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,使用用户ID和用户键名格式
|
|
|
|
|
userId = userinfo.UserId
|
|
|
|
|
}
|
2023-07-06 10:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
//设置内存大小
|
|
|
|
|
l.r.ParseMultipartForm(32 << 20)
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
fileObject, _, err := l.r.FormFile("file")
|
2023-10-13 09:44:02 +00:00
|
|
|
|
//fmt.Printf("%#v", fileObject)
|
2023-08-10 07:23:28 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
|
2023-07-06 10:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
// 读取数据流
|
|
|
|
|
ioData, err := io.ReadAll(fileObject)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
|
2023-07-07 06:49:02 +00:00
|
|
|
|
}
|
2023-07-06 10:23:43 +00:00
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
// 上传文件
|
|
|
|
|
var upload = file.Upload{
|
|
|
|
|
Ctx: l.ctx,
|
|
|
|
|
MysqlConn: l.svcCtx.MysqlConn,
|
|
|
|
|
AwsSession: l.svcCtx.AwsSession,
|
|
|
|
|
}
|
2023-10-25 11:15:07 +00:00
|
|
|
|
var resourceId string
|
|
|
|
|
if len(req.FileKey) == 64 {
|
|
|
|
|
resourceId = req.FileKey
|
|
|
|
|
} else {
|
|
|
|
|
resourceId = hash.JsonHashKey(req.FileKey)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 07:23:28 +00:00
|
|
|
|
uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{
|
|
|
|
|
FileHash: resourceId,
|
|
|
|
|
FileByte: ioData,
|
|
|
|
|
UploadBucket: req.UploadBucket,
|
|
|
|
|
ApiType: req.ApiType,
|
|
|
|
|
UserId: userId,
|
|
|
|
|
GuestId: guestId,
|
2023-08-17 03:46:17 +00:00
|
|
|
|
Source: req.Source,
|
2023-08-23 09:51:51 +00:00
|
|
|
|
Metadata: req.Metadata,
|
|
|
|
|
Refresh: req.Refresh,
|
|
|
|
|
ResourceId: req.ResourceId,
|
2023-08-10 07:23:28 +00:00
|
|
|
|
})
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
2023-07-06 10:23:43 +00:00
|
|
|
|
if err != nil {
|
2023-07-07 06:49:02 +00:00
|
|
|
|
logx.Error(err)
|
2023-08-10 07:23:28 +00:00
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed")
|
2023-07-06 10:23:43 +00:00
|
|
|
|
}
|
2023-07-18 05:04:29 +00:00
|
|
|
|
|
2023-11-20 06:34:12 +00:00
|
|
|
|
// 是否备份--异步任务
|
2023-11-20 06:48:00 +00:00
|
|
|
|
if req.BackupType != "" {
|
|
|
|
|
time.AfterFunc(time.Second*1, func() {
|
|
|
|
|
upload.UploadType = "oss"
|
|
|
|
|
upload.AliyunOSS.Endpoint = l.svcCtx.Config.AliyunOSS.Endpoint
|
|
|
|
|
upload.AliyunOSS.AccessKeyId = l.svcCtx.Config.AliyunOSS.AccessKeyId
|
|
|
|
|
upload.AliyunOSS.AccessKeySecret = l.svcCtx.Config.AliyunOSS.AccessKeySecret
|
|
|
|
|
upload.AliyunOSS.BucketName = l.svcCtx.Config.AliyunOSS.BucketName
|
|
|
|
|
upload.AliyunOSS.BucketUrl = l.svcCtx.Config.AliyunOSS.BucketUrl
|
|
|
|
|
upload.AliyunOSS.BasePath = l.svcCtx.Config.AliyunOSS.BasePath
|
|
|
|
|
|
|
|
|
|
err = upload.UploadFileByUrl(resourceId, uploadRes.ResourceUrl)
|
|
|
|
|
})
|
|
|
|
|
}
|
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{}{
|
2023-08-10 07:23:28 +00:00
|
|
|
|
"upload_data": UploadUrl{
|
|
|
|
|
Status: 1,
|
|
|
|
|
ResourceId: uploadRes.ResourceId,
|
|
|
|
|
ResourceUrl: uploadRes.ResourceUrl,
|
|
|
|
|
},
|
2023-07-06 10:23:43 +00:00
|
|
|
|
})
|
|
|
|
|
}
|