parent
7cb27b2102
commit
9d8a462dc4
@ -0,0 +1,60 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileSystem_IsFileExist(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := &FileSystem{User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}}
|
||||
|
||||
// 存在
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/s", "1.txt").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"Name"}).AddRow("s"),
|
||||
)
|
||||
testResult := fs.IsFileExist("/s/1.txt")
|
||||
asserts.True(testResult)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 不存在
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/ss/dfsd", "1.txt").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"Name"}),
|
||||
)
|
||||
testResult = fs.IsFileExist("/ss/dfsd/1.txt")
|
||||
asserts.False(testResult)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFileSystem_IsPathExist(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := &FileSystem{User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}}
|
||||
|
||||
// 存在
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/s/sda").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}).AddRow("sda"),
|
||||
)
|
||||
testResult, folder := fs.IsPathExist("/s/sda")
|
||||
asserts.True(testResult)
|
||||
asserts.Equal("sda", folder.Name)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 不存在
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/s/sda").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}),
|
||||
)
|
||||
testResult, _ = fs.IsPathExist("/s/sda")
|
||||
asserts.False(testResult)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type FileHeaderMock struct {
|
||||
testMock.Mock
|
||||
}
|
||||
|
||||
func (m FileHeaderMock) Put(ctx context.Context, file io.ReadCloser, dst string) error {
|
||||
args := m.Called(ctx, file, dst)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m FileHeaderMock) Delete(ctx context.Context, files []string) ([]string, error) {
|
||||
args := m.Called(ctx, files)
|
||||
return args.Get(0).([]string), args.Error(1)
|
||||
}
|
||||
|
||||
func TestFileSystem_Upload(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 正常
|
||||
testHandller := new(FileHeaderMock)
|
||||
testHandller.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
|
||||
fs := &FileSystem{
|
||||
Handler: testHandller,
|
||||
User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
Policy: model.Policy{
|
||||
AutoRename: false,
|
||||
DirNameRule: "{path}",
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
ctx = context.WithValue(ctx, GinCtx, c)
|
||||
cancel()
|
||||
file := local.FileStream{
|
||||
Size: 5,
|
||||
VirtualPath: "/",
|
||||
Name: "1.txt",
|
||||
}
|
||||
err := fs.Upload(ctx, file)
|
||||
asserts.NoError(err)
|
||||
|
||||
// BeforeUpload 返回错误
|
||||
fs.BeforeUpload = func(ctx context.Context, fs *FileSystem) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
err = fs.Upload(ctx, file)
|
||||
asserts.Error(err)
|
||||
fs.BeforeUpload = nil
|
||||
testHandller.AssertExpectations(t)
|
||||
|
||||
// 上传文件失败
|
||||
testHandller2 := new(FileHeaderMock)
|
||||
testHandller2.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(errors.New("error"))
|
||||
fs.Handler = testHandller2
|
||||
err = fs.Upload(ctx, file)
|
||||
asserts.Error(err)
|
||||
testHandller2.AssertExpectations(t)
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
// UploadService 本地策略文件上传服务
|
||||
type UploadService struct {
|
||||
Name string `form:"name" binding:"required,lte=255"`
|
||||
Path string `form:"path" binding:"lte=65536"`
|
||||
File *multipart.FileHeader `form:"file" binding:"required"`
|
||||
}
|
||||
|
||||
// Upload 处理本地策略小文件上传
|
||||
func (service *UploadService) Upload(ctx context.Context, c *gin.Context) serializer.Response {
|
||||
// TODO 检查文件大小是否小于分片最大大小
|
||||
file, err := service.File.Open()
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeIOFailed, "无法打开上传数据", err)
|
||||
}
|
||||
|
||||
fileData := local.FileData{
|
||||
MIMEType: service.File.Header.Get("Content-Type"),
|
||||
File: file,
|
||||
Size: uint64(service.File.Size),
|
||||
Name: service.Name,
|
||||
}
|
||||
user, _ := c.Get("user")
|
||||
|
||||
// 创建文件系统
|
||||
fs, err := filesystem.NewFileSystem(user.(*model.User))
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
||||
}
|
||||
|
||||
// 给文件系统分配钩子
|
||||
fs.BeforeUpload = filesystem.GenericBeforeUpload
|
||||
|
||||
// 执行上传
|
||||
err = fs.Upload(ctx, fileData)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeUploadFailed, err.Error(), err)
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Msg: "Pong",
|
||||
}
|
||||
}
|
Loading…
Reference in new issue