Test: filesystem/Preview
This commit is contained in:
parent
8a262a15d5
commit
35c2a5c977
4 changed files with 113 additions and 6 deletions
|
@ -68,7 +68,6 @@ func GetPolicyByID(ID interface{}) (Policy, error) {
|
|||
}
|
||||
|
||||
// IsDirectlyPreview 返回此策略下文件是否可以直接预览(不需要重定向)
|
||||
// TODO 测试
|
||||
func (policy *Policy) IsDirectlyPreview() bool {
|
||||
return policy.Type == "local"
|
||||
}
|
||||
|
|
|
@ -133,3 +133,11 @@ func TestPolicy_GenerateFileName(t *testing.T) {
|
|||
testPolicy.FileNameRule = "{uid}123{originname}"
|
||||
asserts.Equal("1123{filename}{.suffix}", testPolicy.GenerateFileName(1, ""))
|
||||
}
|
||||
|
||||
func TestPolicy_IsDirectlyPreview(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
policy := Policy{Type: "local"}
|
||||
asserts.True(policy.IsDirectlyPreview())
|
||||
policy.Type = "remote"
|
||||
asserts.False(policy.IsDirectlyPreview())
|
||||
}
|
||||
|
|
|
@ -88,7 +88,6 @@ func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (
|
|||
}
|
||||
|
||||
// Preview 预览文件
|
||||
// TODO 测试
|
||||
func (fs *FileSystem) Preview(ctx context.Context, path string) (*response.ContentResponse, error) {
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
|
@ -106,6 +105,7 @@ func (fs *FileSystem) Preview(ctx context.Context, path string) (*response.Conte
|
|||
Content: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 否则重定向到签名的预览URL
|
||||
ttl, err := strconv.ParseInt(model.GetSettingByName("preview_timeout"), 10, 64)
|
||||
if err != nil {
|
||||
|
@ -116,12 +116,10 @@ func (fs *FileSystem) Preview(ctx context.Context, path string) (*response.Conte
|
|||
err,
|
||||
)
|
||||
}
|
||||
|
||||
previewURL, err := fs.signURL(ctx, &fs.FileTarget[0], ttl, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response.ContentResponse{
|
||||
Redirect: true,
|
||||
URL: previewURL,
|
||||
|
|
|
@ -403,14 +403,14 @@ func TestFileSystem_GetDownloadURL(t *testing.T) {
|
|||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "1.txt", 1))
|
||||
// 相关设置
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs("download_timeout").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "20"))
|
||||
// 查找上传策略
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}).
|
||||
AddRow(35, "local", true),
|
||||
)
|
||||
// 相关设置
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs("download_timeout").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "20"))
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://cloudreve.org"))
|
||||
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt", "download_timeout")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
@ -490,3 +490,105 @@ func TestFileSystem_GetPhysicalFileContent(t *testing.T) {
|
|||
asserts.NotNil(rs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSystem_Preview(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 文件不存在
|
||||
{
|
||||
fs := FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.Nil(resp)
|
||||
}
|
||||
|
||||
// 直接返回文件内容,找不到文件
|
||||
{
|
||||
fs := FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
fs.FileTarget = []model.File{
|
||||
{
|
||||
PolicyID: 1,
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Type: "local",
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(resp)
|
||||
}
|
||||
|
||||
// 直接返回文件内容
|
||||
{
|
||||
fs := FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
fs.FileTarget = []model.File{
|
||||
{
|
||||
SourceName: "tests/file1.txt",
|
||||
PolicyID: 1,
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Type: "local",
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(resp)
|
||||
asserts.False(resp.Redirect)
|
||||
asserts.NoError(resp.Content.Close())
|
||||
}
|
||||
|
||||
// 需要重定向,无法解析有效期设置
|
||||
{
|
||||
fs := FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
fs.FileTarget = []model.File{
|
||||
{
|
||||
SourceName: "tests/file1.txt",
|
||||
PolicyID: 1,
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Type: "remote",
|
||||
},
|
||||
},
|
||||
}
|
||||
asserts.NoError(cache.Set("setting_preview_timeout", "test", 0))
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(resp)
|
||||
asserts.Equal(serializer.CodeInternalSetting, err.(serializer.AppError).Code)
|
||||
}
|
||||
|
||||
// 需要重定向,成功
|
||||
{
|
||||
fs := FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
fs.FileTarget = []model.File{
|
||||
{
|
||||
SourceName: "tests/file1.txt",
|
||||
PolicyID: 1,
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Type: "remote",
|
||||
},
|
||||
},
|
||||
}
|
||||
asserts.NoError(cache.Set("setting_preview_timeout", "233", 0))
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(resp)
|
||||
asserts.True(resp.Redirect)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue