Feat: download torrent / multiple file / select file

This commit is contained in:
HFO4
2020-02-05 15:11:34 +08:00
parent 3ed84ad5ec
commit 491e4de9de
12 changed files with 197 additions and 55 deletions

View File

@@ -1,17 +1,72 @@
package controllers
import (
"context"
ariaCall "github.com/HFO4/cloudreve/pkg/aria2"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/service/aria2"
"github.com/HFO4/cloudreve/service/explorer"
"github.com/gin-gonic/gin"
"strings"
)
// AddAria2URL 添加离线下载URL
func AddAria2URL(c *gin.Context) {
var addService aria2.AddURLService
if err := c.ShouldBindJSON(&addService); err == nil {
res := addService.Add(c)
res := addService.Add(c, ariaCall.URLTask)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// SelectAria2File 选择多文件离线下载中要下载的文件
func SelectAria2File(c *gin.Context) {
var selectService aria2.SelectFileService
if err := c.ShouldBindJSON(&selectService); err == nil {
res := selectService.Select(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AddAria2Torrent 添加离线下载种子
func AddAria2Torrent(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.SingleFileService
if err := c.ShouldBindUri(&service); err == nil {
// 验证必须是种子文件
filePath := c.Param("path")
if !strings.HasSuffix(filePath, ".torrent") {
c.JSON(200, serializer.ParamErr("只能下载 .torrent 文件", nil))
return
}
// 获取种子内容的下载地址
res := service.CreateDownloadSession(ctx, c)
if res.Code != 0 {
c.JSON(200, res)
return
}
// 创建下载任务
var addService aria2.AddURLService
addService.URL = res.Data.(string)
if err := c.ShouldBindJSON(&addService); err == nil {
addService.URL = res.Data.(string)
res := addService.Add(c, ariaCall.URLTask)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
} else {
c.JSON(200, ErrorResponse(err))
}
}

View File

@@ -276,7 +276,12 @@ func InitMasterRouter() *gin.Engine {
// 离线下载任务
aria2 := auth.Group("aria2")
{
// 创建URL下载任务
aria2.POST("url", controllers.AddAria2URL)
// 创建种子下载任务
aria2.POST("torrent/*path", controllers.AddAria2Torrent)
// 重新选择要下载的文件
aria2.PUT("select/:gid", controllers.SelectAria2File)
}
// 目录