diff --git a/dao/file.go b/dao/file.go index e25766c..a1458c7 100644 --- a/dao/file.go +++ b/dao/file.go @@ -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 +} diff --git a/handler/file.go b/handler/file.go index 7cdba2e..bd232d5 100644 --- a/handler/file.go +++ b/handler/file.go @@ -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 } diff --git a/proto/status.go b/proto/status.go index afeab80..6910185 100644 --- a/proto/status.go +++ b/proto/status.go @@ -145,5 +145,5 @@ const ( UserFileTypeAvatar = "avatar" // 用户头像 UserFileTypeFile = "file" // 通用文件 UserFileTypeConfig = "config" // 配置文件 - + UserMaxUploadSize = 1024 * 1024 * 100 )