From f9b37a3359b4acf5da568c176a21eaa9215296c8 Mon Sep 17 00:00:00 2001
From: HFO4 <912394456@qq.com>
Date: Tue, 19 Nov 2019 18:10:18 +0800
Subject: [PATCH] Fix: test failed due to missing context

---
 pkg/filesystem/context.go    |  4 ++--
 pkg/filesystem/filesystem.go | 19 +++++++++++++++----
 pkg/filesystem/hooks.go      | 17 ++++++++++++-----
 pkg/filesystem/hooks_test.go | 16 +++++++++++-----
 4 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/pkg/filesystem/context.go b/pkg/filesystem/context.go
index 59ddc2b..3c7406f 100644
--- a/pkg/filesystem/context.go
+++ b/pkg/filesystem/context.go
@@ -7,6 +7,6 @@ const (
 	GinCtx key = iota
 	// SavePathCtx 文件物理路径
 	SavePathCtx
-	// FileCtx 上传的文件
-	FileCtx
+	// FileHeaderCtx 上传的文件
+	FileHeaderCtx
 )
diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go
index 89c014d..e3cb67f 100644
--- a/pkg/filesystem/filesystem.go
+++ b/pkg/filesystem/filesystem.go
@@ -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 返回给定路径的文件是否存在
diff --git a/pkg/filesystem/hooks.go b/pkg/filesystem/hooks.go
index f939b44..3d4d3a6 100644
--- a/pkg/filesystem/hooks.go
+++ b/pkg/filesystem/hooks.go
@@ -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
 }
diff --git a/pkg/filesystem/hooks_test.go b/pkg/filesystem/hooks_test.go
index 528820c..c6d808f 100644
--- a/pkg/filesystem/hooks_test.go
+++ b/pkg/filesystem/hooks_test.go
@@ -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))
 }