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(filesystem): Support sub-directories for cdn/proxy domain #2116

Open
wants to merge 4 commits into
base: master
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release --clean --skip-validate
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
62 changes: 0 additions & 62 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ env:
before:
hooks:
- go mod tidy
- sh -c "cd assets && rm -rf build && yarn install --network-timeout 1000000 && yarn run build && cd ../ && zip -r - assets/build >assets.zip"
builds:
-
env:
Expand Down Expand Up @@ -58,64 +57,3 @@ changelog:
exclude:
- '^docs:'
- '^test:'

release:
draft: true
prerelease: auto
target_commitish: '{{ .Commit }}'
name_template: "{{.Version}}"

dockers:
-
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
goos: linux
goarch: amd64
goamd64: v1
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-amd64"
-
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/arm64"
goos: linux
goarch: arm64
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-arm64"
-
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/arm/v6"
goos: linux
goarch: arm
goarm: '6'
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-armv6"
-
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--platform=linux/arm/v7"
goos: linux
goarch: arm
goarm: '7'
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-armv7"

docker_manifests:
- name_template: "cloudreve/cloudreve:latest"
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-amd64"
- "cloudreve/cloudreve:{{ .Tag }}-arm64"
- "cloudreve/cloudreve:{{ .Tag }}-armv6"
- "cloudreve/cloudreve:{{ .Tag }}-armv7"
- name_template: "cloudreve/cloudreve:{{ .Tag }}"
image_templates:
- "cloudreve/cloudreve:{{ .Tag }}-amd64"
- "cloudreve/cloudreve:{{ .Tag }}-arm64"
- "cloudreve/cloudreve:{{ .Tag }}-armv6"
- "cloudreve/cloudreve:{{ .Tag }}-armv7"
Binary file modified assets.zip
Binary file not shown.
22 changes: 13 additions & 9 deletions pkg/filesystem/driver/cos/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ func (handler Driver) CORS() error {
}

