Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions drivers/139/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
11 changes: 6 additions & 5 deletions drivers/alias/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions drivers/alias/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions drivers/alist_v3/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions internal/model/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
11 changes: 11 additions & 0 deletions internal/op/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions server/common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
3 changes: 3 additions & 0 deletions server/handles/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading