feat:获取资源详情
This commit is contained in:
parent
d5e5cdc8f0
commit
7b32eb9154
|
@ -1,21 +1,16 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fusenapi/model/gmodel"
|
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
"fusenapi/utils/file"
|
"fusenapi/utils/file"
|
||||||
"fusenapi/utils/hash"
|
"fusenapi/utils/hash"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -66,91 +61,35 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定义存储桶名称
|
// 根据hash 查询数据资源
|
||||||
var bucketName *string
|
|
||||||
var apiType int64 = req.ApiType
|
|
||||||
|
|
||||||
// 根据类别选择存储桶
|
|
||||||
switch req.UploadBucket {
|
|
||||||
case 2:
|
|
||||||
bucketName = basic.TempfileBucketName
|
|
||||||
default:
|
|
||||||
bucketName = basic.StorageBucketName
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置AWS会话的区域
|
|
||||||
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
|
||||||
|
|
||||||
// 创建新的S3服务实例
|
|
||||||
svc := s3.New(l.svcCtx.AwsSession)
|
|
||||||
|
|
||||||
// 定义S3请求和当前时间
|
|
||||||
var s3req *request.Request
|
|
||||||
|
|
||||||
var resourceId string = hash.JsonHashKey(req.FileKey)
|
var resourceId string = hash.JsonHashKey(req.FileKey)
|
||||||
|
|
||||||
var uploadUrl = UploadUrl{}
|
// 上传文件
|
||||||
resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn)
|
var upload = file.Upload{
|
||||||
resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId)
|
Ctx: l.ctx,
|
||||||
if err == nil && resourceInfo.ResourceId != "" {
|
MysqlConn: l.svcCtx.MysqlConn,
|
||||||
uploadUrl.Status = 1
|
AwsSession: l.svcCtx.AwsSession,
|
||||||
uploadUrl.ResourceId = resourceId
|
}
|
||||||
uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl
|
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||||||
} else {
|
FileHash: resourceId,
|
||||||
dist, contentType, err := file.FileBase64ToByte(req.FileData)
|
FileData: req.FileData,
|
||||||
|
UploadBucket: req.UploadBucket,
|
||||||
|
ApiType: req.ApiType,
|
||||||
|
UserId: userId,
|
||||||
|
GuestId: guestId,
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,base64tobyte error")
|
return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed")
|
||||||
}
|
|
||||||
|
|
||||||
// 创建S3对象存储请求
|
|
||||||
s3req, _ = svc.PutObjectRequest(
|
|
||||||
&s3.PutObjectInput{
|
|
||||||
Bucket: bucketName,
|
|
||||||
Key: &resourceId,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// 设置请求体为文件数据
|
|
||||||
s3req.SetBufferBody(dist)
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
err = s3req.Send()
|
|
||||||
|
|
||||||
// 检查是否有错误
|
|
||||||
if err != nil {
|
|
||||||
logx.Error(err)
|
|
||||||
uploadUrl.Status = 0
|
|
||||||
} else {
|
|
||||||
var url = s3req.HTTPRequest.URL.String()
|
|
||||||
// 打印请求URL
|
|
||||||
logx.Info(url)
|
|
||||||
uploadUrl.Status = 1
|
|
||||||
uploadUrl.ResourceId = resourceId
|
|
||||||
uploadUrl.ResourceUrl = url
|
|
||||||
var version string = "0.0.1"
|
|
||||||
var nowTime = time.Now()
|
|
||||||
_, err = resourceModel.Create(l.ctx, &gmodel.FsResource{
|
|
||||||
ResourceId: resourceId,
|
|
||||||
UserId: &userId,
|
|
||||||
GuestId: &guestId,
|
|
||||||
ResourceType: &contentType,
|
|
||||||
ResourceUrl: &url,
|
|
||||||
Version: &version,
|
|
||||||
UploadedAt: &nowTime,
|
|
||||||
Metadata: &req.Metadata,
|
|
||||||
ApiType: &apiType,
|
|
||||||
BucketName: bucketName,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logx.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回成功的响应和上传URL
|
// 返回成功的响应和上传URL
|
||||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||||
"upload_data": uploadUrl,
|
"upload_data": UploadUrl{
|
||||||
|
Status: 1,
|
||||||
|
ResourceId: uploadRes.ResourceId,
|
||||||
|
ResourceUrl: uploadRes.ResourceUrl,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,63 +65,71 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
|
||||||
|
|
||||||
var uploadBaseRes = UploadBaseRes{}
|
var uploadBaseRes = UploadBaseRes{}
|
||||||
resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn)
|
resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn)
|
||||||
resourceInfo, err := resourceModel.FindOneById(upload.Ctx, resourceId)
|
err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error {
|
||||||
if err == nil && resourceInfo.ResourceId != "" {
|
resourceModelTS := gmodel.NewFsResourceModel(connGorm)
|
||||||
uploadBaseRes.Status = 1
|
resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId)
|
||||||
uploadBaseRes.ResourceId = resourceId
|
if err == nil && resourceInfo.ResourceId != "" {
|
||||||
uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl
|
|
||||||
} else {
|
|
||||||
dist, contentType, err := FileBase64ToByte(req.FileData)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoGet.mysql")
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建S3对象存储请求
|
|
||||||
s3req, _ = svc.PutObjectRequest(
|
|
||||||
&s3.PutObjectInput{
|
|
||||||
Bucket: bucketName,
|
|
||||||
Key: &resourceId,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// 设置请求体为文件数据
|
|
||||||
s3req.SetBufferBody(dist)
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
err = s3req.Send()
|
|
||||||
|
|
||||||
// 检查是否有错误
|
|
||||||
if err != nil {
|
|
||||||
logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.s3req")
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
var url = s3req.HTTPRequest.URL.String()
|
|
||||||
// 打印请求URL
|
|
||||||
logx.Info(url)
|
|
||||||
uploadBaseRes.Status = 1
|
uploadBaseRes.Status = 1
|
||||||
uploadBaseRes.ResourceId = resourceId
|
uploadBaseRes.ResourceId = resourceId
|
||||||
uploadBaseRes.ResourceUrl = url
|
uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl
|
||||||
var version string = "0.0.1"
|
} else {
|
||||||
var nowTime = time.Now()
|
dist, contentType, err := FileBase64ToByte(req.FileData)
|
||||||
_, err = resourceModel.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 {
|
if err != nil {
|
||||||
logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql")
|
logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoGet.mysql")
|
||||||
return nil, err
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建S3对象存储请求
|
||||||
|
s3req, _ = svc.PutObjectRequest(
|
||||||
|
&s3.PutObjectInput{
|
||||||
|
Bucket: bucketName,
|
||||||
|
Key: &resourceId,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// 设置请求体为文件数据
|
||||||
|
s3req.SetBufferBody(dist)
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
err = s3req.Send()
|
||||||
|
|
||||||
|
// 检查是否有错误
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("err:%+v,desc:%+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:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql")
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &uploadBaseRes, err
|
return &uploadBaseRes, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user