enhance(download): Use just-in-time host in download URl, instead of SiteURL in site settings

This commit is contained in:
Aaron Liu
2023-05-25 19:49:32 +08:00
parent 4c834e75fa
commit 4aafe1dc7a
16 changed files with 36 additions and 137 deletions

View File

@@ -124,7 +124,7 @@ func (handler *Driver) Get(ctx context.Context, path string) (response.RSCloser,
}
// 获取文件源地址
downloadURL, err := handler.Source(ctx, path, url.URL{}, 0, true, speedLimit)
downloadURL, err := handler.Source(ctx, path, 0, true, speedLimit)
if err != nil {
return nil, err
}
@@ -233,14 +233,7 @@ func (handler *Driver) Thumb(ctx context.Context, file *model.File) (*response.C
}
// Source 获取外链URL
func (handler *Driver) Source(
ctx context.Context,
path string,
baseURL url.URL,
ttl int64,
isDownload bool,
speed int,
) (string, error) {
func (handler *Driver) Source(ctx context.Context, path string, ttl int64, isDownload bool, speed int) (string, error) {
// 尝试从上下文获取文件名
fileName := "file"
if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {

View File

@@ -9,7 +9,6 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
@@ -51,7 +50,7 @@ func TestHandler_Source(t *testing.T) {
AuthInstance: auth.HMACAuth{},
}
ctx := context.Background()
res, err := handler.Source(ctx, "", url.URL{}, 0, true, 0)
res, err := handler.Source(ctx, "", 0, true, 0)
asserts.NoError(err)
asserts.NotEmpty(res)
}
@@ -66,7 +65,7 @@ func TestHandler_Source(t *testing.T) {
SourceName: "1.txt",
}
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
res, err := handler.Source(ctx, "", url.URL{}, 10, true, 0)
res, err := handler.Source(ctx, "", 10, true, 0)
asserts.NoError(err)
asserts.Contains(res, "api/v3/slave/download/0")
}
@@ -81,7 +80,7 @@ func TestHandler_Source(t *testing.T) {
SourceName: "1.txt",
}
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
res, err := handler.Source(ctx, "", url.URL{}, 10, true, 0)
res, err := handler.Source(ctx, "", 10, true, 0)
asserts.NoError(err)
asserts.Contains(res, "api/v3/slave/download/0")
asserts.Contains(res, "https://cqu.edu.cn")
@@ -97,7 +96,7 @@ func TestHandler_Source(t *testing.T) {
SourceName: "1.txt",
}
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
res, err := handler.Source(ctx, "", url.URL{}, 10, true, 0)
res, err := handler.Source(ctx, "", 10, true, 0)
asserts.Error(err)
asserts.Empty(res)
}
@@ -112,7 +111,7 @@ func TestHandler_Source(t *testing.T) {
SourceName: "1.txt",
}
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
res, err := handler.Source(ctx, "", url.URL{}, 10, false, 0)
res, err := handler.Source(ctx, "", 10, false, 0)
asserts.NoError(err)
asserts.Contains(res, "api/v3/slave/source/0")
}