From cf8ab29a17e1cfa38277d8a35884eb921972005d Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Thu, 27 Apr 2023 16:33:01 +0800 Subject: [PATCH] feat: optional allow be mounted (close #4218) --- drivers/alist_v3/driver.go | 31 +++++++++++++++++++++++++++++- drivers/alist_v3/meta.go | 1 + internal/bootstrap/data/setting.go | 1 + internal/conf/const.go | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/drivers/alist_v3/driver.go b/drivers/alist_v3/driver.go index ab1e03c8..f30bdcdb 100644 --- a/drivers/alist_v3/driver.go +++ b/drivers/alist_v3/driver.go @@ -2,13 +2,17 @@ package alist_v3 import ( "context" + "fmt" "net/http" "path" "strconv" "strings" + "github.com/alist-org/alist/v3/drivers/base" + "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/server/common" "github.com/go-resty/resty/v2" ) @@ -32,8 +36,33 @@ func (d *AListV3) Init(ctx context.Context) error { _, err := d.request("/me", http.MethodGet, func(req *resty.Request) { req.SetResult(&resp) }) + if err != nil { + return err + } + // if the username is not empty and the username is not the same as the current username, then login again if d.Username != "" && d.Username != resp.Data.Username { - return d.login() + err = d.login() + if err != nil { + return err + } + } + // re-get the user info + _, err = d.request("/me", http.MethodGet, func(req *resty.Request) { + req.SetResult(&resp) + }) + if err != nil { + return err + } + if resp.Data.Role == model.GUEST { + url := d.Address + "/api/public/settings" + res, err := base.RestyClient.R().Get(url) + if err != nil { + return err + } + allowMounted := utils.Json.Get(res.Body(), "data", conf.AllowMounted).ToString() == "true" + if !allowMounted { + return fmt.Errorf("the site does not allow mounted") + } } return err } diff --git a/drivers/alist_v3/meta.go b/drivers/alist_v3/meta.go index d3f877fd..bb3d35ae 100644 --- a/drivers/alist_v3/meta.go +++ b/drivers/alist_v3/meta.go @@ -18,6 +18,7 @@ var config = driver.Config{ Name: "AList V3", LocalSort: true, DefaultRoot: "/", + CheckStatus: true, } func init() { diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index 49d7c5b8..b8472fa4 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -85,6 +85,7 @@ func InitialSettings() []model.SettingItem { {Key: "pagination_type", Value: "all", Type: conf.TypeSelect, Options: "all,pagination,load_more,auto_load_more", Group: model.SITE}, {Key: "default_page_size", Value: "30", Type: conf.TypeNumber, Group: model.SITE}, {Key: conf.AllowIndexed, Value: "false", Type: conf.TypeBool, Group: model.SITE}, + {Key: conf.AllowMounted, Value: "true", Type: conf.TypeBool, Group: model.SITE}, // style settings {Key: conf.Logo, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeText, Group: model.STYLE}, {Key: conf.Favicon, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.STYLE}, diff --git a/internal/conf/const.go b/internal/conf/const.go index 3bbf4fc2..bed55d65 100644 --- a/internal/conf/const.go +++ b/internal/conf/const.go @@ -14,6 +14,7 @@ const ( SiteTitle = "site_title" Announcement = "announcement" AllowIndexed = "allow_indexed" + AllowMounted = "allow_mounted" Logo = "logo" Favicon = "favicon"