oss文件存储服务-租户存储空间信息同步接口优化
This commit is contained in:
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"oss/consts"
|
||||
"oss/dao"
|
||||
"oss/model/dto"
|
||||
@@ -23,11 +24,12 @@ type file struct{}
|
||||
var File = new(file)
|
||||
|
||||
func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto.UploadFileRes, err error) {
|
||||
fileSize := gconv.Byte(req.File.Size)
|
||||
totalFileSize := int64(0)
|
||||
fileSize := gconv.Int(req.File.Size)
|
||||
totalFileSize := 0
|
||||
// 获取租户id
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "获取用户信息失败: %v", err)
|
||||
return
|
||||
}
|
||||
tenantId := gconv.String(user.TenantId)
|
||||
@@ -40,6 +42,7 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
// 获取redis-租户存储容量总数
|
||||
get, err := redis.RedisClient.Get(ctx, tenantOssTotalKey)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "获取redis-租户存储容量总数失败: %v", err)
|
||||
return err
|
||||
}
|
||||
tenantOssTotalEntity := &entity.TenantOssTotal{}
|
||||
@@ -50,33 +53,39 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
}
|
||||
tenantOssTotal, err := TenantOssTotal.GetOneByTenantId(ctx, getByTenantIdReq)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "查询数据库-获取租户存储容量总数失败: %v", err)
|
||||
return err
|
||||
}
|
||||
if tenantOssTotal.Id.IsZero() {
|
||||
tenantOssTotalEntity.TenantId = user.TenantId
|
||||
tenantOssTotalEntity.UsedOssSize = int64(0)
|
||||
tenantOssTotalEntity.TotalOssSize = g.Cfg().MustGet(ctx, "oss.capacitySize").Int64()
|
||||
tenantOssTotalEntity.UsedOssSize = 0
|
||||
tenantOssTotalEntity.TotalOssSize = g.Cfg().MustGet(ctx, "oss.capacitySize").Int() * 1024 * 1024 * 1024
|
||||
} else {
|
||||
tenantOssTotalEntity = tenantOssTotal.TenantOssTotal
|
||||
}
|
||||
} else {
|
||||
// 反序列化-redis获取租户存储容量总数
|
||||
err = gconv.Struct(get, tenantOssTotalEntity)
|
||||
if err != nil {
|
||||
if err = gconv.Struct(get, tenantOssTotalEntity); err != nil {
|
||||
glog.Errorf(ctx, "反序列化-redis获取租户存储容量总数失败: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
tenantId = gconv.String(tenantOssTotalEntity.TenantId)
|
||||
fileSize = gconv.Byte(tenantOssTotalEntity.UsedOssSize) + fileSize
|
||||
fileSize = tenantOssTotalEntity.UsedOssSize + fileSize
|
||||
totalFileSize = tenantOssTotalEntity.TotalOssSize
|
||||
// 设置redis-租户存储容量总数
|
||||
tenantOssTotalKeyMap := map[string]interface{}{"tenantId": tenantId, "UsedOssSize": fileSize, "TotalOssSize": totalFileSize}
|
||||
tenantOssTotalKeyMap := dto.TenantOssTotal{
|
||||
TenantId: tenantId,
|
||||
UsedOssSize: fileSize,
|
||||
TotalOssSize: totalFileSize,
|
||||
Updater: gconv.String(user.UserName),
|
||||
}
|
||||
// 修改redis-租户存储容量总数 超时时间10分钟
|
||||
err = redis.RedisClient.SetEX(ctx, tenantOssTotalKey, tenantOssTotalKeyMap, gconv.Int64(time.Minute*10))
|
||||
if err != nil {
|
||||
if err = redis.RedisClient.SetEX(ctx, tenantOssTotalKey, tenantOssTotalKeyMap, gconv.Int64(time.Minute*10)); err != nil {
|
||||
glog.Errorf(ctx, "修改redis-租户存储容量总数 超时时间10分钟失败: %v", err)
|
||||
return err
|
||||
}
|
||||
if fileSize > gconv.Byte(totalFileSize) {
|
||||
if fileSize > totalFileSize {
|
||||
return gerror.New("存储服务内存不足")
|
||||
}
|
||||
return nil
|
||||
@@ -88,8 +97,9 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
return nil, gerror.New("存储服务内存不足")
|
||||
}
|
||||
// 上传图片
|
||||
fileURL, err := minio.UploadImage(ctx, req.File)
|
||||
fileURL, err := minio.UploadFile(ctx, req.File)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "上传图片失败: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
// 插入数据库
|
||||
@@ -104,6 +114,6 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
// 返回图片url
|
||||
return &dto.UploadFileRes{
|
||||
FileURL: fileURL,
|
||||
FileAddressPrefix: minio.GetImgAddressPrefix(ctx),
|
||||
FileAddressPrefix: minio.GetIFileAddressPrefix(ctx),
|
||||
}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user