parent
3ed84ad5ec
commit
491e4de9de
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
package aria2
|
||||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/aria2"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SelectFileService 选择要下载的文件服务
|
||||
type SelectFileService struct {
|
||||
Indexes []int `json:"indexes" binding:"required"`
|
||||
}
|
||||
|
||||
// Select 选取要下载的文件
|
||||
func (service *SelectFileService) Select(c *gin.Context) serializer.Response {
|
||||
userCtx, _ := c.Get("user")
|
||||
user := userCtx.(*model.User)
|
||||
|
||||
// 查找下载记录
|
||||
download, err := model.GetDownloadByGid(c.Param("gid"), user.ID)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err)
|
||||
}
|
||||
|
||||
if download.StatusInfo.BitTorrent.Mode != "multi" || (download.Status != aria2.Downloading && download.Status != aria2.Paused) {
|
||||
return serializer.Err(serializer.CodeNoPermissionErr, "此下载任务无法选取文件", err)
|
||||
}
|
||||
|
||||
// 选取下载
|
||||
if err := aria2.Instance.Select(download, service.Indexes); err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, "操作失败", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
|
||||
}
|
Loading…
Reference in new issue