init
This commit is contained in:
157
service/aria2/add.go
Normal file
157
service/aria2/add.go
Normal file
@ -0,0 +1,157 @@
|
||||
package aria2
|
||||
|
||||
import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/monitor"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/mq"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AddURLService 添加URL离线下载服务
|
||||
type BatchAddURLService struct {
|
||||
URLs []string `json:"url" binding:"required"`
|
||||
Dst string `json:"dst" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// Add 主机批量创建新的链接离线下载任务
|
||||
func (service *BatchAddURLService) Add(c *gin.Context, taskType int) serializer.Response {
|
||||
// 创建文件系统
|
||||
fs, err := filesystem.NewFileSystemFromContext(c)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeCreateFSError, "", 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.CodeParentNotExist, "", nil)
|
||||
}
|
||||
|
||||
// 检查批量任务数量
|
||||
limit := fs.User.Group.OptionsSerialized.Aria2BatchSize
|
||||
if limit > 0 && len(service.URLs) > limit {
|
||||
return serializer.Err(serializer.CodeBatchAria2Size, "", 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"`
|
||||
PreferredNode uint `json:"preferred_node"`
|
||||
}
|
||||
|
||||
// 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.CodeCreateFSError, "", 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.CodeParentNotExist, "", 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, "", nil)
|
||||
}
|
||||
|
||||
if service.PreferredNode > 0 && !fs.User.Group.OptionsSerialized.SelectNode {
|
||||
return serializer.Err(serializer.CodeGroupNotAllowed, "not allowed to select nodes", nil)
|
||||
}
|
||||
|
||||
// 创建任务
|
||||
task := &model.Download{
|
||||
Status: common.Ready,
|
||||
Type: taskType,
|
||||
Dst: service.Dst,
|
||||
UserID: fs.User.ID,
|
||||
Source: service.URL,
|
||||
}
|
||||
|
||||
// 获取 Aria2 负载均衡器
|
||||
lb := aria2.GetLoadBalancer()
|
||||
|
||||
// 获取 Aria2 实例
|
||||
err, node := cluster.Default.BalanceNodeByFeature("aria2", lb, fs.User.Group.OptionsSerialized.AvailableNodes,
|
||||
service.PreferredNode)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to get Aria2 instance", err)
|
||||
}
|
||||
|
||||
// 创建任务
|
||||
gid, err := node.GetAria2Instance().CreateTask(task, fs.User.Group.OptionsSerialized.Aria2Options)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeCreateTaskError, "", err)
|
||||
}
|
||||
|
||||
task.GID = gid
|
||||
task.NodeID = node.ID()
|
||||
_, err = task.Create()
|
||||
if err != nil {
|
||||
return serializer.DBErr("Failed to create task record", err)
|
||||
}
|
||||
|
||||
// 创建任务监控
|
||||
monitor.NewMonitor(task, cluster.Default, mq.GlobalMQ)
|
||||
|
||||
return serializer.Response{}
|
||||
}
|
||||
|
||||
// Add 从机创建新的链接离线下载任务
|
||||
func Add(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response {
|
||||
caller, _ := c.Get("MasterAria2Instance")
|
||||
|
||||
// 创建任务
|
||||
gid, err := caller.(common.Aria2).CreateTask(service.Task, service.GroupOptions)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to create aria2 task", err)
|
||||
}
|
||||
|
||||
// 创建事件通知回调
|
||||
siteID, _ := c.Get("MasterSiteID")
|
||||
mq.GlobalMQ.SubscribeCallback(gid, func(message mq.Message) {
|
||||
if err := cluster.DefaultController.SendNotification(siteID.(string), message.TriggeredBy, message); err != nil {
|
||||
util.Log().Warning("Failed to send remote download task status change notifications: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
return serializer.Response{Data: gid}
|
||||
}
|
172
service/aria2/manage.go
Normal file
172
service/aria2/manage.go
Normal file
@ -0,0 +1,172 @@
|
||||
package aria2
|
||||
|
||||
import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SelectFileService 选择要下载的文件服务
|
||||
type SelectFileService struct {
|
||||
Indexes []int `json:"indexes" binding:"required"`
|
||||
}
|
||||
|
||||
// DownloadTaskService 下载任务管理服务
|
||||
type DownloadTaskService struct {
|
||||
GID string `uri:"gid" binding:"required"`
|
||||
}
|
||||
|
||||
// DownloadListService 下载列表服务
|
||||
type DownloadListService struct {
|
||||
Page uint `form:"page"`
|
||||
}
|
||||
|
||||
// Finished 获取已完成的任务
|
||||
func (service *DownloadListService) Finished(c *gin.Context, user *model.User) serializer.Response {
|
||||
// 查找下载记录
|
||||
downloads := model.GetDownloadsByStatusAndUser(service.Page, user.ID, common.Error, common.Complete, common.Canceled, common.Unknown)
|
||||
for key, download := range downloads {
|
||||
node := cluster.Default.GetNodeByID(download.GetNodeID())
|
||||
if node != nil {
|
||||
downloads[key].NodeName = node.DBModel().Name
|
||||
}
|
||||
}
|
||||
|
||||
return serializer.BuildFinishedListResponse(downloads)
|
||||
}
|
||||
|
||||
// Downloading 获取正在下载中的任务
|
||||
func (service *DownloadListService) Downloading(c *gin.Context, user *model.User) serializer.Response {
|
||||
// 查找下载记录
|
||||
downloads := model.GetDownloadsByStatusAndUser(service.Page, user.ID, common.Downloading, common.Seeding, common.Paused, common.Ready)
|
||||
intervals := make(map[uint]int)
|
||||
for key, download := range downloads {
|
||||
if _, ok := intervals[download.ID]; !ok {
|
||||
if node := cluster.Default.GetNodeByID(download.GetNodeID()); node != nil {
|
||||
intervals[download.ID] = node.DBModel().Aria2OptionsSerialized.Interval
|
||||
}
|
||||
}
|
||||
|
||||
node := cluster.Default.GetNodeByID(download.GetNodeID())
|
||||
if node != nil {
|
||||
downloads[key].NodeName = node.DBModel().Name
|
||||
}
|
||||
}
|
||||
|
||||
return serializer.BuildDownloadingResponse(downloads, intervals)
|
||||
}
|
||||
|
||||
// Delete 取消或删除下载任务
|
||||
func (service *DownloadTaskService) Delete(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, "Download record not found", err)
|
||||
}
|
||||
|
||||
if download.Status >= common.Error && download.Status <= common.Unknown {
|
||||
// 如果任务已完成,则删除任务记录
|
||||
if err := download.Delete(); err != nil {
|
||||
return serializer.DBErr("Failed to delete task record", err)
|
||||
}
|
||||
return serializer.Response{}
|
||||
}
|
||||
|
||||
// 取消任务
|
||||
node := cluster.Default.GetNodeByID(download.GetNodeID())
|
||||
if node == nil {
|
||||
return serializer.Err(serializer.CodeNodeOffline, "", err)
|
||||
}
|
||||
|
||||
if err := node.GetAria2Instance().Cancel(download); err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, "Operation failed", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
}
|
||||
|
||||
// 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, "Download record not found", err)
|
||||
}
|
||||
|
||||
if download.StatusInfo.BitTorrent.Mode != "multi" || (download.Status != common.Downloading && download.Status != common.Paused) {
|
||||
return serializer.ParamErr("You cannot select files for this task", nil)
|
||||
}
|
||||
|
||||
// 选取下载
|
||||
node := cluster.Default.GetNodeByID(download.GetNodeID())
|
||||
if err := node.GetAria2Instance().Select(download, service.Indexes); err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, "Operation failed", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
|
||||
}
|
||||
|
||||
// SlaveStatus 从机查询离线任务状态
|
||||
func SlaveStatus(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response {
|
||||
caller, _ := c.Get("MasterAria2Instance")
|
||||
|
||||
// 查询任务
|
||||
status, err := caller.(common.Aria2).Status(service.Task)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to query remote download task status", err)
|
||||
}
|
||||
|
||||
return serializer.NewResponseWithGobData(status)
|
||||
|
||||
}
|
||||
|
||||
// SlaveCancel 取消从机离线下载任务
|
||||
func SlaveCancel(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response {
|
||||
caller, _ := c.Get("MasterAria2Instance")
|
||||
|
||||
// 查询任务
|
||||
err := caller.(common.Aria2).Cancel(service.Task)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to cancel task", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
|
||||
}
|
||||
|
||||
// SlaveSelect 从机选取离线下载任务文件
|
||||
func SlaveSelect(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response {
|
||||
caller, _ := c.Get("MasterAria2Instance")
|
||||
|
||||
// 查询任务
|
||||
err := caller.(common.Aria2).Select(service.Task, service.Files)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to select files", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
|
||||
}
|
||||
|
||||
// SlaveSelect 从机选取离线下载任务文件
|
||||
func SlaveDeleteTemp(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response {
|
||||
caller, _ := c.Get("MasterAria2Instance")
|
||||
|
||||
// 查询任务
|
||||
err := caller.(common.Aria2).DeleteTempFile(service.Task)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeInternalSetting, "Failed to delete temp files", err)
|
||||
}
|
||||
|
||||
return serializer.Response{}
|
||||
|
||||
}
|
Reference in New Issue
Block a user