mirror of https://github.com/cloudreve/Cloudreve
Fix: test failed due to missing context
parent
20ea86eaf6
commit
f9b37a3359
|
@ -7,6 +7,6 @@ const (
|
||||||
GinCtx key = iota
|
GinCtx key = iota
|
||||||
// SavePathCtx 文件物理路径
|
// SavePathCtx 文件物理路径
|
||||||
SavePathCtx
|
SavePathCtx
|
||||||
// FileCtx 上传的文件
|
// FileHeaderCtx 上传的文件
|
||||||
FileCtx
|
FileHeaderCtx
|
||||||
)
|
)
|
||||||
|
|
|
@ -73,6 +73,16 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ============
|
||||||
|
文件相关
|
||||||
|
============
|
||||||
|
*/
|
||||||
|
|
||||||
|
// AddFile 新增文件记录
|
||||||
|
func (fs *FileSystem) AddFile(parent *model.Folder) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
/* ================
|
/* ================
|
||||||
上传处理相关
|
上传处理相关
|
||||||
================
|
================
|
||||||
|
@ -80,7 +90,7 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||||
|
|
||||||
// Upload 上传文件
|
// Upload 上传文件
|
||||||
func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
|
func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
|
||||||
ctx = context.WithValue(ctx, FileCtx, file)
|
ctx = context.WithValue(ctx, FileHeaderCtx, file)
|
||||||
|
|
||||||
// 上传前的钩子
|
// 上传前的钩子
|
||||||
if fs.BeforeUpload != nil {
|
if fs.BeforeUpload != nil {
|
||||||
|
@ -162,9 +172,10 @@ func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileHe
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// IsPathExist 返回给定目录是否存在
|
// IsPathExist 返回给定目录是否存在
|
||||||
func (fs *FileSystem) IsPathExist(path string) bool {
|
// 如果存在就返回目录
|
||||||
_, err := model.GetFolderByPath(path, fs.User.ID)
|
func (fs *FileSystem) IsPathExist(path string) (bool, model.Folder) {
|
||||||
return err == nil
|
folder, err := model.GetFolderByPath(path, fs.User.ID)
|
||||||
|
return err == nil, folder
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFileExist 返回给定路径的文件是否存在
|
// IsFileExist 返回给定路径的文件是否存在
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
||||||
func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
|
func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
file := ctx.Value(FileCtx).(FileHeader)
|
file := ctx.Value(FileHeaderCtx).(FileHeader)
|
||||||
|
|
||||||
// 验证单文件尺寸
|
// 验证单文件尺寸
|
||||||
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
if !fs.ValidateFileSize(ctx, file.GetSize()) {
|
||||||
|
@ -35,7 +35,7 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
|
|
||||||
// GenericAfterUploadCanceled 通用上传取消处理钩子,包含数据库操作
|
// GenericAfterUploadCanceled 通用上传取消处理钩子,包含数据库操作
|
||||||
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error {
|
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error {
|
||||||
file := ctx.Value(FileCtx).(FileHeader)
|
file := ctx.Value(FileHeaderCtx).(FileHeader)
|
||||||
|
|
||||||
filePath := ctx.Value(SavePathCtx).(string)
|
filePath := ctx.Value(SavePathCtx).(string)
|
||||||
// 删除临时文件
|
// 删除临时文件
|
||||||
|
@ -56,20 +56,27 @@ func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error {
|
||||||
// GenericAfterUpload 文件上传完成后,包含数据库操作
|
// GenericAfterUpload 文件上传完成后,包含数据库操作
|
||||||
func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
// 文件存放的虚拟路径
|
// 文件存放的虚拟路径
|
||||||
virtualPath := ctx.Value(FileCtx).(FileHeader).GetVirtualPath()
|
virtualPath := ctx.Value(FileHeaderCtx).(FileHeader).GetVirtualPath()
|
||||||
|
|
||||||
// 检查路径是否存在
|
// 检查路径是否存在
|
||||||
if !fs.IsPathExist(virtualPath) {
|
isExist, folder := fs.IsPathExist(virtualPath)
|
||||||
|
if !isExist {
|
||||||
return errors.New("路径\"" + virtualPath + "\"不存在")
|
return errors.New("路径\"" + virtualPath + "\"不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
if fs.IsFileExist(path.Join(
|
if fs.IsFileExist(path.Join(
|
||||||
virtualPath,
|
virtualPath,
|
||||||
ctx.Value(FileCtx).(FileHeader).GetFileName(),
|
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
|
||||||
)) {
|
)) {
|
||||||
return errors.New("同名文件已存在")
|
return errors.New("同名文件已存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 向数据库中插入记录
|
||||||
|
err := fs.AddFile(&folder)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("无法插入文件记录")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,11 @@ import (
|
||||||
|
|
||||||
func TestGenericBeforeUpload(t *testing.T) {
|
func TestGenericBeforeUpload(t *testing.T) {
|
||||||
asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
ctx := context.Background()
|
|
||||||
file := local.FileData{
|
file := local.FileData{
|
||||||
Size: 5,
|
Size: 5,
|
||||||
Name: "1.txt",
|
Name: "1.txt",
|
||||||
}
|
}
|
||||||
|
ctx := context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
fs := FileSystem{
|
fs := FileSystem{
|
||||||
User: &model.User{
|
User: &model.User{
|
||||||
Storage: 0,
|
Storage: 0,
|
||||||
|
@ -30,12 +30,18 @@ func TestGenericBeforeUpload(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
asserts.Error(GenericBeforeUpload(ctx, &fs, file))
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
||||||
|
|
||||||
file.Size = 1
|
file.Size = 1
|
||||||
file.Name = "1"
|
file.Name = "1"
|
||||||
asserts.Error(GenericBeforeUpload(ctx, &fs, file))
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
||||||
|
|
||||||
file.Name = "1.txt"
|
file.Name = "1.txt"
|
||||||
asserts.NoError(GenericBeforeUpload(ctx, &fs, file))
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
|
asserts.NoError(GenericBeforeUpload(ctx, &fs))
|
||||||
|
|
||||||
file.Name = "1.t/xt"
|
file.Name = "1.t/xt"
|
||||||
asserts.Error(GenericBeforeUpload(ctx, &fs, file))
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
||||||
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue