diff --git a/pkg/filesystem/archive.go b/pkg/filesystem/archive.go new file mode 100644 index 0000000..e4b5af9 --- /dev/null +++ b/pkg/filesystem/archive.go @@ -0,0 +1 @@ +package filesystem diff --git a/pkg/filesystem/file.go b/pkg/filesystem/file.go index 99fbe40..e12a441 100644 --- a/pkg/filesystem/file.go +++ b/pkg/filesystem/file.go @@ -93,7 +93,6 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (io.ReadSeeke // 将当前存储策略重设为文件使用的 fs.Policy = fs.FileTarget[0].GetPolicy() err = fs.dispatchHandler() - defer fs.CleanTargets() if err != nil { return nil, err } diff --git a/pkg/filesystem/file_test.go b/pkg/filesystem/file_test.go index 6d91abb..63e6698 100644 --- a/pkg/filesystem/file_test.go +++ b/pkg/filesystem/file_test.go @@ -75,6 +75,7 @@ func TestFileSystem_GetContent(t *testing.T) { rs, err := fs.GetContent(ctx, "not exist file") asserts.Equal(ErrObjectNotExist, err) asserts.Nil(rs) + fs.CleanTargets() // 未知存储策略 file, err := os.Create("TestFileSystem_GetContent.txt") @@ -90,6 +91,7 @@ func TestFileSystem_GetContent(t *testing.T) { rs, err = fs.GetContent(ctx, "/TestFileSystem_GetContent.txt") asserts.Error(err) asserts.NoError(mock.ExpectationsWereMet()) + fs.CleanTargets() // 打开文件失败 mock.ExpectQuery("SELECT(.+)"). @@ -101,6 +103,7 @@ func TestFileSystem_GetContent(t *testing.T) { rs, err = fs.GetContent(ctx, "/TestFileSystem_GetContent.txt") asserts.Equal(serializer.CodeIOFailed, err.(serializer.AppError).Code) asserts.NoError(mock.ExpectationsWereMet()) + fs.CleanTargets() // 打开成功 mock.ExpectQuery("SELECT(.+)"). diff --git a/pkg/filesystem/local/handler.go b/pkg/filesystem/local/handler.go index 828852e..9281c66 100644 --- a/pkg/filesystem/local/handler.go +++ b/pkg/filesystem/local/handler.go @@ -32,12 +32,13 @@ func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, err } // 开启一个协程,用于请求结束后关闭reader - go closeReader(ctx, file) + // go closeReader(ctx, file) return file, nil } // closeReader 用于在请求结束后关闭reader +// TODO 让业务代码自己关闭 func closeReader(ctx context.Context, closer io.Closer) { select { case <-ctx.Done(): diff --git a/pkg/serializer/user.go b/pkg/serializer/user.go index a5cd139..59d5279 100644 --- a/pkg/serializer/user.go +++ b/pkg/serializer/user.go @@ -35,9 +35,11 @@ type policy struct { } type group struct { - AllowShare bool `json:"allowShare"` - AllowRemoteDownload bool `json:"allowRemoteDownload"` - AllowTorrentDownload bool `json:"allowTorrentDownload"` + ID uint `json:"id"` + Name string `json:"name"` + AllowShare bool `json:"allowShare"` + AllowRemoteDownload bool `json:"allowRemoteDownload"` + AllowTorrentDownload bool `json:"allowTorrentDownload"` } type storage struct { @@ -65,6 +67,8 @@ func BuildUser(user model.User) User { AllowGetSource: user.Policy.IsOriginLinkEnable, }, Group: group{ + ID: user.GroupID, + Name: user.Group.Name, AllowShare: user.Group.ShareEnabled, AllowRemoteDownload: aria2Option[0], AllowTorrentDownload: aria2Option[2], diff --git a/routers/controllers/file.go b/routers/controllers/file.go index eb24ada..b6d8916 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -11,6 +11,7 @@ import ( "github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/service/explorer" "github.com/gin-gonic/gin" + "io" "net/http" "net/url" "strconv" @@ -99,6 +100,11 @@ func Thumb(c *gin.Context) { } http.ServeContent(c.Writer, c.Request, "thumb.png", fs.FileTarget[0].UpdatedAt, resp.Content) + // 检查是否需要关闭文件 + if fc, ok := resp.Content.(io.Closer); ok { + defer fc.Close() + } + } // Download 文件下载 diff --git a/service/explorer/file.go b/service/explorer/file.go index c6f4178..83decce 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -6,6 +6,7 @@ import ( "github.com/HFO4/cloudreve/pkg/filesystem/fsctx" "github.com/HFO4/cloudreve/pkg/serializer" "github.com/gin-gonic/gin" + "io" "net/http" ) @@ -41,6 +42,11 @@ func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Con // 发送文件 http.ServeContent(c.Writer, c.Request, service.Name, fs.FileTarget[0].UpdatedAt, rs) + // 检查是否需要关闭文件 + if fc, ok := rs.(io.Closer); ok { + defer fc.Close() + } + return serializer.Response{ Code: 0, } @@ -66,6 +72,11 @@ func (service *FileDownloadService) Download(ctx context.Context, c *gin.Context // 发送文件 http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs) + // 检查是否需要关闭文件 + if fc, ok := rs.(io.Closer); ok { + defer fc.Close() + } + return serializer.Response{ Code: 0, }