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:
@@ -107,6 +107,7 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (io.ReadSeeke
|
||||
// 返回每个分组失败的文件列表
|
||||
func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*model.File) map[uint][]string {
|
||||
// 失败的文件列表
|
||||
// TODO 并行删除
|
||||
failed := make(map[uint][]string, len(files))
|
||||
|
||||
for policyID, toBeDeletedFiles := range files {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
||||
/* =================
|
||||
@@ -209,12 +210,25 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
return []Object{}, nil
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var childFolders []model.Folder
|
||||
var childFiles []model.File
|
||||
wg.Add(2)
|
||||
|
||||
// 获取子目录
|
||||
childFolders, _ := folder.GetChildFolder()
|
||||
go func() {
|
||||
childFolders, _ = folder.GetChildFolder()
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
// 获取子文件
|
||||
childFiles, _ := folder.GetChildFile()
|
||||
go func() {
|
||||
childFiles, _ = folder.GetChildFile()
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
// 汇总处理结果
|
||||
wg.Wait()
|
||||
objects := make([]Object, 0, len(childFiles)+len(childFolders))
|
||||
// 所有对象的父目录
|
||||
var processedPath string
|
||||
|
||||
@@ -299,17 +299,17 @@ func TestFileSystem_Delete(t *testing.T) {
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
// 删除文件记录
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("DELETE(.+)").
|
||||
mock.ExpectExec("DELETE(.+)files").
|
||||
WillReturnResult(sqlmock.NewResult(0, 3))
|
||||
mock.ExpectCommit()
|
||||
// 归还容量
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").
|
||||
mock.ExpectExec("UPDATE(.+)users").
|
||||
WillReturnResult(sqlmock.NewResult(0, 3))
|
||||
mock.ExpectCommit()
|
||||
// 删除目录
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("DELETE(.+)").
|
||||
mock.ExpectExec("DELETE(.+)folders").
|
||||
WillReturnResult(sqlmock.NewResult(0, 3))
|
||||
mock.ExpectCommit()
|
||||
|
||||
|
||||
@@ -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