Skip to content

Commit

Permalink
feat: set aria2 client and add url to aria2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 29, 2022
1 parent a09a1b8 commit 8abee65
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
8 changes: 8 additions & 0 deletions internal/aria2/aria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aria2

import (
"context"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/aria2/rpc"
"github.com/alist-org/alist/v3/pkg/task"
"github.com/pkg/errors"
Expand All @@ -13,6 +15,12 @@ var DownTaskManager = task.NewTaskManager[string](3)
var notify = NewNotify()
var client rpc.Client

func InitClient(timeout int) error {
uri := setting.GetByKey(conf.Aria2Uri)
secret := setting.GetByKey(conf.Aria2Secret)
return InitAria2Client(uri, secret, timeout)
}

func InitAria2Client(uri string, secret string, timeout int) error {
c, err := rpc.New(context.Background(), uri, secret, time.Duration(timeout)*time.Second, notify)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func initialSettings() {
{Key: conf.CustomizeHead, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.LinkExpiration, Value: "0", Type: conf.TypeNumber, Group: model.GLOBAL, Flag: model.PRIVATE},
// aria2 settings
{Key: conf.Aria2Uri, Value: "http://localhost:6800/jsonrpc", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
{Key: conf.Aria2Secret, Value: "", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
// single settings
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
}
Expand Down
36 changes: 21 additions & 15 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@ const (
)

const (
VERSION = "version"
BaseUrl = "base_url"
SiteTitle = "site_title"
SiteLogo = "site_logo"
Favicon = "favicon"
Announcement = "announcement"
IconColor = "icon_color"
TextTypes = "text_types"
AudioTypes = "audio_types"
VideoTypes = "video_types"
ProxyTypes = "proxy_types"
PdfViewerUrl = "pdf_viewer_url"
AudioAutoplay = "audio_autoplay"
VideoAutoplay = "video_autoplay"
VERSION = "version"
BaseUrl = "base_url"
SiteTitle = "site_title"
SiteLogo = "site_logo"
Favicon = "favicon"
Announcement = "announcement"
IconColor = "icon_color"

TextTypes = "text_types"
AudioTypes = "audio_types"
VideoTypes = "video_types"
ProxyTypes = "proxy_types"
PdfViewerUrl = "pdf_viewer_url"
AudioAutoplay = "audio_autoplay"
VideoAutoplay = "video_autoplay"

HideFiles = "hide_files"
GlobalReadme = "global_readme"
CustomizeHead = "customize_head"
CustomizeBody = "customize_body"
LinkExpiration = "link_expiration"
Token = "token"

Aria2Uri = "aria2_uri"
Aria2Secret = "aria2_secret"

Token = "token"
)
1 change: 1 addition & 0 deletions internal/model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
PREVIEW
GLOBAL
SINGLE
ARIA2
)

const (
Expand Down
69 changes: 69 additions & 0 deletions server/controllers/aria2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package controllers

import (
"github.com/alist-org/alist/v3/internal/aria2"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
stdpath "path"
)

type SetAria2Req struct {
Uri string `json:"uri"`
Secret string `json:"secret"`
}

func SetAria2(c *gin.Context) {
var req SetAria2Req
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
items := []model.SettingItem{
{Key: conf.Aria2Uri, Value: req.Uri, Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
{Key: conf.Aria2Secret, Value: req.Secret, Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
}
if err := db.SaveSettingItems(items); err != nil {
common.ErrorResp(c, err, 500)
return
}
err := aria2.InitClient(2)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

type AddAria2Req struct {
Urls []string `json:"urls"`
Path string `json:"path"`
}

func AddAria2(c *gin.Context) {
user := c.MustGet("user").(*model.User)
if !user.IsAdmin() && !user.Aira2 {
common.ErrorStrResp(c, "permission denied", 403)
return
}
if !aria2.IsAria2Ready() {
common.ErrorStrResp(c, "aria2 not ready", 500)
return
}
var req AddAria2Req
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = stdpath.Join(user.BasePath, req.Path)
for _, url := range req.Urls {
err := aria2.AddURI(c, url, req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

0 comments on commit 8abee65

Please sign in to comment.