Files
full/module/base/cloud/internal/logic/disk/quota.go
2026-09-22 21:15:34 +08:00

44 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package disk
import (
"bsm/full/module/base/cloud/internal/impl"
"bsm/full/module/base/cloud/internal/models"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/printer"
)
// defaultMaxStorage 默认容量配额100GB与 Space.Get 创建默认空间时的取值保持一致
const defaultMaxStorage int64 = 100 * 1024 * 1024 * 1024
// checkQuota 校验「已用空间 + 本次新增」是否超出配额,超出返回明确错误
func checkQuota(passportID uint, addSize int64) error {
if addSize <= 0 {
return nil
}
// 配额取用户空间记录的 max_storage无空间记录时使用默认配额
maxStorage := defaultMaxStorage
var space models.CloudSpace
if err := impl.DBService.Where("passport_id = ?", passportID).First(&space).Error; err == nil && space.MaxStorage > 0 {
maxStorage = space.MaxStorage
}
// 已用空间按云盘文件实际占用统计
var usedStorage int64
row := impl.DBService.Model(&models.CloudDiskFile{}).
Joins("JOIN cloud_disk_dirs ON cloud_disk_files.directory_id = cloud_disk_dirs.id").
Where("cloud_disk_dirs.passport_id = ?", passportID).
Select("COALESCE(SUM(cloud_disk_files.size), 0)").
Row()
if err := row.Scan(&usedStorage); err != nil {
printer.Error("Query used storage error: %v", err)
return errcode.ErrDB
}
if usedStorage+addSize > maxStorage {
return errcode.ErrResourceExhausted
}
return nil
}