mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Remove: mutipart file upload
This commit is contained in:
@@ -3,6 +3,7 @@ package filesystem
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@ package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -82,13 +85,65 @@ func TestGenericAfterUploadCanceled(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
//func TestGenericAfterUpload(t *testing.T) {
|
||||
// asserts := assert.New(t)
|
||||
// testObj := FileSystem{}
|
||||
// ctx := context.WithValue(context.Background(),FileHeaderCtx,local.FileStream{
|
||||
// VirtualPath: "/我的文件",
|
||||
// Name: "test.txt",
|
||||
// })
|
||||
//
|
||||
//
|
||||
//}
|
||||
func TestGenericAfterUpload(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.WithValue(context.Background(), FileHeaderCtx, local.FileStream{
|
||||
VirtualPath: "/我的文件",
|
||||
Name: "test.txt",
|
||||
})
|
||||
ctx = context.WithValue(ctx, SavePathCtx, "")
|
||||
|
||||
// 正常
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
err := GenericAfterUpload(ctx, &fs)
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 路径不存在
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}),
|
||||
)
|
||||
err = GenericAfterUpload(ctx, &fs)
|
||||
asserts.Equal(ErrPathNotExist, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 文件已存在
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("test.txt"),
|
||||
)
|
||||
err = GenericAfterUpload(ctx, &fs)
|
||||
asserts.Equal(ErrFileExisted, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 插入失败
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
err = GenericAfterUpload(ctx, &fs)
|
||||
asserts.Equal(ErrInsertFileRecord, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
}
|
||||
|
||||
60
pkg/filesystem/path_test.go
Normal file
60
pkg/filesystem/path_test.go
Normal file
@@ -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())
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"path/filepath"
|
||||
@@ -79,8 +80,10 @@ func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileHe
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// 客户端正常关闭,不执行操作
|
||||
fmt.Println("正常")
|
||||
case <-ginCtx.Request.Context().Done():
|
||||
// 客户端取消了上传
|
||||
fmt.Println("取消")
|
||||
if fs.AfterUploadCanceled == nil {
|
||||
return
|
||||
}
|
||||
|
||||
80
pkg/filesystem/upload_test.go
Normal file
80
pkg/filesystem/upload_test.go
Normal file
@@ -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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user