From e8aa0435c5d094df13e2ed065c173155403023d5 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Thu, 12 Dec 2019 10:04:24 +0800 Subject: [PATCH] Feat: archive & download routers for test --- models/migration.go | 4 ++++ pkg/filesystem/archive.go | 15 +++++++++++++++ pkg/serializer/error.go | 8 ++++---- pkg/task/pool.go | 21 +++++++++++++++++++++ service/explorer/objects.go | 20 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 pkg/task/pool.go diff --git a/models/migration.go b/models/migration.go index 349e146..a57c7ea 100644 --- a/models/migration.go +++ b/models/migration.go @@ -166,6 +166,10 @@ func addDefaultGroups() { Color: "danger", WebDAVEnabled: true, Aria2Option: "0,0,0", + OptionsSerialized: GroupOption{ + ArchiveDownloadEnabled: true, + ArchiveTaskEnabled: true, + }, } if err := DB.Create(&defaultAdminGroup).Error; err != nil { util.Log().Panic("无法创建管理用户组, %s", err) diff --git a/pkg/filesystem/archive.go b/pkg/filesystem/archive.go index e4b5af9..2a8f994 100644 --- a/pkg/filesystem/archive.go +++ b/pkg/filesystem/archive.go @@ -1 +1,16 @@ package filesystem + +import ( + "context" + "io" +) + +/* ============== + 压缩/解压缩 + ============== +*/ + +// Compress 创建给定目录和文件的压缩文件 +func (fs *FileSystem) Compress(ctx context.Context, dirs, files []uint) (io.ReadSeeker, error) { + return nil, nil +} diff --git a/pkg/serializer/error.go b/pkg/serializer/error.go index bda0c33..eec0ae2 100644 --- a/pkg/serializer/error.go +++ b/pkg/serializer/error.go @@ -56,16 +56,16 @@ const ( CodeObjectExist = 40004 // CodeSignExpired 签名过期 CodeSignExpired = 40005 + // CodePolicyNotAllowed 当前存储策略不允许 + CodePolicyNotAllowed = 40006 + // CodeGroupNotAllowed 用户组无法进行此操作 + CodeGroupNotAllowed = 40007 // CodeDBError 数据库操作失败 CodeDBError = 50001 // CodeEncryptError 加密失败 CodeEncryptError = 50002 - // CodePolicyNotAllowed 当前存储策略不允许 - CodePolicyNotAllowed = 50003 // CodeIOFailed IO操作失败 CodeIOFailed = 50004 - // CodeGroupNotAllowed 当前用户组不允许 - CodeGroupNotAllowed = 50005 //CodeParamErr 各种奇奇怪怪的参数错误 CodeParamErr = 40001 // CodeNotSet 未定错误,后续尝试从error中获取 diff --git a/pkg/task/pool.go b/pkg/task/pool.go new file mode 100644 index 0000000..d3ef610 --- /dev/null +++ b/pkg/task/pool.go @@ -0,0 +1,21 @@ +package task + +type Pool struct { + // 容量 + capacity int + // 终止信号 + terminateSignal chan error + // 全部任务完成的信号 + finishSignal chan bool +} + +type Worker interface { + Do() error +} + +func (pool *Pool) Submit(worker Worker) { + err := worker.Do() + if err != nil { + close(pool.terminateSignal) + } +} diff --git a/service/explorer/objects.go b/service/explorer/objects.go index 15d7d24..d399b69 100644 --- a/service/explorer/objects.go +++ b/service/explorer/objects.go @@ -2,6 +2,7 @@ package explorer import ( "context" + "fmt" "github.com/HFO4/cloudreve/pkg/filesystem" "github.com/HFO4/cloudreve/pkg/serializer" "github.com/gin-gonic/gin" @@ -28,6 +29,25 @@ type ItemService struct { // ArchiveAndDownload 创建归档并下載文件 func (service *ItemService) ArchiveAndDownload(ctx context.Context, c *gin.Context) serializer.Response { + // 创建文件系统 + fs, err := filesystem.NewFileSystemFromContext(c) + if err != nil { + return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + } + + // 检查用户组权限 + if !fs.User.Group.OptionsSerialized.ArchiveDownloadEnabled { + return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil) + } + + // 开始压缩,获取压缩后的stream + rs, err := fs.Compress(ctx, service.Dirs, service.Items) + if err != nil { + return serializer.Err(serializer.CodeNotSet, "无法创建压缩文件", err) + } + + fmt.Println(rs) + return serializer.Response{ Code: 0, }