feat: get file source link in batch

pull/1259/head
HFO4 2 years ago
parent 4a4375a796
commit 1038bae238

@ -1 +1 @@
Subproject commit c4a7dfea2c05077e6e7c7042c8598886bf6f5531
Subproject commit e4ccca3e28f2df52646385ef3957e79f4d203f94

@ -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获取用户组

@ -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 {

@ -80,6 +80,8 @@ const (
CodeInvalidChunkIndex = 400012
// CodeInvalidContentLength 无效的正文长度
CodeInvalidContentLength = 400013
// CodeBatchSourceSize 超出批量获取外链限制
CodeBatchSourceSize = 40014
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败

@ -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"`
}

@ -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),
}

@ -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))
}
// 获取缩略图

@ -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)
// 创建文件压缩任务

@ -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,
}
}

Loading…
Cancel
Save