mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: download torrent / multiple file / select file
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
// 目录
|
||||
|
||||
Reference in New Issue
Block a user