Skip to content

Commit

Permalink
feat(alias): support proxy and direct together
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 14, 2023
1 parent c410800 commit d9795ff
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
17 changes: 16 additions & 1 deletion drivers/alias/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package alias

import (
"context"
"fmt"
stdpath "path"
"strings"

"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
)

func (d *Alias) listRoot() []model.Obj {
Expand Down Expand Up @@ -76,6 +79,18 @@ func (d *Alias) list(ctx context.Context, dst, sub string) ([]model.Obj, error)
}

func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs) (*model.Link, error) {
link, _, err := fs.Link(ctx, stdpath.Join(dst, sub), args)
reqPath := stdpath.Join(dst, sub)
storage, err := fs.GetStorage(reqPath)
if err != nil {
return nil, err
}
if common.ShouldProxy(storage, stdpath.Base(sub)) {
return &model.Link{
URL: fmt.Sprintf("/p%s?sign=%s",
utils.EncodePath(reqPath, true),
sign.Sign(reqPath)),
}, nil
}
link, _, err := fs.Link(ctx, reqPath, args)
return link, err
}
2 changes: 1 addition & 1 deletion drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (d *Local) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
}
srcBuf = videoBuf
} else {
imgData, err := ioutil.ReadFile(fullPath)
imgData, err := os.ReadFile(fullPath)
if err != nil {
return nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion internal/fs/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package fs

import (
"context"
"strings"

"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)

Expand All @@ -13,5 +16,14 @@ func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, m
if err != nil {
return nil, nil, errors.WithMessage(err, "failed get storage")
}
return op.Link(ctx, storage, actualPath, args)
l, obj, err := op.Link(ctx, storage, actualPath, args)
if err != nil {
return nil, nil, errors.WithMessage(err, "failed link")
}
if l.URL != "" && !strings.HasPrefix(l.URL, "http://") && !strings.HasPrefix(l.URL, "https://") {
if c, ok := ctx.(*gin.Context); ok {
l.URL = common.GetApiUrl(c.Request) + l.URL
}
}
return l, obj, nil
}
17 changes: 17 additions & 0 deletions server/common/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
"strings"

"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"
)
Expand Down Expand Up @@ -49,3 +51,18 @@ func CanAccess(user *model.User, meta *model.Meta, reqPath string, password stri
// validate password
return meta.Password == password
}

// ShouldProxy TODO need optimize
// when should be proxy?
// 1. config.MustProxy()
// 2. storage.WebProxy
// 3. proxy_types
func ShouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
return false
}
28 changes: 11 additions & 17 deletions server/handles/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handles

import (
"fmt"
"io"
stdpath "path"
"strings"

Expand All @@ -14,6 +15,7 @@ import (
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

func Down(c *gin.Context) {
Expand All @@ -24,11 +26,10 @@ func Down(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if shouldProxy(storage, filename) {
if common.ShouldProxy(storage, filename) {
Proxy(c)
return
} else {

link, _, err := fs.Link(c, rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
Expand All @@ -38,6 +39,14 @@ func Down(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if link.Data != nil {
defer func(Data io.ReadCloser) {
err := Data.Close()
if err != nil {
log.Errorf("close data error: %s", err)
}
}(link.Data)
}
c.Header("Referrer-Policy", "no-referrer")
c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
if setting.GetBool(conf.ForwardDirectLinkParams) {
Expand Down Expand Up @@ -102,21 +111,6 @@ func Proxy(c *gin.Context) {
}
}

// TODO need optimize
// when should be proxy?
// 1. config.MustProxy()
// 2. storage.WebProxy
// 3. proxy_types
func shouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
return false
}

// TODO need optimize
// when can be proxy?
// 1. text file
Expand Down
10 changes: 10 additions & 0 deletions server/handles/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handles

import (
"fmt"
"io"
stdpath "path"
"regexp"

Expand All @@ -15,6 +16,7 @@ import (
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

type MkdirOrLinkReq struct {
Expand Down Expand Up @@ -379,6 +381,14 @@ func Link(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if link.Data != nil {
defer func(Data io.ReadCloser) {
err := Data.Close()
if err != nil {
log.Errorf("close link data error: %v", err)
}
}(link.Data)
}
common.SuccessResp(c, link)
return
}

3 comments on commit d9795ff

@anwen-anyi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下是测试的新问题

问题1、

文件名后面括号里面的代表 这个存储在挂载的时候使用的读取下载方式

1

问题2、

同时如果有大小写不同的文件夹也会进行二次生成
例如
本地1,本地2 分别有 video 小写的V 文件夹 OneDrive 有一个大写的 Video 文件夹
然后就会生成一个小写的 video文件夹里面只有 本地1,本地2 两个的
同时 大写的 Video 里面 会集合三个文件夹的文件
2

@anwen-anyi
Copy link
Contributor

@anwen-anyi anwen-anyi commented on d9795ff Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 第一个问题在 上个版本(第一次提交)的时候似乎只是看alias这个存储是怎么设置的
    • 如果alias 在添加的时候未勾选Web代理(302模式),那么302的都能播放,本地代理的不能播放(代理URL没测试估计一样)
    • 如果alias 在添加的时候勾选Web代理(本地代理模式),那么这个文件夹内 本地代理和302的都可以播放了

这次提交看不懂是根据什么来了QAQ

  1. 第二个问题在 上个版本(第一次提交)的时候就存在,当时没注意

@xhofe
Copy link
Collaborator Author

@xhofe xhofe commented on d9795ff Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 忘记link时判断存在了
  2. 这是因为Windows 大小写不敏感,video和Video会被认为是同一个文件夹。你换成Linux或者Mac就不会有这个问题了

Please sign in to comment.