From ba34a092d1128a2a096bc2f12b587dac3bcf6862 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Thu, 12 Dec 2019 09:16:24 +0800 Subject: [PATCH] Feat: group options in database scheme --- models/group.go | 38 +++++++++++++++++++++++++++++++------ routers/controllers/file.go | 14 ++++++++++++++ routers/router.go | 2 ++ service/explorer/objects.go | 7 +++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/models/group.go b/models/group.go index 51ce165..d9f3a06 100644 --- a/models/group.go +++ b/models/group.go @@ -16,9 +16,17 @@ type Group struct { Aria2Option string Color string SpeedLimit int + Options string `json:"-",gorm:"size:4096"` // 数据库忽略字段 - PolicyList []uint `gorm:"-"` + PolicyList []uint `gorm:"-"` + OptionsSerialized GroupOption `gorm:"-"` +} + +// GroupOption 用户组其他配置 +type GroupOption struct { + ArchiveDownloadEnabled bool `json:"archive_download"` + ArchiveTaskEnabled bool `json:"archive_task"` } // GetAria2Option 获取用户离线下载设备 @@ -42,8 +50,19 @@ func GetGroupByID(ID interface{}) (Group, error) { // AfterFind 找到用户组后的钩子,处理Policy列表 func (group *Group) AfterFind() (err error) { - // 解析用户设置到OptionsSerialized - err = json.Unmarshal([]byte(group.Policies), &group.PolicyList) + // 解析用户组策略列表 + if group.Policies != "" { + err = json.Unmarshal([]byte(group.Policies), &group.PolicyList) + } + if err != nil { + return err + } + + // 解析用户组设置 + if group.Options != "" { + err = json.Unmarshal([]byte(group.Options), &group.OptionsSerialized) + } + return err } @@ -53,9 +72,16 @@ func (group *Group) BeforeSave() (err error) { return err } -//SerializePolicyList 将序列后的可选策略列表写入数据库字段 +//SerializePolicyList 将序列后的可选策略列表、配置写入数据库字段 +// TODO 完善测试 func (group *Group) SerializePolicyList() (err error) { - optionsValue, err := json.Marshal(&group.PolicyList) - group.Policies = string(optionsValue) + policies, err := json.Marshal(&group.PolicyList) + group.Policies = string(policies) + if err != nil { + return err + } + + optionsValue, err := json.Marshal(&group.OptionsSerialized) + group.Options = string(optionsValue) return err } diff --git a/routers/controllers/file.go b/routers/controllers/file.go index b6d8916..ecad5d0 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -17,6 +17,20 @@ import ( "strconv" ) +func ArchiveAndDownload(c *gin.Context) { + // 创建上下文 + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + var service explorer.ItemService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.ArchiveAndDownload(ctx, c) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} + // AnonymousGetContent 匿名获取文件资源 func AnonymousGetContent(c *gin.Context) { // 创建上下文 diff --git a/routers/router.go b/routers/router.go index b0e1f28..95d99f0 100644 --- a/routers/router.go +++ b/routers/router.go @@ -101,6 +101,8 @@ func InitRouter() *gin.Engine { file.GET("thumb/:id", controllers.Thumb) // 取得文件外链 file.GET("source/:id", controllers.GetSource) + // 测试用:压缩文件和目录并下載 + file.POST("archive", controllers.ArchiveAndDownload) } // 目录 diff --git a/service/explorer/objects.go b/service/explorer/objects.go index d5f1222..15d7d24 100644 --- a/service/explorer/objects.go +++ b/service/explorer/objects.go @@ -26,6 +26,13 @@ type ItemService struct { Dirs []uint `json:"dirs" binding:"exists"` } +// ArchiveAndDownload 创建归档并下載文件 +func (service *ItemService) ArchiveAndDownload(ctx context.Context, c *gin.Context) serializer.Response { + return serializer.Response{ + Code: 0, + } +} + // Delete 删除对象 func (service *ItemService) Delete(ctx context.Context, c *gin.Context) serializer.Response { // 创建文件系统