mirror of https://github.com/cloudreve/Cloudreve
Test: filesystem/file, filesystem, filesystem/hook, model/file/Create
parent
438ce02420
commit
7cb27b2102
|
@ -1,6 +1,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/DATA-DOG/go-sqlmock"
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -16,3 +17,27 @@ func TestGetFileByPathAndName(t *testing.T) {
|
||||||
asserts.Equal("1.cia", file.Name)
|
asserts.Equal("1.cia", file.Name)
|
||||||
asserts.NoError(mock.ExpectationsWereMet())
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFile_Create(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
file := File{
|
||||||
|
Name: "123",
|
||||||
|
}
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(5, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
fileID, err := file.Create()
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal(uint(5), fileID)
|
||||||
|
asserts.Equal(uint(5), file.ID)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||||
|
mock.ExpectRollback()
|
||||||
|
fileID, err = file.Create()
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ func Init() {
|
||||||
|
|
||||||
//db.SetLogger(util.Log())
|
//db.SetLogger(util.Log())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Panic("连接数据库不成功", err)
|
util.Log().Panic("连接数据库不成功, %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//设置连接池
|
//设置连接池
|
||||||
|
|
|
@ -45,7 +45,7 @@ func migration() {
|
||||||
// 迁移完毕后写入版本锁 version.lock
|
// 迁移完毕后写入版本锁 version.lock
|
||||||
err := conf.WriteVersionLock()
|
err := conf.WriteVersionLock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Warning("无法写入版本控制锁 version.lock, ", err)
|
util.Log().Warning("无法写入版本控制锁 version.lock, %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func addDefaultPolicy() {
|
||||||
IsOriginLinkEnable: false,
|
IsOriginLinkEnable: false,
|
||||||
}
|
}
|
||||||
if err := DB.Create(&defaultPolicy).Error; err != nil {
|
if err := DB.Create(&defaultPolicy).Error; err != nil {
|
||||||
util.Log().Panic("无法创建初始存储策略, ", err)
|
util.Log().Panic("无法创建初始存储策略, %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,12 @@ package filesystem
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
UnknownPolicyTypeError = errors.New("未知存储策略类型")
|
ErrUnknownPolicyType = errors.New("未知存储策略类型")
|
||||||
FileSizeTooBigError = errors.New("单个文件尺寸太大")
|
ErrFileSizeTooBig = errors.New("单个文件尺寸太大")
|
||||||
FileExtensionNotAllowedError = errors.New("不允许上传此类型的文件")
|
ErrFileExtensionNotAllowed = errors.New("不允许上传此类型的文件")
|
||||||
InsufficientCapacityError = errors.New("容量空间不足")
|
ErrInsufficientCapacity = errors.New("容量空间不足")
|
||||||
IlegalObjectNameError = errors.New("目标名称非法")
|
ErrIllegalObjectName = errors.New("目标名称非法")
|
||||||
|
ErrInsertFileRecord = errors.New("无法插入文件记录")
|
||||||
|
ErrFileExisted = errors.New("同名文件已存在")
|
||||||
|
ErrPathNotExist = errors.New("路径不存在")
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package filesystem
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileSystem_AddFile(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
file := local.FileData{
|
||||||
|
Size: 5,
|
||||||
|
Name: "1.txt",
|
||||||
|
}
|
||||||
|
folder := model.Folder{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
PositionAbsolute: "/我的文件",
|
||||||
|
}
|
||||||
|
fs := FileSystem{
|
||||||
|
User: &model.User{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
Policy: model.Policy{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
|
ctx = context.WithValue(ctx, SavePathCtx, "/Uploads/1_sad.txt")
|
||||||
|
|
||||||
|
_, err := fs.AddFile(ctx, &folder)
|
||||||
|
|
||||||
|
asserts.Error(err)
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
f, err := fs.AddFile(ctx, &folder)
|
||||||
|
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal("/Uploads/1_sad.txt", f.SourceName)
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/HFO4/cloudreve/models"
|
"github.com/HFO4/cloudreve/models"
|
||||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||||
|
testMock "github.com/stretchr/testify/mock"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,6 +28,10 @@ type Handler interface {
|
||||||
|
|
||||||
// FileSystem 管理文件的文件系统
|
// FileSystem 管理文件的文件系统
|
||||||
type FileSystem struct {
|
type FileSystem struct {
|
||||||
|
/*
|
||||||
|
测试用
|
||||||
|
*/
|
||||||
|
testMock.Mock
|
||||||
/*
|
/*
|
||||||
文件系统所有者
|
文件系统所有者
|
||||||
*/
|
*/
|
||||||
|
@ -59,7 +64,7 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||||
case "local":
|
case "local":
|
||||||
handler = local.Handler{}
|
handler = local.Handler{}
|
||||||
default:
|
default:
|
||||||
return nil, UnknownPolicyTypeError
|
return nil, ErrUnknownPolicyType
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 分配默认钩子
|
// TODO 分配默认钩子
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package filesystem
|
||||||
|
|
||||||
|
import (
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewFileSystem(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
user := model.User{
|
||||||
|
Policy: model.Policy{
|
||||||
|
Type: "local",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fs, err := NewFileSystem(&user)
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.NotNil(fs.Handler)
|
||||||
|
|
||||||
|
user.Policy.Type = "unknown"
|
||||||
|
fs, err = NewFileSystem(&user)
|
||||||
|
asserts.Error(err)
|
||||||
|
}
|
|
@ -13,22 +13,22 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
|
|
||||||
// 验证单文件尺寸
|
// 验证单文件尺寸
|
||||||
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
||||||
return FileSizeTooBigError
|
return ErrFileSizeTooBig
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证文件名
|
// 验证文件名
|
||||||
if !fs.ValidateLegalName(ctx, file.GetFileName()) {
|
if !fs.ValidateLegalName(ctx, file.GetFileName()) {
|
||||||
return IlegalObjectNameError
|
return ErrIllegalObjectName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证扩展名
|
// 验证扩展名
|
||||||
if !fs.ValidateExtension(ctx, file.GetFileName()) {
|
if !fs.ValidateExtension(ctx, file.GetFileName()) {
|
||||||
return FileExtensionNotAllowedError
|
return ErrFileExtensionNotAllowed
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证并扣除容量
|
// 验证并扣除容量
|
||||||
if !fs.ValidateCapacity(ctx, file.GetSize()) {
|
if !fs.ValidateCapacity(ctx, file.GetSize()) {
|
||||||
return InsufficientCapacityError
|
return ErrInsufficientCapacity
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
// 检查路径是否存在
|
// 检查路径是否存在
|
||||||
isExist, folder := fs.IsPathExist(virtualPath)
|
isExist, folder := fs.IsPathExist(virtualPath)
|
||||||
if !isExist {
|
if !isExist {
|
||||||
return errors.New("路径\"" + virtualPath + "\"不存在")
|
return ErrPathNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
|
@ -69,13 +69,13 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
virtualPath,
|
virtualPath,
|
||||||
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
|
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
|
||||||
)) {
|
)) {
|
||||||
return errors.New("同名文件已存在")
|
return ErrFileExisted
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向数据库中插入记录
|
// 向数据库中插入记录
|
||||||
file, err := fs.AddFile(ctx, &folder)
|
file, err := fs.AddFile(ctx, folder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("无法插入文件记录")
|
return ErrInsertFileRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 是否需要立即获取图像大小?
|
// TODO 是否需要立即获取图像大小?
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
model "github.com/HFO4/cloudreve/models"
|
model "github.com/HFO4/cloudreve/models"
|
||||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,3 +46,49 @@ func TestGenericBeforeUpload(t *testing.T) {
|
||||||
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenericAfterUploadCanceled(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
f, err := os.Create("TestGenericAfterUploadCanceled")
|
||||||
|
asserts.NoError(err)
|
||||||
|
f.Close()
|
||||||
|
file := local.FileStream{
|
||||||
|
Size: 5,
|
||||||
|
Name: "TestGenericAfterUploadCanceled",
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(context.Background(), SavePathCtx, "TestGenericAfterUploadCanceled")
|
||||||
|
ctx = context.WithValue(ctx, FileHeaderCtx, file)
|
||||||
|
fs := FileSystem{
|
||||||
|
User: &model.User{Storage: 5},
|
||||||
|
Handler: local.Handler{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal(uint64(0), fs.User.Storage)
|
||||||
|
|
||||||
|
f, err = os.Create("TestGenericAfterUploadCanceled")
|
||||||
|
asserts.NoError(err)
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
// 容量不能再降低
|
||||||
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
||||||
|
asserts.Error(err)
|
||||||
|
|
||||||
|
//文件不存在
|
||||||
|
fs.User.Storage = 5
|
||||||
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
||||||
|
asserts.NoError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//func TestGenericAfterUpload(t *testing.T) {
|
||||||
|
// asserts := assert.New(t)
|
||||||
|
// testObj := FileSystem{}
|
||||||
|
// ctx := context.WithValue(context.Background(),FileHeaderCtx,local.FileStream{
|
||||||
|
// VirtualPath: "/我的文件",
|
||||||
|
// Name: "test.txt",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -42,3 +43,27 @@ func TestHandler_Put(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler_Delete(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
handler := Handler{}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
file, err := os.Create("test.file")
|
||||||
|
asserts.NoError(err)
|
||||||
|
_ = file.Close()
|
||||||
|
list, err := handler.Delete(ctx, []string{"test.file"})
|
||||||
|
asserts.Equal([]string{"test.file"}, list)
|
||||||
|
asserts.NoError(err)
|
||||||
|
|
||||||
|
file, err = os.Create("test.file")
|
||||||
|
asserts.NoError(err)
|
||||||
|
_ = file.Close()
|
||||||
|
list, err = handler.Delete(ctx, []string{"test.file", "test.notexist"})
|
||||||
|
asserts.Equal([]string{"test.file"}, list)
|
||||||
|
asserts.Error(err)
|
||||||
|
|
||||||
|
list, err = handler.Delete(ctx, []string{"test.notexist"})
|
||||||
|
asserts.Equal([]string{}, list)
|
||||||
|
asserts.Error(err)
|
||||||
|
}
|
||||||
|
|
|
@ -12,9 +12,9 @@ import (
|
||||||
|
|
||||||
// IsPathExist 返回给定目录是否存在
|
// IsPathExist 返回给定目录是否存在
|
||||||
// 如果存在就返回目录
|
// 如果存在就返回目录
|
||||||
func (fs *FileSystem) IsPathExist(path string) (bool, model.Folder) {
|
func (fs *FileSystem) IsPathExist(path string) (bool, *model.Folder) {
|
||||||
folder, err := model.GetFolderByPath(path, fs.User.ID)
|
folder, err := model.GetFolderByPath(path, fs.User.ID)
|
||||||
return err == nil, folder
|
return err == nil, &folder
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFileExist 返回给定路径的文件是否存在
|
// IsFileExist 返回给定路径的文件是否存在
|
||||||
|
|
Loading…
Reference in New Issue