// Get 获取文件
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
func (handler Driver) Get(ctx context.Context, objectPath string) (response.RSCloser, error) {
// 获取文件源地址
downloadURL, err := handler.Source(ctx, path, int64(model.GetIntSetting("preview_timeout", 60)), false, 0)
downloadURL, err := handler.Source(ctx, objectPath, int64(model.GetIntSetting("preview_timeout", 60)), false, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co
}

// Source 获取外链URL
func (handler Driver) Source(ctx context.Context, path string, ttl int64, isDownload bool, speed int) (string, error) {
func (handler Driver) Source(ctx context.Context, objectPath string, ttl int64, isDownload bool, speed int) (string, error) {
// 尝试从上下文获取文件名
fileName := ""
if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {
Expand All @@ -285,18 +285,18 @@ func (handler Driver) Source(ctx context.Context, path string, ttl int64, isDown
options.ContentDescription = "attachment; filename=\"" + url.PathEscape(fileName) + "\""
}

return handler.signSourceURL(ctx, path, ttl, &options)
return handler.signSourceURL(ctx, objectPath, ttl, &options)
}

func (handler Driver) signSourceURL(ctx context.Context, path string, ttl int64, options *urlOption) (string, error) {
func (handler Driver) signSourceURL(ctx context.Context, objectPath string, ttl int64, options *urlOption) (string, error) {
cdnURL, err := url.Parse(handler.Policy.BaseURL)
if err != nil {
return "", err
}

// 公有空间不需要签名
if !handler.Policy.IsPrivate {
file, err := url.Parse(path)
file, err := url.Parse(objectPath)
if err != nil {
return "", err
}
Expand All @@ -314,7 +314,7 @@ func (handler Driver) signSourceURL(ctx context.Context, path string, ttl int64,
return sourceURL.String(), nil
}

presignedURL, err := handler.Client.Object.GetPresignedURL(ctx, http.MethodGet, path,
presignedURL, err := handler.Client.Object.GetPresignedURL(ctx, http.MethodGet, objectPath,
handler.Policy.AccessKey, handler.Policy.SecretKey, time.Duration(ttl)*time.Second, options)
if err != nil {
return "", err
Expand All @@ -324,6 +324,10 @@ func (handler Driver) signSourceURL(ctx context.Context, path string, ttl int64,
presignedURL.Host = cdnURL.Host
presignedURL.Scheme = cdnURL.Scheme

// 支持代理域名使用子目录
// Support sub-directories for proxy domain
presignedURL.Path = path.Join(cdnURL.Path, presignedURL.Path)

return presignedURL.String(), nil
}

Expand Down Expand Up @@ -374,8 +378,8 @@ func (handler Driver) CancelToken(ctx context.Context, uploadSession *serializer
}

// Meta 获取文件信息
func (handler Driver) Meta(ctx context.Context, path string) (*MetaData, error) {
res, err := handler.Client.Object.Head(ctx, path, &cossdk.ObjectHeadOptions{})
func (handler Driver) Meta(ctx context.Context, objectPath string) (*MetaData, error) {
res, err := handler.Client.Object.Head(ctx, objectPath, &cossdk.ObjectHeadOptions{})
if err != nil {
return nil, err
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/filesystem/driver/local/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/url"
"os"
"path"
"path/filepath"

model "github.com/cloudreve/Cloudreve/v3/models"
Expand Down Expand Up @@ -76,9 +77,9 @@ func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]
}

// Get 获取文件内容
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
func (handler Driver) Get(ctx context.Context, objectPath string) (response.RSCloser, error) {
// 打开文件
file, err := os.Open(util.RelativePath(path))
file, err := os.Open(util.RelativePath(objectPath))
if err != nil {
util.Log().Debug("Failed to open file: %s", err)
return nil, err
Expand Down Expand Up @@ -219,7 +220,7 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co
}

// Source 获取外链URL
func (handler Driver) Source(ctx context.Context, path string, ttl int64, isDownload bool, speed int) (string, error) {
func (handler Driver) Source(ctx context.Context, objectPath string, ttl int64, isDownload bool, speed int) (string, error) {
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
if !ok {
return "", errors.New("failed to read file model context")
Expand Down Expand Up @@ -268,7 +269,11 @@ func (handler Driver) Source(ctx context.Context, path string, ttl int64, isDown

finalURL := signedURI.String()
if baseURL != nil {
finalURL = baseURL.ResolveReference(signedURI).String()
// 支持代理域名使用子目录
// Support sub-directories for proxy domain
baseURL.Path = path.Join(baseURL.Path, signedURI.Path)
baseURL.RawQuery = signedURI.RawQuery
finalURL = baseURL.String()
}

return finalURL, nil
Expand Down
15 changes: 10 additions & 5 deletions pkg/filesystem/driver/onedrive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func (handler Driver) List(ctx context.Context, base string, recursive bool) ([]
}

// Get 获取文件
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
func (handler Driver) Get(ctx context.Context, objectPath string) (response.RSCloser, error) {
// 获取文件源地址
downloadURL, err := handler.Source(
ctx,
path,
objectPath,
60,
false,
0,
Expand Down Expand Up @@ -162,12 +162,12 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co
// Source 获取外链URL
func (handler Driver) Source(
ctx context.Context,
path string,
objectPath string,
ttl int64,
isDownload bool,
speed int,
) (string, error) {
cacheKey := fmt.Sprintf("onedrive_source_%d_%s", handler.Policy.ID, path)
cacheKey := fmt.Sprintf("onedrive_source_%d_%s", handler.Policy.ID, objectPath)
if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {
cacheKey = fmt.Sprintf("onedrive_source_file_%d_%d", file.UpdatedAt.Unix(), file.ID)
}
Expand All @@ -178,7 +178,7 @@ func (handler Driver) Source(
}

// 缓存不存在,重新获取
res, err := handler.Client.Meta(ctx, "", path)
res, err := handler.Client.Meta(ctx, "", objectPath)
if err == nil {
// 写入新的缓存
cache.Set(
Expand Down Expand Up @@ -206,6 +206,11 @@ func (handler Driver) replaceSourceHost(origin string) (string, error) {
// 替换反代地址
source.Scheme = cdn.Scheme
source.Host = cdn.Host

// 支持代理域名使用子目录
// Support sub-directories for proxy domain
source.Path = path.Join(cdn.Path, source.Path)

return source.String(), nil
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/filesystem/driver/oss/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ func (handler *Driver) List(ctx context.Context, base string, recursive bool) ([
}

// Get 获取文件
func (handler *Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
func (handler *Driver) Get(ctx context.Context, objectPath string) (response.RSCloser, error) {
// 通过VersionID禁止缓存
ctx = context.WithValue(ctx, VersionID, time.Now().UnixNano())

// 尽可能使用私有 Endpoint
ctx = context.WithValue(ctx, fsctx.ForceUsePublicEndpointCtx, false)

// 获取文件源地址
downloadURL, err := handler.Source(ctx, path, int64(model.GetIntSetting("preview_timeout", 60)), false, 0)
downloadURL, err := handler.Source(ctx, objectPath, int64(model.GetIntSetting("preview_timeout", 60)), false, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -334,7 +334,7 @@ func (handler *Driver) Thumb(ctx context.Context, file *model.File) (*response.C
}

// Source 获取外链URL
func (handler *Driver) Source(ctx context.Context, path string, ttl int64, isDownload bool, speed int) (string, error) {
func (handler *Driver) Source(ctx context.Context, objectPath string, ttl int64, isDownload bool, speed int) (string, error) {
// 初始化客户端
usePublicEndpoint := true
if forceUsePublicEndpoint, ok := ctx.Value(fsctx.ForceUsePublicEndpointCtx).(bool); ok {
Expand Down Expand Up @@ -369,11 +369,11 @@ func (handler *Driver) Source(ctx context.Context, path string, ttl int64, isDow
signOptions = append(signOptions, oss.TrafficLimitParam(int64(speed)))
}

return handler.signSourceURL(ctx, path, ttl, signOptions)
return handler.signSourceURL(ctx, objectPath, ttl, signOptions)
}

func (handler *Driver) signSourceURL(ctx context.Context, path string, ttl int64, options []oss.Option) (string, error) {
signedURL, err := handler.bucket.SignURL(path, oss.HTTPGet, ttl, options...)
func (handler *Driver) signSourceURL(ctx context.Context, objectPath string, ttl int64, options []oss.Option) (string, error) {
signedURL, err := handler.bucket.SignURL(objectPath, oss.HTTPGet, ttl, options...)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -401,6 +401,10 @@ func (handler *Driver) signSourceURL(ctx context.Context, path string, ttl int64
}
finalURL.Host = cdnURL.Host
finalURL.Scheme = cdnURL.Scheme

// 支持代理域名使用子目录
// Support sub-directories for proxy domain
finalURL.Path = path.Join(cdnURL.Path, finalURL.Path)
}

return finalURL.String(), nil
Expand Down
15 changes: 9 additions & 6 deletions pkg/filesystem/driver/remote/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func (handler *Driver) getAPIUrl(scope string, routes ...string) string {
}

// Get 获取文件内容
func (handler *Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
func (handler *Driver) Get(ctx context.Context, objectPath string) (response.RSCloser, error) {
// 尝试获取速度限制
speedLimit := 0
if user, ok := ctx.Value(fsctx.UserCtx).(model.User); ok {
speedLimit = user.Group.SpeedLimit
}

// 获取文件源地址
downloadURL, err := handler.Source(ctx, path, 0, true, speedLimit)
downloadURL, err := handler.Source(ctx, objectPath, 0, true, speedLimit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -233,7 +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, ttl int64, isDownload bool, speed int) (string, error) {
func (handler *Driver) Source(ctx context.Context, objectPath string, ttl int64, isDownload bool, speed int) (string, error) {
// 尝试从上下文获取文件名
fileName := "file"
if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {
Expand Down Expand Up @@ -263,7 +263,7 @@ func (handler *Driver) Source(ctx context.Context, path string, ttl int64, isDow
}

// 签名下载地址
sourcePath := base64.RawURLEncoding.EncodeToString([]byte(path))
sourcePath := base64.RawURLEncoding.EncodeToString([]byte(objectPath))
signedURI, err = auth.SignURI(
handler.AuthInstance,
fmt.Sprintf("%s/%d/%s/%s", controller, speed, sourcePath, url.PathEscape(fileName)),
Expand All @@ -274,9 +274,12 @@ func (handler *Driver) Source(ctx context.Context, path string, ttl int64, isDow
return "", serializer.NewError(serializer.CodeEncryptError, "Failed to sign URL", err)
}

finalURL := serverURL.ResolveReference(signedURI).String()
return finalURL, nil
// 支持代理域名使用子目录
// Support sub-directories for proxy domain
serverURL.Path = path.Join(serverURL.Path, signedURI.Path)
serverURL.RawQuery = signedURI.RawQuery

return serverURL.String(), nil
}

// Token 获取上传策略和认证Token
Expand Down
Loading