From 1038bae238172ea1d5e0181b52fa2f0313f6b5d9 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 29 Apr 2022 19:59:25 +0800 Subject: [PATCH] feat: get file source link in batch --- assets | 2 +- models/group.go | 19 +++++++++++-------- models/migration.go | 6 +++++- pkg/serializer/error.go | 2 ++ pkg/serializer/explorer.go | 8 ++++++++ pkg/serializer/user.go | 2 ++ routers/controllers/file.go | 32 ++++++-------------------------- routers/router.go | 2 +- service/explorer/file.go | 37 +++++++++++++++++++++++++++++++++++++ 9 files changed, 73 insertions(+), 37 deletions(-) diff --git a/assets b/assets index c4a7dfe..e4ccca3 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit c4a7dfea2c05077e6e7c7042c8598886bf6f5531 +Subproject commit e4ccca3e28f2df52646385ef3957e79f4d203f94 diff --git a/models/group.go b/models/group.go index b31f7cd..cc34d70 100644 --- a/models/group.go +++ b/models/group.go @@ -23,14 +23,17 @@ type Group struct { // GroupOption 用户组其他配置 type GroupOption struct { - ArchiveDownload bool `json:"archive_download,omitempty"` // 打包下载 - ArchiveTask bool `json:"archive_task,omitempty"` // 在线压缩 - CompressSize uint64 `json:"compress_size,omitempty"` // 可压缩大小 - DecompressSize uint64 `json:"decompress_size,omitempty"` - OneTimeDownload bool `json:"one_time_download,omitempty"` - ShareDownload bool `json:"share_download,omitempty"` - Aria2 bool `json:"aria2,omitempty"` // 离线下载 - Aria2Options map[string]interface{} `json:"aria2_options,omitempty"` // 离线下载用户组配置 + ArchiveDownload bool `json:"archive_download,omitempty"` // 打包下载 + ArchiveTask bool `json:"archive_task,omitempty"` // 在线压缩 + CompressSize uint64 `json:"compress_size,omitempty"` // 可压缩大小 + DecompressSize uint64 `json:"decompress_size,omitempty"` + OneTimeDownload bool `json:"one_time_download,omitempty"` + ShareDownload bool `json:"share_download,omitempty"` + Aria2 bool `json:"aria2,omitempty"` // 离线下载 + Aria2Options map[string]interface{} `json:"aria2_options,omitempty"` // 离线下载用户组配置 + SourceBatchSize int `json:"source_batch,omitempty"` + Aria2BatchSize int `json:"aria2_batch,omitempty"` + Aria2TaskSizeLimit uint64 `json:"aria2_size,omitempty"` } // GetGroupByID 用ID获取用户组 diff --git a/models/migration.go b/models/migration.go index ac36d32..07012b6 100644 --- a/models/migration.go +++ b/models/migration.go @@ -108,6 +108,8 @@ func addDefaultGroups() { ArchiveTask: true, ShareDownload: true, Aria2: true, + SourceBatchSize: 1000, + Aria2BatchSize: 50, }, } if err := DB.Create(&defaultAdminGroup).Error; err != nil { @@ -126,7 +128,9 @@ func addDefaultGroups() { ShareEnabled: true, WebDAVEnabled: true, OptionsSerialized: GroupOption{ - ShareDownload: true, + ShareDownload: true, + SourceBatchSize: 10, + Aria2BatchSize: 1, }, } if err := DB.Create(&defaultAdminGroup).Error; err != nil { diff --git a/pkg/serializer/error.go b/pkg/serializer/error.go index 910f3f8..8de5d59 100644 --- a/pkg/serializer/error.go +++ b/pkg/serializer/error.go @@ -80,6 +80,8 @@ const ( CodeInvalidChunkIndex = 400012 // CodeInvalidContentLength 无效的正文长度 CodeInvalidContentLength = 400013 + // CodeBatchSourceSize 超出批量获取外链限制 + CodeBatchSourceSize = 40014 // CodeDBError 数据库操作失败 CodeDBError = 50001 // CodeEncryptError 加密失败 diff --git a/pkg/serializer/explorer.go b/pkg/serializer/explorer.go index 62fbeb7..afa5ac4 100644 --- a/pkg/serializer/explorer.go +++ b/pkg/serializer/explorer.go @@ -76,3 +76,11 @@ func BuildObjectList(parent uint, objects []Object, policy *model.Policy) Object return res } + +// Sources 获取外链的结果响应 +type Sources struct { + URL string `json:"url"` + Name string `json:"name"` + Parent uint `json:"parent"` + Error string `json:"error,omitempty"` +} diff --git a/pkg/serializer/user.go b/pkg/serializer/user.go index fcbc4db..2df80bd 100644 --- a/pkg/serializer/user.go +++ b/pkg/serializer/user.go @@ -40,6 +40,7 @@ type group struct { ShareDownload bool `json:"shareDownload"` CompressEnabled bool `json:"compress"` WebDAVEnabled bool `json:"webdav"` + SourceBatchSize int `json:"sourceBatch"` } type tag struct { @@ -98,6 +99,7 @@ func BuildUser(user model.User) User { ShareDownload: user.Group.OptionsSerialized.ShareDownload, CompressEnabled: user.Group.OptionsSerialized.ArchiveTask, WebDAVEnabled: user.Group.WebDAVEnabled, + SourceBatchSize: user.Group.OptionsSerialized.SourceBatchSize, }, Tags: buildTagRes(tags), } diff --git a/routers/controllers/file.go b/routers/controllers/file.go index 67bbb73..8dca01e 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -121,20 +121,6 @@ func GetSource(c *gin.Context) { c.JSON(200, serializer.ParamErr("文件不存在", err)) return } - - sourceURL, err := fs.GetSource(ctx, fileID.(uint)) - if err != nil { - c.JSON(200, serializer.Err(serializer.CodeNotSet, err.Error(), err)) - return - } - - c.JSON(200, serializer.Response{ - Code: 0, - Data: struct { - URL string `json:"url"` - }{URL: sourceURL}, - }) - } // Thumb 获取文件缩略图 @@ -143,18 +129,12 @@ func Thumb(c *gin.Context) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - fs, err := filesystem.NewFileSystemFromContext(c) - if err != nil { - c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)) - return - } - defer fs.Recycle() - - // 获取文件ID - fileID, ok := c.Get("object_id") - if !ok { - c.JSON(200, serializer.ParamErr("文件不存在", err)) - return + var service explorer.ItemIDService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.Sources(ctx, c) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) } // 获取缩略图 diff --git a/routers/router.go b/routers/router.go index da2cc4c..e2553d9 100644 --- a/routers/router.go +++ b/routers/router.go @@ -558,7 +558,7 @@ func InitMasterRouter() *gin.Engine { // 获取缩略图 file.GET("thumb/:id", controllers.Thumb) // 取得文件外链 - file.GET("source/:id", controllers.GetSource) + file.POST("source", controllers.GetSource) // 打包要下载的文件 file.POST("archive", controllers.Archive) // 创建文件压缩任务 diff --git a/service/explorer/file.go b/service/explorer/file.go index b58185b..15c88b2 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -428,3 +428,40 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se Code: 0, } } + +// Sources 批量获取对象的外链 +func (s *ItemIDService) Sources(ctx context.Context, c *gin.Context) serializer.Response { + fs, err := filesystem.NewFileSystemFromContext(c) + if err != nil { + return serializer.Err(serializer.CodePolicyNotAllowed, "无法初始化文件系统", err) + } + defer fs.Recycle() + + if len(s.Raw().Items) > fs.User.Group.OptionsSerialized.SourceBatchSize { + return serializer.Err(serializer.CodeBatchSourceSize, "超出批量获取外链的最大数量限制", err) + } + + res := make([]serializer.Sources, 0, len(s.Raw().Items)) + for _, id := range s.Raw().Items { + fs.FileTarget = []model.File{} + sourceURL, err := fs.GetSource(ctx, id) + if len(fs.FileTarget) > 0 { + current := serializer.Sources{ + URL: sourceURL, + Name: fs.FileTarget[0].Name, + Parent: fs.FileTarget[0].FolderID, + } + + if err != nil { + current.Error = err.Error() + } + + res = append(res, current) + } + } + + return serializer.Response{ + Code: 0, + Data: res, + } +}