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