feat:获取资源详情

This commit is contained in:
Hiven 2023-08-09 15:15:12 +08:00
parent aa96a52a5b
commit 29e42011a9
2 changed files with 112 additions and 68 deletions

View File

@ -2,22 +2,18 @@ package logic
import ( import (
"encoding/json" "encoding/json"
"fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/file"
"fusenapi/utils/hash" "fusenapi/utils/hash"
"io" "io"
"net/http" "net/http"
"time"
"context" "context"
"fusenapi/server/upload/internal/svc" "fusenapi/server/upload/internal/svc"
"fusenapi/server/upload/internal/types" "fusenapi/server/upload/internal/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/mr" "github.com/zeromicro/go-zero/core/mr"
) )
@ -101,16 +97,6 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
//获取上传的文件组 //获取上传的文件组
files := l.r.MultipartForm.File["file"] files := l.r.MultipartForm.File["file"]
// 设置AWS会话的区域
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
// 创建新的S3服务实例
svc := s3.New(l.svcCtx.AwsSession)
// 定义S3请求和当前时间
var s3req *request.Request
resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn)
result, err := mr.MapReduce(func(source chan<- interface{}) { result, err := mr.MapReduce(func(source chan<- interface{}) {
for i, info := range uploadInfoList { for i, info := range uploadInfoList {
fileType := files[i].Header.Get("Content-Type") fileType := files[i].Header.Get("Content-Type")
@ -148,60 +134,28 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq,
uploadUrl.ResourceType = uploadDataInfo.FileType uploadUrl.ResourceType = uploadDataInfo.FileType
var resourceId string = uploadDataInfo.HashKey var resourceId string = uploadDataInfo.HashKey
// 查询数据库
resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
uploadUrl.Status = 1
uploadUrl.ResourceId = resourceId
uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl
} else {
// 创建S3对象存储请求
s3req, _ = svc.PutObjectRequest(
&s3.PutObjectInput{
Bucket: uploadDataInfo.Bucket,
Key: &uploadDataInfo.HashKey,
},
)
// 设置请求体为文件数据 // 上传文件
s3req.SetBufferBody(uploadDataInfo.FileData) var upload = file.Upload{
Ctx: l.ctx,
// 发送请求 MysqlConn: l.svcCtx.MysqlConn,
err = s3req.Send() AwsSession: l.svcCtx.AwsSession,
// 检查是否有错误 }
if err != nil { uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{
logx.Error(err) FileHash: resourceId,
uploadUrl.Status = 0 FileByte: uploadDataInfo.FileData,
} else { UploadBucket: 1,
contentType := http.DetectContentType(uploadDataInfo.FileData) ApiType: 2,
var url = s3req.HTTPRequest.URL.String() UserId: userId,
// 打印请求URL GuestId: guestId,
logx.Info(url) })
uploadUrl.Status = 1 if err == nil {
uploadUrl.ResourceId = resourceId uploadUrl.ResourceId = uploadRes.ResourceId
uploadUrl.ResourceUrl = url uploadUrl.ResourceType = uploadRes.ResourceType
var version string = "0.0.1" uploadUrl.ResourceUrl = uploadRes.ResourceUrl
var nowTime = time.Now() // Notice 这个必须加!
_, err = resourceModel.Create(l.ctx, &gmodel.FsResource{ writer.Write(uploadUrl)
ResourceId: resourceId,
UserId: &userId,
GuestId: &guestId,
ResourceType: &contentType,
ResourceUrl: &url,
Version: &version,
UploadedAt: &nowTime,
Metadata: &uploadDataInfo.Metadata,
ApiType: &uploadDataInfo.ApiType,
BucketName: bucketName,
})
if err != nil {
logx.Error(err)
}
}
} }
// Notice 这个必须加!
writer.Write(uploadUrl)
}, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) { }, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) {
var uploadUrlList = make(map[string][]*UploadUrl) var uploadUrlList = make(map[string][]*UploadUrl)
var uploadUrlListFail []*UploadUrl var uploadUrlListFail []*UploadUrl
@ -235,7 +189,6 @@ type UploadInfo struct {
FileKeys string `json:"file_keys"` // 上传文件唯一标识 FileKeys string `json:"file_keys"` // 上传文件唯一标识
FileData *string `json:"file_data"` // 上传文件Base64 FileData *string `json:"file_data"` // 上传文件Base64
Metadata string `json:"meta_data"` // 上传文件额外信息 Metadata string `json:"meta_data"` // 上传文件额外信息
} }
type UploadData struct { type UploadData struct {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"net/http"
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -28,6 +29,7 @@ type UploadBaseReq struct {
ApiType int64 ApiType int64
UserId int64 UserId int64
GuestId int64 GuestId int64
FileByte []byte
} }
type UploadBaseRes struct { type UploadBaseRes struct {
@ -133,3 +135,92 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
return &uploadBaseRes, err return &uploadBaseRes, err
} }
func (upload *Upload) UploadFileByByte(req *UploadBaseReq) (*UploadBaseRes, error) {
// 定义存储桶名称
var bucketName *string
var apiType int64 = req.ApiType
// 根据类别选择存储桶
switch req.UploadBucket {
case 2:
bucketName = basic.TempfileBucketName
default:
bucketName = basic.StorageBucketName
}
// 设置AWS会话的区域
upload.AwsSession.Config.Region = aws.String("us-west-1")
// 创建新的S3服务实例
svc := s3.New(upload.AwsSession)
// 定义S3请求和当前时间
var s3req *request.Request
var resourceId string = req.FileHash
var uploadBaseRes = UploadBaseRes{}
resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn)
err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error {
resourceModelTS := gmodel.NewFsResourceModel(connGorm)
resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
uploadBaseRes.Status = 1
uploadBaseRes.ResourceId = resourceId
uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl
} else {
contentType := http.DetectContentType(req.FileByte)
// 创建S3对象存储请求
s3req, _ = svc.PutObjectRequest(
&s3.PutObjectInput{
Bucket: bucketName,
Key: &resourceId,
},
)
// 设置请求体为文件数据
s3req.SetBufferBody(req.FileByte)
// 发送请求
err = s3req.Send()
// 检查是否有错误
if err != nil {
logx.Errorf("err:%+vdesc:%+v", err, "fail.upload.s3req")
return err
} else {
var url = s3req.HTTPRequest.URL.String()
// 打印请求URL
logx.Info(url)
uploadBaseRes.Status = 1
uploadBaseRes.ResourceId = resourceId
uploadBaseRes.ResourceUrl = url
var version string = "0.0.1"
var nowTime = time.Now()
_, err = resourceModelTS.Create(upload.Ctx, &gmodel.FsResource{
ResourceId: resourceId,
UserId: &req.UserId,
GuestId: &req.GuestId,
ResourceType: &contentType,
ResourceUrl: &url,
Version: &version,
UploadedAt: &nowTime,
Metadata: &req.Metadata,
ApiType: &apiType,
BucketName: bucketName,
})
if err != nil {
logx.Errorf("err:%+vdesc:%+v", err, "fail.upload.resourceInfoAdd.mysql")
return err
}
}
}
return nil
})
if err != nil {
return nil, err
}
return &uploadBaseRes, err
}