feat: create aria2 task in batch

pull/1259/head
HFO4 3 years ago
parent 1038bae238
commit 94507fe609

@ -23,17 +23,16 @@ 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"` // 离线下载用户组配置
SourceBatchSize int `json:"source_batch,omitempty"`
Aria2BatchSize int `json:"aria2_batch,omitempty"`
Aria2TaskSizeLimit uint64 `json:"aria2_size,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"`
}
// GetGroupByID 用ID获取用户组

@ -82,6 +82,8 @@ const (
CodeInvalidContentLength = 400013
// CodeBatchSourceSize 超出批量获取外链限制
CodeBatchSourceSize = 40014
// CodeBatchAria2Size 超出最大 Aria2 任务数量限制
CodeBatchAria2Size = 40012
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败

@ -11,7 +11,7 @@ import (
// AddAria2URL 添加离线下载URL
func AddAria2URL(c *gin.Context) {
var addService aria2.AddURLService
var addService aria2.BatchAddURLService
if err := c.ShouldBindJSON(&addService); err == nil {
res := addService.Add(c, common.URLTask)
c.JSON(200, res)
@ -52,7 +52,7 @@ func AddAria2Torrent(c *gin.Context) {
if err := c.ShouldBindJSON(&addService); err == nil {
addService.URL = res.Data.(string)
res := addService.Add(c, common.URLTask)
res := addService.Add(c, nil, common.URLTask)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))

@ -14,13 +14,13 @@ import (
)
// AddURLService 添加URL离线下载服务
type AddURLService struct {
URL string `json:"url" binding:"required"`
Dst string `json:"dst" binding:"required,min=1"`
type BatchAddURLService struct {
URLs []string `json:"url" binding:"required"`
Dst string `json:"dst" binding:"required,min=1"`
}
// Add 主机创建新的链接离线下载任务
func (service *AddURLService) Add(c *gin.Context, taskType int) serializer.Response {
// Add 主机批量创建新的链接离线下载任务
func (service *BatchAddURLService) Add(c *gin.Context, taskType int) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
@ -38,6 +38,60 @@ func (service *AddURLService) Add(c *gin.Context, taskType int) serializer.Respo
return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil)
}
// 检查批量任务数量
limit := fs.User.Group.OptionsSerialized.Aria2BatchSize
if limit > 0 && len(service.URLs) > limit {
return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil)
}
res := make([]serializer.Response, 0, len(service.URLs))
for _, target := range service.URLs {
subService := &AddURLService{
URL: target,
Dst: service.Dst,
}
addRes := subService.Add(c, fs, taskType)
res = append(res, addRes)
}
return serializer.Response{Data: res}
}
// AddURLService 添加URL离线下载服务
type AddURLService struct {
URL string `json:"url" binding:"required"`
Dst string `json:"dst" binding:"required,min=1"`
}
// Add 主机创建新的链接离线下载任务
func (service *AddURLService) Add(c *gin.Context, fs *filesystem.FileSystem, taskType int) serializer.Response {
if fs == nil {
var err error
// 创建文件系统
fs, err = filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}
defer fs.Recycle()
// 检查用户组权限
if !fs.User.Group.OptionsSerialized.Aria2 {
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
}
// 存放目录是否存在
if exist, _ := fs.IsPathExist(service.Dst); !exist {
return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil)
}
}
downloads := model.GetDownloadsByStatusAndUser(0, fs.User.ID, common.Downloading, common.Paused, common.Ready)
limit := fs.User.Group.OptionsSerialized.Aria2BatchSize
if limit > 0 && len(downloads)+1 > limit {
return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil)
}
// 创建任务
task := &model.Download{
Status: common.Ready,

Loading…
Cancel
Save