feat: add `proxy_range` option for `139Yun` `Alias` `AList V3` (#6496)

pull/6500/head
j2rong4cn 2024-05-22 23:31:42 +08:00 committed by GitHub
parent 7013d1b7b8
commit 5f60b51cf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 62 additions and 14 deletions

View File

@ -14,12 +14,15 @@ type Addition struct {
}
var config = driver.Config{
Name: "139Yun",
LocalSort: true,
Name: "139Yun",
LocalSort: true,
ProxyRangeOption: true,
}
func init() {
op.RegisterDriver(func() driver.Driver {
return &Yun139{}
d := &Yun139{}
d.ProxyRange = true
return d
})
}

View File

@ -14,11 +14,12 @@ type Addition struct {
}
var config = driver.Config{
Name: "Alias",
LocalSort: true,
NoCache: true,
NoUpload: true,
DefaultRoot: "/",
Name: "Alias",
LocalSort: true,
NoCache: true,
NoUpload: true,
DefaultRoot: "/",
ProxyRangeOption: true,
}
func init() {

View File

@ -103,12 +103,16 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs)
return nil, err
}
if common.ShouldProxy(storage, stdpath.Base(sub)) {
return &model.Link{
link := &model.Link{
URL: fmt.Sprintf("%s/p%s?sign=%s",
common.GetApiUrl(args.HttpReq),
utils.EncodePath(reqPath, true),
sign.Sign(reqPath)),
}, nil
}
if args.HttpReq != nil && d.ProxyRange {
link.RangeReadCloser = common.NoProxyRange
}
return link, nil
}
link, _, err := fs.Link(ctx, reqPath, args)
return link, err

View File

@ -16,10 +16,11 @@ type Addition struct {
}
var config = driver.Config{
Name: "AList V3",
LocalSort: true,
DefaultRoot: "/",
CheckStatus: true,
Name: "AList V3",
LocalSort: true,
DefaultRoot: "/",
CheckStatus: true,
ProxyRangeOption: true,
}
func init() {

View File

@ -12,6 +12,7 @@ type Config struct {
CheckStatus bool `json:"-"`
Alert string `json:"alert"` //info,success,warning,danger
NoOverwriteUpload bool `json:"-"` // whether to support overwrite upload
ProxyRangeOption bool `json:"-"`
}
func (c Config) MustProxy() bool {

View File

@ -27,6 +27,7 @@ type Sort struct {
type Proxy struct {
WebProxy bool `json:"web_proxy"`
WebdavPolicy string `json:"webdav_policy"`
ProxyRange bool `json:"proxy_range"`
DownProxyUrl string `json:"down_proxy_url"`
}

View File

@ -93,6 +93,17 @@ func getMainItems(config driver.Config) []driver.Item {
Required: true,
},
}...)
if config.ProxyRangeOption {
item := driver.Item{
Name: "proxy_range",
Type: conf.TypeBool,
Help: "Need to enable proxy",
}
if config.Name == "139Yun" {
item.Default = "true"
}
items = append(items, item)
}
} else {
items = append(items, driver.Item{
Name: "webdav_policy",

View File

@ -9,8 +9,10 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/net"
"github.com/alist-org/alist/v3/internal/stream"
"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/alist-org/alist/v3/pkg/utils"
log "github.com/sirupsen/logrus"
)
func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error {
@ -82,3 +84,21 @@ func attachFileName(w http.ResponseWriter, file model.Obj) {
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, fileName, url.PathEscape(fileName)))
w.Header().Set("Content-Type", utils.GetMimeType(fileName))
}
var NoProxyRange = &model.RangeReadCloser{}
func ProxyRange(link *model.Link, size int64) {
if link.MFile != nil {
return
}
if link.RangeReadCloser == nil {
var rrc, err = stream.GetRangeReadCloserFromLink(size, link)
if err != nil {
log.Warnf("ProxyRange error: %s", err)
return
}
link.RangeReadCloser = rrc
} else if link.RangeReadCloser == NoProxyRange {
link.RangeReadCloser = nil
}
}

View File

@ -106,6 +106,9 @@ func Proxy(c *gin.Context) {
return
}
}
if storage.GetStorage().ProxyRange {
common.ProxyRange(link, file.GetSize())
}
err = common.Proxy(c.Writer, c.Request, link, file)
if err != nil {
common.ErrorResp(c, err, 500, true)

View File

@ -247,6 +247,9 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta
if err != nil {
return http.StatusInternalServerError, err
}
if storage.GetStorage().ProxyRange {
common.ProxyRange(link, fi.GetSize())
}
err = common.Proxy(w, r, link, fi)
if err != nil {
log.Errorf("webdav proxy error: %+v", err)