修复文件上传权限问题

This commit is contained in:
junleea 2025-03-30 16:38:09 +08:00
parent 25cdf5416d
commit ac2ddb1beb
3 changed files with 29 additions and 5 deletions

View File

@ -267,3 +267,18 @@ func FindFileAuthByID(id int) FileAuth {
}
return file
}
// 获取用户文件占用空间
type UserFileSpace struct {
TotalSize int `gorm:"column:total_size"`
}
func GetUserFileSpace(userID int) UserFileSpace {
var space UserFileSpace
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Raw("SELECT SUM(file_size) as total_size FROM file_auths left join files on file_auths.file_id = files.id where file_auths.auth_id=? AND file_auths.deleted_at IS NULL", userID).Scan(&space)
} else {
DB.Raw("SELECT SUM(file_size) as total_size FROM file_auths left join files on file_auths.file_id = files.id where file_auths.auth_id= ? AND file_auths.deleted_at IS NULL ", userID).Scan(&space)
}
return space
}

View File

@ -210,9 +210,11 @@ func UploadFileV2(c *gin.Context) {
return
}
user := dao.FindUserByUserID(id1)
if user.Upload == false {
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
//查看用户上传的所有文件大小是否超过限制
userFileSpace := dao.GetUserFileSpace(id1)
user := service.GetUserByIDWithCache(id1)
if userFileSpace.TotalSize > proto.UserMaxUploadSize && user.Role != "admin" {
c.JSON(http.StatusOK, gin.H{"error": "user file space is full", "code": proto.NoUploadPermissions, "message": "failed"})
return
}
//上传文件
@ -221,6 +223,7 @@ func UploadFileV2(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"error": "upload file failed", "code": proto.UploadFileFailed, "message": "failed"})
return
}
//计算文件md5值
if md5_ == "" {
file_, _ := file.Open()
@ -234,7 +237,13 @@ func UploadFileV2(c *gin.Context) {
fileExist := dao.FindFileByMd5(md5_)
if fileExist.ID != 0 {
fileExist.FilePath = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
//添加用户文件
fileAuth := service.CreateUserFile(id1, fileExist.FileName, int(fileExist.ID), uploadType)
if fileAuth.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save user file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist, "file_auth": fileAuth})
return
}

View File

@ -145,5 +145,5 @@ const (
UserFileTypeAvatar = "avatar" // 用户头像
UserFileTypeFile = "file" // 通用文件
UserFileTypeConfig = "config" // 配置文件
UserMaxUploadSize = 1024 * 1024 * 100
)