Compare commits

..

4 Commits

10 changed files with 57 additions and 46 deletions

View File

@@ -13,15 +13,17 @@ database:
host: "116.204.74.41"
port: "15432"
user: "postgres"
pass: "123456"
pass: "Bjang09@686^*^"
name: "oss"
role: "master"
maxIdle: "5"
maxOpen: "20"
maxLifetime: "60s"
charset: "utf8mb4" #数据库编码
debug: true
dryRun: false #空跑
role: "master" # (可选)数据库主从角色(master/slave)默认为master。如果不使用应用主从机制请不配置或留空即可。
debug: false # (可选)开启调试模式
dryRun: false # (可选)ORM空跑(只读不写)
charset: "utf8" # (可选)数据库编码(如: utf8mb4/utf8/gbk/gb2312)一般设置为utf8mb4。默认为utf8。
timezone: "Asia/Shanghai" # (可选)时区配置,例如:Local
maxIdle: 5 # (可选)连接池最大闲置的连接数(默认10)
maxOpen: 20 # (可选)连接池最大打开的连接数(默认无限制)
maxLifetime: "30s" # (可选)连接对象可重复使用的时间长度(默认30秒)
maxIdleConnTime: "30s" # (可选v2.10新增)连接池中空闲连接的最大生存时间(默认30秒)。可以通过配置文件或SetConnMaxIdleTime方法设置避免长时间空闲连接占用资源。
createdAt: "created_at" # (可选)自动创建时间字段名称
updatedAt: "updated_at" # (可选)自动更新时间字段名称
deletedAt: "deleted_at" # (可选)软删除时间字段名称
@@ -30,15 +32,17 @@ database:
host: "116.204.74.41"
port: "15432"
user: "postgres"
pass: "123456"
pass: "Bjang09@686^*^"
name: "oss"
role: "slave"
maxIdle: "5"
maxOpen: "20"
maxLifetime: "60s"
charset: "utf8mb4" #数据库编码
debug: true
dryRun: false #空跑
role: "slave" # (可选)数据库主从角色(master/slave)默认为master。如果不使用应用主从机制请不配置或留空即可。
debug: false # (可选)开启调试模式
dryRun: false # (可选)ORM空跑(只读不写)
charset: "utf8" # (可选)数据库编码(如: utf8mb4/utf8/gbk/gb2312)一般设置为utf8mb4。默认为utf8。
timezone: "Asia/Shanghai" # (可选)时区配置,例如:Local
maxIdle: 5 # (可选)连接池最大闲置的连接数(默认10)
maxOpen: 20 # (可选)连接池最大打开的连接数(默认无限制)
maxLifetime: "30s" # (可选)连接对象可重复使用的时间长度(默认30秒)
maxIdleConnTime: "30s" # (可选v2.10新增)连接池中空闲连接的最大生存时间(默认30秒)。可以通过配置文件或SetConnMaxIdleTime方法设置避免长时间空闲连接占用资源。
createdAt: "created_at" # (可选)自动创建时间字段名称
updatedAt: "updated_at" # (可选)自动更新时间字段名称
deletedAt: "deleted_at" # (可选)软删除时间字段名称
@@ -71,7 +75,7 @@ minio:
region: "us-east-1" # 与 MinIO 服务端 REGION 一致(默认 us-east-1
# 文件上传服务地址与oss模块minio中的endpoint一致
filePrefix: "116.204.74.41:9000"
filePrefix: "http://116.204.74.41:9000"
# 文件存储初始化容量大小配置
oss:

View File

@@ -1,7 +0,0 @@
package consts
// MongoDB集合名称常量
const (
FileCollection = "file"
TenantOssTotalCollection = "tenant_oss_total"
)

7
consts/table_name.go Normal file
View File

@@ -0,0 +1,7 @@
package consts
// 数据库表名
const (
TableNameFile = "file"
TableNameTenantOssTotal = "tenant_oss_total"
)

View File

