2023-08-01 10:13:30 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-02 03:13:28 +00:00
|
|
|
|
"encoding/json"
|
2023-08-01 10:13:30 +00:00
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
2023-08-09 07:15:12 +00:00
|
|
|
|
"fusenapi/utils/file"
|
2023-08-02 03:13:28 +00:00
|
|
|
|
"fusenapi/utils/hash"
|
2023-08-01 10:13:30 +00:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/upload/internal/svc"
|
|
|
|
|
"fusenapi/server/upload/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/mr"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UploadFilesBackendLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
r *http.Request
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUploadFilesBackendLogic(r *http.Request, svcCtx *svc.ServiceContext) *UploadFilesBackendLogic {
|
|
|
|
|
return &UploadFilesBackendLogic{
|
|
|
|
|
Logger: logx.WithContext(r.Context()),
|
|
|
|
|
ctx: r.Context(),
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
r: r,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
|
// func (l *UploadFilesBackendLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
|
// func (l *UploadFilesBackendLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
|
if userinfo.IsOnlooker() {
|
|
|
|
|
// 如果是,返回未授权的错误码
|
|
|
|
|
return resp.SetStatus(basic.CodeUnAuth)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义用户ID和S3键名格式
|
|
|
|
|
var userId int64
|
|
|
|
|
var guestId int64
|
|
|
|
|
|
|
|
|
|
// 检查用户是否是游客
|
|
|
|
|
if userinfo.IsGuest() {
|
|
|
|
|
// 如果是,使用游客ID和游客键名格式
|
|
|
|
|
guestId = userinfo.GuestId
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,使用用户ID和用户键名格式
|
|
|
|
|
userId = userinfo.UserId
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 03:13:28 +00:00
|
|
|
|
var uploadInfoList []UploadInfo
|
|
|
|
|
err := json.Unmarshal([]byte(req.UploadInfo), &uploadInfoList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,params Unmarshal failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileLen = len(uploadInfoList)
|
2023-08-01 10:13:30 +00:00
|
|
|
|
|
|
|
|
|
if fileLen == 0 {
|
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
|
|
|
|
|
}
|
|
|
|
|
if req.ApiType == 1 && fileLen > 100 {
|
|
|
|
|
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err, files count is beyond the maximum")
|
|
|
|
|
}
|
2023-08-02 03:13:28 +00:00
|
|
|
|
|
|
|
|
|
// 定义存储桶名称
|
|
|
|
|
var bucketName *string
|
|
|
|
|
|
|
|
|
|
// 根据类别选择存储桶
|
|
|
|
|
switch req.UploadBucket {
|
|
|
|
|
case 2:
|
|
|
|
|
bucketName = basic.TempfileBucketName
|
|
|
|
|
default:
|
|
|
|
|
bucketName = basic.StorageBucketName
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 10:13:30 +00:00
|
|
|
|
//设置内存大小
|
|
|
|
|
l.r.ParseMultipartForm(32 << 20)
|
2023-08-01 11:49:56 +00:00
|
|
|
|
|
2023-08-01 10:13:30 +00:00
|
|
|
|
//获取上传的文件组
|
|
|
|
|
files := l.r.MultipartForm.File["file"]
|
|
|
|
|
|
|
|
|
|
result, err := mr.MapReduce(func(source chan<- interface{}) {
|
2023-08-02 03:13:28 +00:00
|
|
|
|
for i, info := range uploadInfoList {
|
2023-08-01 10:13:30 +00:00
|
|
|
|
fileType := files[i].Header.Get("Content-Type")
|
|
|
|
|
// 打开文件
|
|
|
|
|
file, err := files[i].Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
// 读取数据流
|
|
|
|
|
ioData, err := io.ReadAll(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 一系列业务逻辑....验证类型,文件大小
|
2023-08-02 03:13:28 +00:00
|
|
|
|
|
2023-08-08 02:53:28 +00:00
|
|
|
|
var hashKey string = hash.JsonHashKey(info.FileKeys)
|
2023-08-02 03:13:28 +00:00
|
|
|
|
source <- UploadData{
|
|
|
|
|
FileKey: info.FileKeys,
|
2023-08-01 10:13:30 +00:00
|
|
|
|
FileType: fileType,
|
|
|
|
|
Metadata: info.Metadata,
|
|
|
|
|
FileData: ioData,
|
|
|
|
|
ApiType: req.ApiType,
|
2023-08-02 03:13:28 +00:00
|
|
|
|
Bucket: bucketName,
|
|
|
|
|
HashKey: hashKey,
|
2023-08-17 03:46:17 +00:00
|
|
|
|
Source: req.Source,
|
2023-08-01 10:13:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, func(item interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
2023-08-02 03:13:28 +00:00
|
|
|
|
uploadDataInfo := item.(UploadData)
|
2023-08-01 10:13:30 +00:00
|
|
|
|
|
2023-08-02 03:30:27 +00:00
|
|
|
|
var uploadUrl = UploadUrl{}
|
|
|
|
|
uploadUrl.Key = uploadDataInfo.FileKey
|
|
|
|
|
uploadUrl.ApiType = uploadDataInfo.ApiType
|
|
|
|
|
uploadUrl.ResourceType = uploadDataInfo.FileType
|
|
|
|
|
|
2023-08-02 03:13:28 +00:00
|
|
|
|
var resourceId string = uploadDataInfo.HashKey
|
2023-08-01 10:13:30 +00:00
|
|
|
|
|
2023-08-09 07:15:12 +00:00
|
|
|
|
// 上传文件
|
|
|
|
|
var upload = file.Upload{
|
|
|
|
|
Ctx: l.ctx,
|
|
|
|
|
MysqlConn: l.svcCtx.MysqlConn,
|
|
|
|
|
AwsSession: l.svcCtx.AwsSession,
|
|
|
|
|
}
|
|
|
|
|
uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{
|
|
|
|
|
FileHash: resourceId,
|
|
|
|
|
FileByte: uploadDataInfo.FileData,
|
|
|
|
|
UploadBucket: 1,
|
2023-08-10 06:01:22 +00:00
|
|
|
|
ApiType: req.ApiType,
|
2023-08-09 07:15:12 +00:00
|
|
|
|
UserId: userId,
|
|
|
|
|
GuestId: guestId,
|
2023-08-17 03:46:17 +00:00
|
|
|
|
Source: uploadDataInfo.Source,
|
2023-08-09 07:15:12 +00:00
|
|
|
|
})
|
|
|
|
|
if err == nil {
|
2023-08-09 07:17:02 +00:00
|
|
|
|
uploadUrl.Status = 1
|
2023-08-09 07:15:12 +00:00
|
|
|
|
uploadUrl.ResourceId = uploadRes.ResourceId
|
|
|
|
|
uploadUrl.ResourceType = uploadRes.ResourceType
|
|
|
|
|
uploadUrl.ResourceUrl = uploadRes.ResourceUrl
|
|
|
|
|
// Notice 这个必须加!
|
|
|
|
|
writer.Write(uploadUrl)
|
2023-08-01 10:13:30 +00:00
|
|
|
|
}
|
|
|
|
|
}, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) {
|
2023-08-02 03:13:28 +00:00
|
|
|
|
var uploadUrlList = make(map[string][]*UploadUrl)
|
|
|
|
|
var uploadUrlListFail []*UploadUrl
|
|
|
|
|
var uploadUrlListSuccess []*UploadUrl
|
2023-08-01 10:13:30 +00:00
|
|
|
|
for p := range pipe {
|
2023-08-02 03:13:28 +00:00
|
|
|
|
var uploadUrl = p.(UploadUrl)
|
2023-08-01 10:13:30 +00:00
|
|
|
|
if uploadUrl.Status == 1 {
|
|
|
|
|
uploadUrlListSuccess = append(uploadUrlListSuccess, &uploadUrl)
|
|
|
|
|
} else {
|
|
|
|
|
uploadUrlListFail = append(uploadUrlListFail, &uploadUrl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notice 这个必须加!
|
|
|
|
|
uploadUrlList["success"] = uploadUrlListSuccess
|
|
|
|
|
uploadUrlList["fail"] = uploadUrlListFail
|
|
|
|
|
writer.Write(uploadUrlList)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回成功的响应和上传URL
|
|
|
|
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
2023-08-07 08:19:54 +00:00
|
|
|
|
"upload_data": result,
|
2023-08-01 10:13:30 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 03:13:28 +00:00
|
|
|
|
type UploadInfo struct {
|
2023-08-07 03:34:22 +00:00
|
|
|
|
FileSize int64 `json:"file_size"` // 上传文件大小
|
|
|
|
|
FileKeys string `json:"file_keys"` // 上传文件唯一标识
|
|
|
|
|
FileData *string `json:"file_data"` // 上传文件Base64
|
|
|
|
|
Metadata string `json:"meta_data"` // 上传文件额外信息
|
2023-08-02 03:13:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UploadData struct {
|
|
|
|
|
ApiType int64 `json:"api_type"`
|
|
|
|
|
FileSize int64 `json:"file_size"`
|
|
|
|
|
FileType string `json:"file_type"`
|
|
|
|
|
FileKey string `json:"file_key"`
|
|
|
|
|
Metadata string `json:"metadata"`
|
|
|
|
|
Bucket *string `json:"bucket"`
|
|
|
|
|
HashKey string `json:"hash_key"`
|
|
|
|
|
FileData []byte `fsfile:"data"`
|
2023-08-17 03:46:17 +00:00
|
|
|
|
Source string `json:"source"`
|
2023-08-01 10:13:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 03:13:28 +00:00
|
|
|
|
type UploadUrl struct {
|
2023-09-07 10:52:01 +00:00
|
|
|
|
LogoId int64 `json:"logo_id"`
|
2023-08-02 03:30:27 +00:00
|
|
|
|
Key string `json:"key"`
|
|
|
|
|
Status int64 `json:"status"`
|
|
|
|
|
ApiType int64 `json:"api_type"`
|
|
|
|
|
ResourceId string `json:"resource_id"`
|
|
|
|
|
ResourceType string `json:"resource_type"`
|
|
|
|
|
ResourceUrl string `json:"resource_url"`
|
2023-08-01 10:13:30 +00:00
|
|
|
|
}
|