From 6b6bfb4c6b6fdef330d7a6fb00574c35d070bf5c Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Mon, 13 Jan 2020 09:47:04 +0800 Subject: [PATCH] Modify: FileName header in remote put request should be urlencoded --- models/policy.go | 10 +++++----- pkg/filesystem/remote/handler.go | 7 ++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/models/policy.go b/models/policy.go index fda84a9..a759fcc 100644 --- a/models/policy.go +++ b/models/policy.go @@ -67,11 +67,6 @@ func GetPolicyByID(ID interface{}) (Policy, error) { return policy, result.Error } -// IsDirectlyPreview 返回此策略下文件是否可以直接预览(不需要重定向) -func (policy *Policy) IsDirectlyPreview() bool { - return policy.Type == "local" -} - // AfterFind 找到存储策略后的钩子 func (policy *Policy) AfterFind() (err error) { // 解析存储策略设置到OptionsSerialized @@ -147,3 +142,8 @@ func (policy *Policy) GenerateFileName(uid uint, origin string) string { fileRule = util.Replace(replaceTable, fileRule) return fileRule } + +// IsDirectlyPreview 返回此策略下文件是否可以直接预览(不需要重定向) +func (policy *Policy) IsDirectlyPreview() bool { + return policy.Type == "local" +} diff --git a/pkg/filesystem/remote/handler.go b/pkg/filesystem/remote/handler.go index cb6920f..a3320f1 100644 --- a/pkg/filesystem/remote/handler.go +++ b/pkg/filesystem/remote/handler.go @@ -101,6 +101,11 @@ func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, return err } + // 对文件名进行URLEncode + fileName, err := url.QueryUnescape(path.Base(dst)) + if err != nil { + return err + } // 上传文件 resp, err := handler.Client.Request( "POST", @@ -109,7 +114,7 @@ func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, request.WithHeader(map[string][]string{ "Authorization": {credential.Token}, "X-Policy": {credential.Policy}, - "X-FileName": {path.Base(dst)}, + "X-FileName": {fileName}, }), request.WithContentLength(int64(size)), ).CheckHTTPResponse(200).DecodeResponse()