2022-07-11 17:12:50 +08:00
|
|
|
package handles
|
2022-06-29 17:31:37 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alist-org/alist/v3/internal/aria2"
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-12-18 19:51:20 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/op"
|
2022-06-29 17:31:37 +08:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SetAria2Req struct {
|
2022-06-29 20:07:33 +08:00
|
|
|
Uri string `json:"uri" form:"uri"`
|
|
|
|
Secret string `json:"secret" form:"secret"`
|
2022-06-29 17:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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},
|
|
|
|
}
|
2022-12-18 19:51:20 +08:00
|
|
|
if err := op.SaveSettingItems(items); err != nil {
|
2022-06-29 17:31:37 +08:00
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-07-12 14:02:29 +08:00
|
|
|
version, err := aria2.InitClient(2)
|
2022-06-29 17:31:37 +08:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-07-12 14:02:29 +08:00
|
|
|
common.SuccessResp(c, version)
|
2022-06-29 17:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddAria2Req struct {
|
|
|
|
Urls []string `json:"urls"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAria2(c *gin.Context) {
|
|
|
|
user := c.MustGet("user").(*model.User)
|
2022-06-29 18:03:12 +08:00
|
|
|
if !user.CanAddAria2Tasks() {
|
2022-06-29 17:31:37 +08:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 21:38:00 +08:00
|
|
|
reqPath, err := user.JoinPath(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 403)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 17:31:37 +08:00
|
|
|
for _, url := range req.Urls {
|
2022-11-30 21:38:00 +08:00
|
|
|
err := aria2.AddURI(c, url, reqPath)
|
2022-06-29 17:31:37 +08:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|