Feat: after uploading finished hook

pull/247/head
HFO4 5 years ago
parent aa17aa8e6a
commit 2e9f256462

@ -19,6 +19,6 @@ type Folder struct {
// GetFolderByPath 根据绝对路径和UID查找目录 // GetFolderByPath 根据绝对路径和UID查找目录
func GetFolderByPath(path string, uid uint) (Folder, error) { func GetFolderByPath(path string, uid uint) (Folder, error) {
var folder Folder var folder Folder
result := DB.Where("owner = ? AND position_absolute = ?", uid, path).Find(&folder) result := DB.Where("owner_id = ? AND position_absolute = ?", uid, path).Find(&folder)
return folder, result.Error return folder, result.Error
} }

@ -1,7 +1,7 @@
package model package model
import ( import (
"fmt" "github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
@ -9,12 +9,16 @@ import (
func TestGetFolderByPath(t *testing.T) { func TestGetFolderByPath(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
//policyRows := sqlmock.NewRows([]string{"id", "name"}). folderRows := sqlmock.NewRows([]string{"id", "name"}).
// AddRow(1, "默认上传策略") AddRow(1, "test")
//mock.ExpectQuery("^SELECT (.+)").WillReturnRows(policyRows) mock.ExpectQuery("^SELECT (.+)").WillReturnRows(folderRows)
folder, _ := GetFolderByPath("/测试/test", 1)
asserts.Equal("test", folder.Name)
asserts.NoError(mock.ExpectationsWereMet())
folder,_ := GetFolderByPath("/测试/test",1) folderRows = sqlmock.NewRows([]string{"id", "name"})
fmt.Println(folder) mock.ExpectQuery("^SELECT (.+)").WillReturnRows(folderRows)
folder, err := GetFolderByPath("/测试/test", 1)
asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(mock.)
} }

@ -104,7 +104,16 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
if fs.AfterUpload != nil { if fs.AfterUpload != nil {
ctx = context.WithValue(ctx, SavePathCtx, savePath) ctx = context.WithValue(ctx, SavePathCtx, savePath)
err = fs.AfterUpload(ctx, fs) err = fs.AfterUpload(ctx, fs)
if err != nil { if err != nil {
// 上传完成后续处理失败
if fs.AfterValidateFailed != nil {
followUpErr := fs.AfterValidateFailed(ctx, fs)
// 失败后再失败...
if followUpErr != nil {
util.Log().Warning("AfterValidateFailed 钩子执行失败,%s", followUpErr)
}
}
return err return err
} }
} }

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
) )
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作 // GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
@ -36,7 +37,7 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error { func GenericAfterUploadCanceled(ctx context.Context, fs *FileSystem) error {
file := ctx.Value(FileCtx).(FileData) file := ctx.Value(FileCtx).(FileData)
filePath := ctx.Value("path").(string) filePath := ctx.Value(SavePathCtx).(string)
// 删除临时文件 // 删除临时文件
if util.Exists(filePath) { if util.Exists(filePath) {
_, err := fs.Handler.Delete(ctx, []string{filePath}) _, err := fs.Handler.Delete(ctx, []string{filePath})
@ -55,13 +56,13 @@ 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 {
// 获取Gin的上下文 // 获取Gin的上下文
//ginCtx := ctx.Value(GinCtx).(*gin.Context) ginCtx := ctx.Value(GinCtx).(*gin.Context)
// 文件存放的虚拟路径 // 文件存放的虚拟路径
//virtualPath := ginCtx.GetHeader("X-Path") virtualPath := util.DotPathToStandardPath(ginCtx.GetHeader("X-Path"))
// 检查路径是否存在 // 检查路径是否存在
if !fs.IsPathExist("/") { if !fs.IsPathExist(virtualPath) {
return errors.New("sss") return errors.New("路径\"" + virtualPath + "\"不存在")
} }
return nil return nil
} }

@ -48,8 +48,8 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er
err := os.Remove(value) err := os.Remove(value)
if err == nil { if err == nil {
deleted = append(deleted, value) deleted = append(deleted, value)
util.Log().Warning("无法删除文件,%s", err)
} else { } else {
util.Log().Warning("无法删除文件,%s", err)
retErr = err retErr = err
} }
} }

@ -0,0 +1,8 @@
package util
import "strings"
// DotPathToStandardPath 将","分割的路径转换为标准路径
func DotPathToStandardPath(path string) string {
return "/" + strings.Replace(path, ",", "/", -1)
}

@ -71,6 +71,8 @@ func FileUploadStream(c *gin.Context) {
// 给文件系统分配钩子 // 给文件系统分配钩子
fs.BeforeUpload = filesystem.GenericBeforeUpload fs.BeforeUpload = filesystem.GenericBeforeUpload
fs.AfterUploadCanceled = filesystem.GenericAfterUploadCanceled fs.AfterUploadCanceled = filesystem.GenericAfterUploadCanceled
fs.AfterUpload = filesystem.GenericAfterUpload
fs.AfterValidateFailed = filesystem.GenericAfterUploadCanceled
// 执行上传 // 执行上传
uploadCtx := context.WithValue(ctx, filesystem.GinCtx, c) uploadCtx := context.WithValue(ctx, filesystem.GinCtx, c)

Loading…
Cancel
Save