@@ -20,7 +20,7 @@ func (d *file) Insert(ctx context.Context, req *dto.UploadFile) (id int64, err e
if err = gconv.Struct(req, &res); err != nil {
return
}
r, err := gfdb.DB(ctx).Model(ctx, consts.FileCollection).Insert(&res)
r, err := gfdb.DB(ctx).Model(ctx, consts.TableNameFile).Insert(&res)
if err != nil {
return
}

View File

@@ -20,7 +20,7 @@ func (d *tenantOssTotal) SaveOrUpdate(ctx context.Context, updateData []*dto.Upd
if err = gconv.Structs(updateData, &res); err != nil {
return
}
r, err := gfdb.DB(ctx).Model(ctx, consts.TenantOssTotalCollection).Data(res).OnConflict(entity.TenantOssCol.TenantId).Save()
r, err := gfdb.DB(ctx).Model(ctx, consts.TableNameTenantOssTotal).Data(res).OnConflict(entity.TenantOssCol.TenantId).Save()
if err != nil {
return
}
@@ -28,7 +28,7 @@ func (d *tenantOssTotal) SaveOrUpdate(ctx context.Context, updateData []*dto.Upd
}
func (d *tenantOssTotal) GetOneByTenantId(ctx context.Context, req *dto.GetByTenantIdReq) (res *entity.TenantOssTotal, err error) {
model := gfdb.DB(ctx).Model(ctx, consts.TenantOssTotalCollection).Where(entity.TenantOssCol.TenantId, req.TenantId)
model := gfdb.DB(ctx).Model(ctx, consts.TableNameTenantOssTotal).Where(entity.TenantOssCol.TenantId, req.TenantId)
record, err := model.One()
if err != nil {
return

View File

@@ -20,5 +20,8 @@ type UploadFile struct {
// UploadFileRes 上传文件响应
type UploadFileRes struct {
FileURL string `json:"fileURL" dc:"上传地址"`
FileSize int `json:"fileSize" dc:"文件大小"`
FileName string `json:"fileName" dc:"文件名称"`
FileFormat string `json:"fileFormat" dc:"文件格式"`
FileAddressPrefix string `json:"fileAddressPrefix"`
}

View File

@@ -8,21 +8,18 @@ import (
type File struct {
beans.SQLBaseDO `orm:",inherit"` // 嵌入基础字段Id, Bid, Creator, CreatedAt, Updater, UpdatedAt, Deleter, DeletedAt, IsDeleted
// 业务字段
TenantId uint64 `orm:"tenant_id" json:"tenantId"` // 租户ID
FileURL string `orm:"file_url" json:"fileURL"` // 文件URL
FileSize int `orm:"file_size" json:"fileSize"`
}
type fileCol struct {
beans.SQLBaseCol
TenantId string
FileURL string
FileSize string
}
var FileCol = fileCol{
SQLBaseCol: beans.DefSQLBaseCol,
TenantId: "tenant_id",
FileURL: "file_url",
FileSize: "file_size",
}

View File

@@ -8,21 +8,18 @@ import (
type TenantOssTotal struct {
beans.SQLBaseDO `orm:",inherit"` // 嵌入基础字段Id, Creator, CreatedAt, Updater, UpdatedAt, TenantId, IsDeleted
// 基础信息
TenantId uint64 `orm:"tenant_id" json:"tenantId"` // 租户ID
UsedOssSize int `orm:"used_oss_size" json:"usedOssSize"`
TotalOssSize int `orm:"total_oss_size" json:"totalOssSize"`
}
type tenantOssCol struct {
beans.SQLBaseCol
TenantId string
UsedOssSize string
TotalOssSize string
}
var TenantOssCol = tenantOssCol{
SQLBaseCol: beans.DefSQLBaseCol,
TenantId: "tenant_id",
UsedOssSize: "used_oss_size",
TotalOssSize: "total_oss_size",
}

View File

@@ -39,7 +39,7 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
// 获取redis-租户存储-锁key
fileLockKey := fmt.Sprintf(consts.FileLockKey, gconv.String(user.TenantId))
success, err := redis.Lock(ctx, fileLockKey, gconv.Int64(time.Minute*1), func(ctx context.Context) error {
success, err := utils.Lock(ctx, fileLockKey, gconv.Int64(time.Minute*1), func(ctx context.Context) error {
// 获取redis-租户存储容量总数
get, err := redis.RedisClient().Get(ctx, tenantOssTotalKey)
if err != nil {
@@ -100,7 +100,7 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
return nil, gerror.New("存储服务内存不足")
}
// 上传图片
fileURL, err := minio.UploadFile(ctx, req.File)
fileURL, fileName, fileFormat, err := minio.UploadFile(ctx, req.File)
if err != nil {
glog.Errorf(ctx, "上传图片失败: %v", err)
return nil, err
@@ -115,8 +115,16 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
return nil, err
}
// 返回图片url
return &dto.UploadFileRes{
res = &dto.UploadFileRes{
FileURL: fileURL,
FileAddressPrefix: minio.GetFileAddressPrefix(ctx),
}, err
FileSize: fileSize,
FileName: fileName,
FileFormat: fileFormat,
}
url, err := utils.GetFileAddressPrefix(ctx)
if err != nil {
return
}
res.FileAddressPrefix = url
return
}

View File

@@ -40,10 +40,12 @@ func (s *tenantOssTotal) UpdateUsedOssSize(ctx context.Context) (err error) {
updateData = append(updateData, e)
}
if len(updateData) != 0 {
// 更新数据库
_, err = dao.TenantOssTotal.SaveOrUpdate(ctx, updateData)
if err != nil {
return err
}
}
return err
}