修复文件上传权限问题
This commit is contained in:
parent
25cdf5416d
commit
ac2ddb1beb
15
dao/file.go
15
dao/file.go
|
|
@ -267,3 +267,18 @@ func FindFileAuthByID(id int) FileAuth {
|
||||||
}
|
}
|
||||||
return file
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -210,9 +210,11 @@ func UploadFileV2(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := dao.FindUserByUserID(id1)
|
//查看用户上传的所有文件大小是否超过限制
|
||||||
if user.Upload == false {
|
userFileSpace := dao.GetUserFileSpace(id1)
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
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
|
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"})
|
c.JSON(http.StatusOK, gin.H{"error": "upload file failed", "code": proto.UploadFileFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//计算文件md5值
|
//计算文件md5值
|
||||||
if md5_ == "" {
|
if md5_ == "" {
|
||||||
file_, _ := file.Open()
|
file_, _ := file.Open()
|
||||||
|
|
@ -234,7 +237,13 @@ func UploadFileV2(c *gin.Context) {
|
||||||
fileExist := dao.FindFileByMd5(md5_)
|
fileExist := dao.FindFileByMd5(md5_)
|
||||||
if fileExist.ID != 0 {
|
if fileExist.ID != 0 {
|
||||||
fileExist.FilePath = ""
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,5 +145,5 @@ const (
|
||||||
UserFileTypeAvatar = "avatar" // 用户头像
|
UserFileTypeAvatar = "avatar" // 用户头像
|
||||||
UserFileTypeFile = "file" // 通用文件
|
UserFileTypeFile = "file" // 通用文件
|
||||||
UserFileTypeConfig = "config" // 配置文件
|
UserFileTypeConfig = "config" // 配置文件
|
||||||
|
UserMaxUploadSize = 1024 * 1024 * 100
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue