Skip to content

Commit 103abc9

Browse files
authored
refactor: pass api_url through context (#457)
* refactor: pass `api_url` through context * 移除 LinkArgs.HttpReq * pref(alias): 减少不必要下载代理 * 修复bug * net: 支持1并发 分片下载
1 parent f023652 commit 103abc9

File tree

30 files changed

+209
-222
lines changed

30 files changed

+209
-222
lines changed

drivers/alias/util.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs)
103103
if err != nil {
104104
return nil, err
105105
}
106-
if _, ok := storage.(*Alias); !ok && !args.Redirect {
106+
useRawLink := len(common.GetApiUrl(ctx)) == 0 // ftp、s3
107+
if !useRawLink {
108+
_, ok := storage.(*Alias)
109+
useRawLink = !ok && !args.Redirect
110+
}
111+
if useRawLink {
107112
link, _, err := op.Link(ctx, storage, reqActualPath, args)
108113
return link, err
109114
}
@@ -114,13 +119,10 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs)
114119
if common.ShouldProxy(storage, stdpath.Base(sub)) {
115120
link := &model.Link{
116121
URL: fmt.Sprintf("%s/p%s?sign=%s",
117-
common.GetApiUrl(args.HttpReq),
122+
common.GetApiUrl(ctx),
118123
utils.EncodePath(reqPath, true),
119124
sign.Sign(reqPath)),
120125
}
121-
if args.HttpReq != nil && d.ProxyRange {
122-
link.RangeReadCloser = common.NoProxyRange
123-
}
124126
return link, nil
125127
}
126128
link, _, err := op.Link(ctx, storage, reqActualPath, args)
@@ -201,31 +203,24 @@ func (d *Alias) extract(ctx context.Context, dst, sub string, args model.Archive
201203
if err != nil {
202204
return nil, err
203205
}
204-
if _, ok := storage.(driver.ArchiveReader); ok {
205-
if _, ok := storage.(*Alias); !ok && !args.Redirect {
206-
link, _, err := op.DriverExtract(ctx, storage, reqActualPath, args)
207-
return link, err
208-
}
206+
if _, ok := storage.(driver.ArchiveReader); !ok {
207+
return nil, errs.NotImplement
208+
}
209+
if args.Redirect && common.ShouldProxy(storage, stdpath.Base(sub)) {
209210
_, err = fs.Get(ctx, reqPath, &fs.GetArgs{NoLog: true})
210211
if err != nil {
211212
return nil, err
212213
}
213-
if common.ShouldProxy(storage, stdpath.Base(sub)) {
214-
link := &model.Link{
215-
URL: fmt.Sprintf("%s/ap%s?inner=%s&pass=%s&sign=%s",
216-
common.GetApiUrl(args.HttpReq),
217-
utils.EncodePath(reqPath, true),
218-
utils.EncodePath(args.InnerPath, true),
219-
url.QueryEscape(args.Password),
220-
sign.SignArchive(reqPath)),
221-
}
222-
if args.HttpReq != nil && d.ProxyRange {
223-
link.RangeReadCloser = common.NoProxyRange
224-
}
225-
return link, nil
214+
link := &model.Link{
215+
URL: fmt.Sprintf("%s/ap%s?inner=%s&pass=%s&sign=%s",
216+
common.GetApiUrl(ctx),
217+
utils.EncodePath(reqPath, true),
218+
utils.EncodePath(args.InnerPath, true),
219+
url.QueryEscape(args.Password),
220+
sign.SignArchive(reqPath)),
226221
}
227-
link, _, err := op.DriverExtract(ctx, storage, reqActualPath, args)
228-
return link, err
222+
return link, nil
229223
}
230-
return nil, errs.NotImplement
224+
link, _, err := op.DriverExtract(ctx, storage, reqActualPath, args)
225+
return link, err
231226
}

drivers/crypt/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
163163
if d.Thumbnail && thumb == "" {
164164
thumbPath := stdpath.Join(args.ReqPath, ".thumbnails", name+".webp")
165165
thumb = fmt.Sprintf("%s/d%s?sign=%s",
166-
common.GetApiUrl(common.GetHttpReq(ctx)),
166+
common.GetApiUrl(ctx),
167167
utils.EncodePath(thumbPath, true),
168168
sign.Sign(thumbPath))
169169
}

drivers/local/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (d *Local) FileInfoToObj(ctx context.Context, f fs.FileInfo, reqPath string
139139
if d.Thumbnail {
140140
typeName := utils.GetFileType(f.Name())
141141
if typeName == conf.IMAGE || typeName == conf.VIDEO {
142-
thumb = common.GetApiUrl(common.GetHttpReq(ctx)) + stdpath.Join("/d", reqPath, f.Name())
142+
thumb = common.GetApiUrl(ctx) + stdpath.Join("/d", reqPath, f.Name())
143143
thumb = utils.EncodePath(thumb, true)
144144
thumb += "?type=thumb&sign=" + sign.Sign(stdpath.Join(reqPath, f.Name()))
145145
}

drivers/netease_music/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (d *NeteaseMusic) Link(ctx context.Context, file model.Obj, args model.Link
7676
if args.Type == "parsed" {
7777
return lrc.getLyricLink(), nil
7878
} else {
79-
return lrc.getProxyLink(args), nil
79+
return lrc.getProxyLink(ctx), nil
8080
}
8181
}
8282

drivers/netease_music/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type LyricObj struct {
4848
lyric string
4949
}
5050

51-
func (lrc *LyricObj) getProxyLink(args model.LinkArgs) *model.Link {
52-
rawURL := common.GetApiUrl(args.HttpReq) + "/p" + lrc.Path
51+
func (lrc *LyricObj) getProxyLink(ctx context.Context) *model.Link {
52+
rawURL := common.GetApiUrl(ctx) + "/p" + lrc.Path
5353
rawURL = utils.EncodePath(rawURL, true) + "?type=parsed&sign=" + sign.Sign(lrc.Path)
5454
return &model.Link{URL: rawURL}
5555
}

internal/authn/authn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package authn
22

33
import (
44
"fmt"
5-
"net/http"
65
"net/url"
76

87
"github.com/OpenListTeam/OpenList/internal/conf"
98
"github.com/OpenListTeam/OpenList/internal/setting"
109
"github.com/OpenListTeam/OpenList/server/common"
10+
"github.com/gin-gonic/gin"
1111
"github.com/go-webauthn/webauthn/webauthn"
1212
)
1313

14-
func NewAuthnInstance(r *http.Request) (*webauthn.WebAuthn, error) {
15-
siteUrl, err := url.Parse(common.GetApiUrl(r))
14+
func NewAuthnInstance(c *gin.Context) (*webauthn.WebAuthn, error) {
15+
siteUrl, err := url.Parse(common.GetApiUrl(c))
1616
if err != nil {
1717
return nil, err
1818
}

internal/conf/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ const (
148148
// ContextKey is the type of context keys.
149149
const (
150150
NoTaskKey = "no_task"
151+
ApiUrlKey = "api_url"
151152
)

internal/fs/archive.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ func (t *ArchiveDownloadTask) GetStatus() string {
4949
}
5050

5151
func (t *ArchiveDownloadTask) Run() error {
52-
t.ReinitCtx()
52+
if err := t.ReinitCtx(); err != nil {
53+
return err
54+
}
5355
t.ClearEndTime()
5456
t.SetStartTime(time.Now())
5557
defer func() { t.SetEndTime(time.Now()) }()
@@ -152,7 +154,9 @@ func (t *ArchiveContentUploadTask) GetStatus() string {
152154
}
153155

154156
func (t *ArchiveContentUploadTask) Run() error {
155-
t.ReinitCtx()
157+
if err := t.ReinitCtx(); err != nil {
158+
return err
159+
}
156160
t.ClearEndTime()
157161
t.SetStartTime(time.Now())
158162
defer func() { t.SetEndTime(time.Now()) }()

internal/fs/copy.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
stdpath "path"
88
"time"
99

10-
"github.com/OpenListTeam/OpenList/internal/errs"
11-
1210
"github.com/OpenListTeam/OpenList/internal/conf"
1311
"github.com/OpenListTeam/OpenList/internal/driver"
12+
"github.com/OpenListTeam/OpenList/internal/errs"
1413
"github.com/OpenListTeam/OpenList/internal/model"
1514
"github.com/OpenListTeam/OpenList/internal/op"
1615
"github.com/OpenListTeam/OpenList/internal/stream"
1716
"github.com/OpenListTeam/OpenList/internal/task"
1817
"github.com/OpenListTeam/OpenList/pkg/utils"
18+
"github.com/OpenListTeam/OpenList/server/common"
1919
"github.com/pkg/errors"
2020
"github.com/xhofe/tache"
2121
)
@@ -40,7 +40,9 @@ func (t *CopyTask) GetStatus() string {
4040
}
4141

4242
func (t *CopyTask) Run() error {
43-
t.ReinitCtx()
43+
if err := t.ReinitCtx(); err != nil {
44+
return err
45+
}
4446
t.ClearEndTime()
4547
t.SetStartTime(time.Now())
4648
defer func() { t.SetEndTime(time.Now()) }()
@@ -107,6 +109,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
107109
t := &CopyTask{
108110
TaskExtension: task.TaskExtension{
109111
Creator: taskCreator,
112+
ApiUrl: common.GetApiUrl(ctx),
110113
},
111114
srcStorage: srcStorage,
112115
dstStorage: dstStorage,
@@ -140,6 +143,7 @@ func copyBetween2Storages(t *CopyTask, srcStorage, dstStorage driver.Driver, src
140143
CopyTaskManager.Add(&CopyTask{
141144
TaskExtension: task.TaskExtension{
142145
Creator: t.GetCreator(),
146+
ApiUrl: t.ApiUrl,
143147
},
144148
srcStorage: srcStorage,
145149
dstStorage: dstStorage,

internal/fs/link.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/OpenListTeam/OpenList/internal/model"
88
"github.com/OpenListTeam/OpenList/internal/op"
99
"github.com/OpenListTeam/OpenList/server/common"
10-
"github.com/gin-gonic/gin"
1110
"github.com/pkg/errors"
1211
)
1312

@@ -21,9 +20,7 @@ func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, m
2120
return nil, nil, errors.WithMessage(err, "failed link")
2221
}
2322
if l.URL != "" && !strings.HasPrefix(l.URL, "http://") && !strings.HasPrefix(l.URL, "https://") {
24-
if c, ok := ctx.(*gin.Context); ok {
25-
l.URL = common.GetApiUrl(c.Request) + l.URL
26-
}
23+
l.URL = common.GetApiUrl(ctx) + l.URL
2724
}
2825
return l, obj, nil
2926
}

0 commit comments

Comments
 (0)