Fix: test failed due to Policy.AutoName

This commit is contained in:
HFO4
2019-11-19 17:20:59 +08:00
parent 88a543ef74
commit 20ea86eaf6
5 changed files with 49 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"io"
"path"
"path/filepath"
)
@@ -165,3 +166,13 @@ func (fs *FileSystem) IsPathExist(path string) bool {
_, err := model.GetFolderByPath(path, fs.User.ID)
return err == nil
}
// IsFileExist 返回给定路径的文件是否存在
func (fs *FileSystem) IsFileExist(fullPath string) bool {
basePath := path.Dir(fullPath)
fileName := path.Base(fullPath)
_, err := model.GetFileByPathAndName(basePath, fileName, fs.User.ID)
return err == nil
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"github.com/HFO4/cloudreve/pkg/util"
"path"
)
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
@@ -61,5 +62,14 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
if !fs.IsPathExist(virtualPath) {
return errors.New("路径\"" + virtualPath + "\"不存在")
}
// 检查文件是否存在
if fs.IsFileExist(path.Join(
virtualPath,
ctx.Value(FileCtx).(FileHeader).GetFileName(),
)) {
return errors.New("同名文件已存在")
}
return nil
}