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

fix(crypt): add sign to thumbnail #6611

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions drivers/alias/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs)
if common.ShouldProxy(storage, stdpath.Base(sub)) {
link := &model.Link{
URL: fmt.Sprintf("%s/p%s?sign=%s",
common.GetApiUrl(args.HttpReq),
common.GetApiUrlFromContext(ctx),
utils.EncodePath(reqPath, true),
sign.Sign(reqPath)),
}
if args.HttpReq != nil && d.ProxyRange {
if d.ProxyRange {
link.RangeReadCloser = common.NoProxyRange
}
return link, nil
Expand Down
7 changes: 6 additions & 1 deletion drivers/crypt/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/sign"
"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"
Expand Down Expand Up @@ -160,7 +161,11 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
// discarding hash as it's encrypted
}
if d.Thumbnail && thumb == "" {
thumb = utils.EncodePath(common.GetApiUrl(nil)+stdpath.Join("/d", args.ReqPath, ".thumbnails", name+".webp"), true)
thumbPath := stdpath.Join(args.ReqPath, ".thumbnails", name+".webp")
thumb = fmt.Sprintf("%s/d%s?sign=%s",
common.GetApiUrlFromContext(ctx),
utils.EncodePath(thumbPath, true),
sign.Sign(thumbPath))
}
if !ok && !d.Thumbnail {
result = append(result, &objRes)
Expand Down
8 changes: 4 additions & 4 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
if !d.ShowHidden && strings.HasPrefix(f.Name(), ".") {
continue
}
file := d.FileInfoToObj(f, args.ReqPath, fullPath)
file := d.FileInfoToObj(ctx, f, args.ReqPath, fullPath)
files = append(files, file)
}
return files, nil
}
func (d *Local) FileInfoToObj(f fs.FileInfo, reqPath string, fullPath string) model.Obj {
func (d *Local) FileInfoToObj(ctx context.Context, f fs.FileInfo, reqPath string, fullPath string) model.Obj {
thumb := ""
if d.Thumbnail {
typeName := utils.GetFileType(f.Name())
if typeName == conf.IMAGE || typeName == conf.VIDEO {
thumb = common.GetApiUrl(nil) + stdpath.Join("/d", reqPath, f.Name())
thumb = common.GetApiUrlFromContext(ctx) + stdpath.Join("/d", reqPath, f.Name())
thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb&sign=" + sign.Sign(stdpath.Join(reqPath, f.Name()))
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func (d *Local) GetMeta(ctx context.Context, path string) (model.Obj, error) {
if err != nil {
return nil, err
}
file := d.FileInfoToObj(f, path, path)
file := d.FileInfoToObj(ctx, f, path, path)
//h := "123123"
//if s, ok := f.(model.SetHash); ok && file.GetHash() == ("","") {
// s.SetHash(h,"SHA1")
Expand Down
2 changes: 1 addition & 1 deletion drivers/netease_music/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (d *NeteaseMusic) Link(ctx context.Context, file model.Obj, args model.Link
if args.Type == "parsed" {
return lrc.getLyricLink(), nil
} else {
return lrc.getProxyLink(args), nil
return lrc.getProxyLink(ctx), nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/netease_music/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type LyricObj struct {
lyric string
}

func (lrc *LyricObj) getProxyLink(args model.LinkArgs) *model.Link {
rawURL := common.GetApiUrl(args.HttpReq) + "/p" + lrc.Path
func (lrc *LyricObj) getProxyLink(ctx context.Context) *model.Link {
rawURL := common.GetApiUrlFromContext(ctx) + "/p" + lrc.Path
rawURL = utils.EncodePath(rawURL, true) + "?type=parsed&sign=" + sign.Sign(lrc.Path)
return &model.Link{URL: rawURL}
}
Expand Down
7 changes: 3 additions & 4 deletions internal/model/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ type ListArgs struct {
}

type LinkArgs struct {
IP string
Header http.Header
Type string
HttpReq *http.Request
IP string
Header http.Header
Type string
}

type Link struct {
Expand Down
15 changes: 12 additions & 3 deletions server/common/base.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package common

import (
"context"
"fmt"
"net/http"
stdpath "path"
"strings"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/gin-gonic/gin"
)

func GetApiUrl(r *http.Request) string {
Expand All @@ -19,12 +21,19 @@ func GetApiUrl(r *http.Request) string {
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
protocol = "https"
}
host := r.Host
if r.Header.Get("X-Forwarded-Host") != "" {
host = r.Header.Get("X-Forwarded-Host")
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
api = fmt.Sprintf("%s://%s", protocol, stdpath.Join(host, api))
}
api = strings.TrimSuffix(api, "/")
return api
}

func GetApiUrlFromContext(ctx context.Context) string {
if c, ok := ctx.(*gin.Context); ok {
return GetApiUrl(c.Request)
}
return GetApiUrl(nil)
}
12 changes: 5 additions & 7 deletions server/handles/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ func Down(c *gin.Context) {
return
} else {
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
Type: c.Query("type"),
HttpReq: c.Request,
IP: c.ClientIP(),
Header: c.Request.Header,
Type: c.Query("type"),
})
if err != nil {
common.ErrorResp(c, err, 500)
Expand Down Expand Up @@ -87,9 +86,8 @@ func Proxy(c *gin.Context) {
}
}
link, file, err := fs.Link(c, rawPath, model.LinkArgs{
Header: c.Request.Header,
Type: c.Query("type"),
HttpReq: c.Request,
Header: c.Request.Header,
Type: c.Query("type"),
})
if err != nil {
common.ErrorResp(c, err, 500)
Expand Down
5 changes: 3 additions & 2 deletions server/handles/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package handles

import (
"fmt"
"github.com/xhofe/tache"
"io"
stdpath "path"

"github.com/xhofe/tache"

"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
Expand Down Expand Up @@ -325,7 +326,7 @@ func Link(c *gin.Context) {
})
return
}
link, _, err := fs.Link(c, rawPath, model.LinkArgs{IP: c.ClientIP(), Header: c.Request.Header, HttpReq: c.Request})
link, _, err := fs.Link(c, rawPath, model.LinkArgs{IP: c.ClientIP(), Header: c.Request.Header})
if err != nil {
common.ErrorResp(c, err, 500)
return
Expand Down
5 changes: 2 additions & 3 deletions server/handles/fsread.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ func FsGet(c *gin.Context) {
} else {
// if storage is not proxy, use raw url by fs.Link
link, _, err := fs.Link(c, reqPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
HttpReq: c.Request,
IP: c.ClientIP(),
Header: c.Request.Header,
})
if err != nil {
common.ErrorResp(c, err, 500)
Expand Down
4 changes: 2 additions & 2 deletions server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta
storage, _ := fs.GetStorage(reqPath, &fs.GetStoragesArgs{})
downProxyUrl := storage.GetStorage().DownProxyUrl
if storage.GetStorage().WebdavNative() || (storage.GetStorage().WebdavProxy() && downProxyUrl == "") {
link, _, err := fs.Link(ctx, reqPath, model.LinkArgs{Header: r.Header, HttpReq: r})
link, _, err := fs.Link(ctx, reqPath, model.LinkArgs{Header: r.Header})
if err != nil {
return http.StatusInternalServerError, err
}
Expand All @@ -263,7 +263,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta
w.Header().Set("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
http.Redirect(w, r, u, http.StatusFound)
} else {
link, _, err := fs.Link(ctx, reqPath, model.LinkArgs{IP: utils.ClientIP(r), Header: r.Header, HttpReq: r})
link, _, err := fs.Link(ctx, reqPath, model.LinkArgs{IP: utils.ClientIP(r), Header: r.Header})
if err != nil {
return http.StatusInternalServerError, err
}
Expand Down
Loading