Fix: test failed due to Policy.AutoName
This commit is contained in:
parent
88a543ef74
commit
20ea86eaf6
5 changed files with 49 additions and 1 deletions
|
@ -15,3 +15,10 @@ type File struct {
|
||||||
PolicyID uint
|
PolicyID uint
|
||||||
Dir string `gorm:"size:65536"`
|
Dir string `gorm:"size:65536"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFileByPathAndName 给定路径、文件名、用户ID,查找文件
|
||||||
|
func GetFileByPathAndName(path string, name string, uid uint) (File, error) {
|
||||||
|
var file File
|
||||||
|
result := DB.Where("user_id = ? AND dir = ? AND name=?", uid, path, name).Find(&file)
|
||||||
|
return file, result.Error
|
||||||
|
}
|
||||||
|
|
18
models/file_test.go
Normal file
18
models/file_test.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetFileByPathAndName(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
fileRows := sqlmock.NewRows([]string{"id", "name"}).
|
||||||
|
AddRow(1, "1.cia")
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(fileRows)
|
||||||
|
file, _ := GetFileByPathAndName("/", "1.cia", 1)
|
||||||
|
asserts.Equal("1.cia", file.Name)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
|
@ -76,7 +76,9 @@ func TestPolicy_GeneratePath(t *testing.T) {
|
||||||
|
|
||||||
func TestPolicy_GenerateFileName(t *testing.T) {
|
func TestPolicy_GenerateFileName(t *testing.T) {
|
||||||
asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
testPolicy := Policy{}
|
testPolicy := Policy{
|
||||||
|
AutoRename: true,
|
||||||
|
}
|
||||||
|
|
||||||
testPolicy.FileNameRule = "{randomkey16}"
|
testPolicy.FileNameRule = "{randomkey16}"
|
||||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
|
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -165,3 +166,13 @@ func (fs *FileSystem) IsPathExist(path string) bool {
|
||||||
_, err := model.GetFolderByPath(path, fs.User.ID)
|
_, err := model.GetFolderByPath(path, fs.User.ID)
|
||||||
return err == nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
|
||||||
|
@ -61,5 +62,14 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||||
if !fs.IsPathExist(virtualPath) {
|
if !fs.IsPathExist(virtualPath) {
|
||||||
return errors.New("路径\"" + virtualPath + "\"不存在")
|
return errors.New("路径\"" + virtualPath + "\"不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if fs.IsFileExist(path.Join(
|
||||||
|
virtualPath,
|
||||||
|
ctx.Value(FileCtx).(FileHeader).GetFileName(),
|
||||||
|
)) {
|
||||||
|
return errors.New("同名文件已存在")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue