mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Fix: cannot edit file in remote server / Modify: separate preview and text-file content controller
This commit is contained in:
@@ -93,7 +93,6 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint) (
|
||||
}
|
||||
|
||||
// cancelCompress 取消压缩进程
|
||||
// TODO 测试
|
||||
func (fs *FileSystem) cancelCompress(ctx context.Context, zipWriter *zip.Writer, file *os.File, path string) {
|
||||
util.Log().Debug("客户端取消压缩请求")
|
||||
zipWriter.Close()
|
||||
|
||||
@@ -88,14 +88,23 @@ func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (
|
||||
}
|
||||
|
||||
// Preview 预览文件
|
||||
func (fs *FileSystem) Preview(ctx context.Context, path string) (*response.ContentResponse, error) {
|
||||
// path - 文件虚拟路径
|
||||
// isText - 是否为文本文件,文本文件会忽略重定向,直接由
|
||||
// 服务端拉取中转给用户,故会对文件大小进行限制
|
||||
func (fs *FileSystem) Preview(ctx context.Context, path string, isText bool) (*response.ContentResponse, error) {
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 如果是文本文件预览,需要检查大小限制
|
||||
sizeLimit := model.GetIntSetting("maxEditSize", 2<<20)
|
||||
if fs.FileTarget[0].Size > uint64(sizeLimit) {
|
||||
return nil, ErrFileSizeTooBig
|
||||
}
|
||||
|
||||
// 是否直接返回文件内容
|
||||
if fs.Policy.IsDirectlyPreview() {
|
||||
if isText || fs.Policy.IsDirectlyPreview() {
|
||||
resp, err := fs.GetDownloadContent(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -510,7 +510,7 @@ func TestFileSystem_Preview(t *testing.T) {
|
||||
User: &model.User{},
|
||||
}
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
resp, err := fs.Preview(ctx, "/1.txt", false)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.Nil(resp)
|
||||
@@ -530,7 +530,7 @@ func TestFileSystem_Preview(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
resp, err := fs.Preview(ctx, "/1.txt", false)
|
||||
asserts.Error(err)
|
||||
asserts.Nil(resp)
|
||||
}
|
||||
@@ -550,7 +550,7 @@ func TestFileSystem_Preview(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
resp, err := fs.Preview(ctx, "/1.txt", false)
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(resp)
|
||||
asserts.False(resp.Redirect)
|
||||
@@ -573,9 +573,31 @@ func TestFileSystem_Preview(t *testing.T) {
|
||||
},
|
||||
}
|
||||
asserts.NoError(cache.Set("setting_preview_timeout", "233", 0))
|
||||
resp, err := fs.Preview(ctx, "/1.txt")
|
||||
resp, err := fs.Preview(ctx, "/1.txt", false)
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(resp)
|
||||
asserts.True(resp.Redirect)
|
||||
}
|
||||
|
||||
// 文本文件,大小超出限制
|
||||
{
|
||||
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",
|
||||
},
|
||||
Size: 11,
|
||||
},
|
||||
}
|
||||
asserts.NoError(cache.Set("setting_maxEditSize", "10", 0))
|
||||
resp, err := fs.Preview(ctx, "/1.txt", true)
|
||||
asserts.Equal(ErrFileSizeTooBig, err)
|
||||
asserts.Nil(resp)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user