forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package aria2 | ||
|
||
import ( | ||
model "github.com/HFO4/cloudreve/models" | ||
"github.com/HFO4/cloudreve/pkg/serializer" | ||
"github.com/HFO4/cloudreve/pkg/util" | ||
"net/url" | ||
) | ||
|
||
// Instance 默认使用的Aria2处理实例 | ||
var Instance Aria2 = &DummyAria2{} | ||
|
||
// Aria2 离线下载处理接口 | ||
type Aria2 interface { | ||
// CreateTask 创建新的任务 | ||
CreateTask(task *model.Download) error | ||
} | ||
|
||
const ( | ||
// URLTask 从URL添加的任务 | ||
URLTask = iota | ||
// TorrentTask 种子任务 | ||
TorrentTask | ||
) | ||
|
||
const ( | ||
// Ready 准备就绪 | ||
Ready = iota | ||
) | ||
|
||
var ( | ||
// ErrNotEnabled 功能未开启错误 | ||
ErrNotEnabled = serializer.NewError(serializer.CodeNoPermissionErr, "离线下载功能未开启", nil) | ||
) | ||
|
||
// DummyAria2 未开启Aria2功能时使用的默认处理器 | ||
type DummyAria2 struct { | ||
} | ||
|
||
// CreateTask 创建新任务,此处直接返回未开启错误 | ||
func (instance *DummyAria2) CreateTask(task *model.Download) error { | ||
return ErrNotEnabled | ||
} | ||
|
||
// Init 初始化 | ||
func Init() { | ||
options := model.GetSettingByNames("aria2_rpcurl", "aria2_token", "aria2_options") | ||
timeout := model.GetIntSetting("aria2_call_timeout", 5) | ||
if options["aria2_rpcurl"] == "" { | ||
// 未开启Aria2服务 | ||
return | ||
} | ||
|
||
util.Log().Info("初始化 aria2 RPC 服务[%s]", options["aria2_rpcurl"]) | ||
client := &RPCService{} | ||
if previousClient, ok := Instance.(*RPCService); ok { | ||
client = previousClient | ||
} | ||
|
||
// 解析RPC服务地址 | ||
server, err := url.Parse(options["aria2_rpcurl"]) | ||
if err != nil { | ||
util.Log().Warning("无法解析 aria2 RPC 服务地址,%s", err) | ||
return | ||
} | ||
server.Path = "/jsonrpc" | ||
|
||
// todo 加载自定义下载配置 | ||
if err := client.Init(server.String(), options["aria2_token"], timeout, []interface{}{}); err != nil { | ||
util.Log().Warning("初始化 aria2 RPC 服务失败,%s", err) | ||
return | ||
} | ||
|
||
Instance = client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package aria2 | ||
|
||
import ( | ||
"context" | ||
model "github.com/HFO4/cloudreve/models" | ||
"github.com/zyxar/argo/rpc" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// RPCService 通过RPC服务的Aria2任务管理器 | ||
type RPCService struct { | ||
options *clientOptions | ||
caller rpc.Client | ||
} | ||
|
||
type clientOptions struct { | ||
Options []interface{} // 创建下载时额外添加的设置 | ||
} | ||
|
||
// Init 初始化 | ||
func (client *RPCService) Init(server, secret string, timeout int, options []interface{}) error { | ||
// 客户端已存在,则关闭先前连接 | ||
if client.caller != nil { | ||
client.caller.Close() | ||
} | ||
|
||
client.options = &clientOptions{ | ||
Options: options, | ||
} | ||
caller, err := rpc.New(context.Background(), server, secret, time.Duration(timeout)*time.Second, | ||
rpc.DummyNotifier{}) | ||
client.caller = caller | ||
return err | ||
} | ||
|
||
// CreateTask 创建新任务 | ||
func (client *RPCService) CreateTask(task *model.Download) error { | ||
// 生成存储路径 | ||
task.Path = filepath.Join( | ||
model.GetSettingByName("aria2_temp_path"), | ||
"aria2", | ||
strconv.FormatInt(time.Now().UnixNano(), 10), | ||
) | ||
|
||
// 创建下载任务 | ||
gid, err := client.caller.AddURI(task.Source, map[string]string{"dir": task.Path}) | ||
if err != nil || gid == "" { | ||
return err | ||
} | ||
|
||
// 保存到数据库 | ||
task.GID = gid | ||
_, err = task.Create() | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/HFO4/cloudreve/service/aria2" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// AddAria2URL 添加离线下载URL | ||
func AddAria2URL(c *gin.Context) { | ||
var addService aria2.AddURLService | ||
if err := c.ShouldBindJSON(&addService); err == nil { | ||
res := addService.Add(c) | ||
c.JSON(200, res) | ||
} else { | ||
c.JSON(200, ErrorResponse(err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.