Feat: user storage pack

This commit is contained in:
HFO4
2020-01-14 10:32:54 +08:00
parent f3e78ec4ff
commit 5be7ec98c1
11 changed files with 148 additions and 7 deletions

View File

@@ -99,7 +99,7 @@ func (fs *FileSystem) Preview(ctx context.Context, path string, isText bool) (*r
// 如果是文本文件预览,需要检查大小限制
sizeLimit := model.GetIntSetting("maxEditSize", 2<<20)
if fs.FileTarget[0].Size > uint64(sizeLimit) {
if isText && fs.FileTarget[0].Size > uint64(sizeLimit) {
return nil, ErrFileSizeTooBig
}

View File

@@ -28,6 +28,7 @@ func TestGenericBeforeUpload(t *testing.T) {
Size: 5,
Name: "1.txt",
}
cache.Set("pack_size_0", uint64(0), 0)
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, file)
fs := FileSystem{
User: &model.User{
@@ -266,6 +267,7 @@ func TestHookIsFileExist(t *testing.T) {
func TestHookValidateCapacity(t *testing.T) {
asserts := assert.New(t)
cache.Set("pack_size_1", uint64(0), 0)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{ID: 1},
Storage: 0,
@@ -313,6 +315,7 @@ func TestHookResetPolicy(t *testing.T) {
func TestHookChangeCapacity(t *testing.T) {
asserts := assert.New(t)
cache.Set("pack_size_1", uint64(0), 0)
// 容量增加 失败
{

View File

@@ -5,6 +5,7 @@ import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/jinzhu/gorm"
@@ -275,6 +276,7 @@ func TestFileSystem_ListDeleteDirs(t *testing.T) {
func TestFileSystem_Delete(t *testing.T) {
conf.DatabaseConfig.Type = "mysql"
asserts := assert.New(t)
cache.Set("pack_size_1", uint64(0), 0)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
@@ -381,6 +383,7 @@ func TestFileSystem_Delete(t *testing.T) {
func TestFileSystem_Copy(t *testing.T) {
asserts := assert.New(t)
cache.Set("pack_size_1", uint64(0), 0)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
@@ -431,6 +434,7 @@ func TestFileSystem_Copy(t *testing.T) {
func TestFileSystem_Move(t *testing.T) {
asserts := assert.New(t)
cache.Set("pack_size_1", uint64(0), 0)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,

View File

@@ -5,6 +5,7 @@ import (
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
@@ -42,6 +43,7 @@ func TestFileSystem_ValidateLegalName(t *testing.T) {
func TestFileSystem_ValidateCapacity(t *testing.T) {
asserts := assert.New(t)
ctx := context.Background()
cache.Set("pack_size_0", uint64(0), 0)
fs := FileSystem{
User: &model.User{
Storage: 10,
@@ -57,6 +59,11 @@ func TestFileSystem_ValidateCapacity(t *testing.T) {
fs.User.Storage = 5
asserts.False(fs.ValidateCapacity(ctx, 10))
asserts.Equal(uint64(5), fs.User.Storage)
fs.User.Storage = 5
cache.Set("pack_size_0", uint64(15), 0)
asserts.True(fs.ValidateCapacity(ctx, 10))
asserts.Equal(uint64(15), fs.User.Storage)
}
func TestFileSystem_ValidateFileSize(t *testing.T) {

View File

@@ -87,13 +87,14 @@ func BuildUserResponse(user model.User) Response {
// BuildUserStorageResponse 序列化用户存储概况响应
func BuildUserStorageResponse(user model.User) Response {
total := user.Group.MaxStorage + user.GetAvailablePackSize()
storageResp := storage{
Used: user.Storage,
Free: user.Group.MaxStorage - user.Storage,
Total: user.Group.MaxStorage,
Free: total - user.Storage,
Total: total,
}
if user.Group.MaxStorage < user.Storage {
if total < user.Storage {
storageResp.Free = 0
}

View File

@@ -2,6 +2,7 @@ package serializer
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -27,6 +28,7 @@ func TestBuildUserResponse(t *testing.T) {
func TestBuildUserStorageResponse(t *testing.T) {
asserts := assert.New(t)
cache.Set("pack_size_0", uint64(0), 0)
{
user := model.User{
@@ -58,4 +60,15 @@ func TestBuildUserStorageResponse(t *testing.T) {
asserts.Equal(uint64(10), res.Data.(storage).Total)
asserts.Equal(uint64(0), res.Data.(storage).Free)
}
{
cache.Set("pack_size_0", uint64(1), 0)
user := model.User{
Storage: 6,
Group: model.Group{MaxStorage: 10},
}
res := BuildUserStorageResponse(user)
asserts.Equal(uint64(6), res.Data.(storage).Used)
asserts.Equal(uint64(11), res.Data.(storage).Total)
asserts.Equal(uint64(5), res.Data.(storage).Free)
}
}