mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Fix: uint may overflow / Test: get user storage
This commit is contained in:
@@ -22,11 +22,11 @@ type User struct {
|
||||
Avatar string `json:"avatar"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
PreferredTheme string `json:"preferred_theme"`
|
||||
Policy Policy `json:"policy"`
|
||||
Group Group `json:"group"`
|
||||
Policy policy `json:"policy"`
|
||||
Group group `json:"group"`
|
||||
}
|
||||
|
||||
type Policy struct {
|
||||
type policy struct {
|
||||
SaveType string `json:"saveType"`
|
||||
MaxSize string `json:"maxSize"`
|
||||
AllowedType []string `json:"allowedType"`
|
||||
@@ -34,12 +34,18 @@ type Policy struct {
|
||||
AllowGetSource bool `json:"allowSource"`
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
type group struct {
|
||||
AllowShare bool `json:"allowShare"`
|
||||
AllowRemoteDownload bool `json:"allowRemoteDownload"`
|
||||
AllowTorrentDownload bool `json:"allowTorrentDownload"`
|
||||
}
|
||||
|
||||
type storage struct {
|
||||
Used uint64 `json:"used"`
|
||||
Free uint64 `json:"free"`
|
||||
Total uint64 `json:"total"`
|
||||
}
|
||||
|
||||
// BuildUser 序列化用户
|
||||
func BuildUser(user model.User) User {
|
||||
aria2Option := user.Group.GetAria2Option()
|
||||
@@ -51,14 +57,14 @@ func BuildUser(user model.User) User {
|
||||
Avatar: user.Avatar,
|
||||
CreatedAt: user.CreatedAt.Unix(),
|
||||
PreferredTheme: user.OptionsSerialized.PreferredTheme,
|
||||
Policy: Policy{
|
||||
Policy: policy{
|
||||
SaveType: user.Policy.Type,
|
||||
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/1024*1024),
|
||||
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/(1024*1024)),
|
||||
AllowedType: user.Policy.OptionsSerialized.FileType,
|
||||
UploadURL: user.Policy.Server,
|
||||
AllowGetSource: user.Policy.IsOriginLinkEnable,
|
||||
},
|
||||
Group: Group{
|
||||
Group: group{
|
||||
AllowShare: user.Group.ShareEnabled,
|
||||
AllowRemoteDownload: aria2Option[0],
|
||||
AllowTorrentDownload: aria2Option[2],
|
||||
@@ -72,3 +78,20 @@ func BuildUserResponse(user model.User) Response {
|
||||
Data: BuildUser(user),
|
||||
}
|
||||
}
|
||||
|
||||
// BuildUserStorageResponse 序列化用户存储概况响应
|
||||
func BuildUserStorageResponse(user model.User) Response {
|
||||
storageResp := storage{
|
||||
Used: user.Storage,
|
||||
Free: user.Group.MaxStorage - user.Storage,
|
||||
Total: user.Group.MaxStorage,
|
||||
}
|
||||
|
||||
if user.Group.MaxStorage < user.Storage {
|
||||
storageResp.Free = 0
|
||||
}
|
||||
|
||||
return Response{
|
||||
Data: storageResp,
|
||||
}
|
||||
}
|
||||
|
||||
61
pkg/serializer/user_test.go
Normal file
61
pkg/serializer/user_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildUser(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := model.User{
|
||||
Policy: model.Policy{MaxSize: 1024 * 1024},
|
||||
}
|
||||
res := BuildUser(user)
|
||||
asserts.Equal("1.00mb", res.Policy.MaxSize)
|
||||
|
||||
}
|
||||
|
||||
func TestBuildUserResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
user := model.User{
|
||||
Policy: model.Policy{MaxSize: 1024 * 1024},
|
||||
}
|
||||
res := BuildUserResponse(user)
|
||||
asserts.Equal("1.00mb", res.Data.(User).Policy.MaxSize)
|
||||
}
|
||||
|
||||
func TestBuildUserStorageResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
{
|
||||
user := model.User{
|
||||
Storage: 0,
|
||||
Group: model.Group{MaxStorage: 10},
|
||||
}
|
||||
res := BuildUserStorageResponse(user)
|
||||
asserts.Equal(uint64(0), res.Data.(storage).Used)
|
||||
asserts.Equal(uint64(10), res.Data.(storage).Total)
|
||||
asserts.Equal(uint64(10), res.Data.(storage).Free)
|
||||
}
|
||||
{
|
||||
user := model.User{
|
||||
Storage: 6,
|
||||
Group: model.Group{MaxStorage: 10},
|
||||
}
|
||||
res := BuildUserStorageResponse(user)
|
||||
asserts.Equal(uint64(6), res.Data.(storage).Used)
|
||||
asserts.Equal(uint64(10), res.Data.(storage).Total)
|
||||
asserts.Equal(uint64(4), res.Data.(storage).Free)
|
||||
}
|
||||
{
|
||||
user := model.User{
|
||||
Storage: 20,
|
||||
Group: model.Group{MaxStorage: 10},
|
||||
}
|
||||
res := BuildUserStorageResponse(user)
|
||||
asserts.Equal(uint64(20), res.Data.(storage).Used)
|
||||
asserts.Equal(uint64(10), res.Data.(storage).Total)
|
||||
asserts.Equal(uint64(0), res.Data.(storage).Free)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user