From c23d129dbb6d50dfc2b61581ad277239ee445dfd Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 4 Jan 2020 16:21:43 +0800 Subject: [PATCH] Modify: use path package in app level --- models/policy.go | 8 ++++---- pkg/filesystem/local/handler.go | 3 ++- pkg/filesystem/remote/handler.go | 23 ++++++++++++++++------- pkg/filesystem/remote/handler_test.go | 2 ++ pkg/filesystem/upload.go | 6 +++--- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/models/policy.go b/models/policy.go index 5a4ec78..fda84a9 100644 --- a/models/policy.go +++ b/models/policy.go @@ -6,7 +6,7 @@ import ( "github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/util" "github.com/jinzhu/gorm" - "path/filepath" + "path" "strconv" "time" ) @@ -96,7 +96,7 @@ func (policy *Policy) SerializeOptions() (err error) { } // GeneratePath 生成存储文件的路径 -func (policy *Policy) GeneratePath(uid uint, path string) string { +func (policy *Policy) GeneratePath(uid uint, origin string) string { dirRule := policy.DirNameRule replaceTable := map[string]string{ "{randomkey16}": util.RandStringRunes(16), @@ -105,10 +105,10 @@ func (policy *Policy) GeneratePath(uid uint, path string) string { "{uid}": strconv.Itoa(int(uid)), "{datetime}": time.Now().Format("20060102150405"), "{date}": time.Now().Format("20060102"), - "{path}": path + "/", + "{path}": origin + "/", } dirRule = util.Replace(replaceTable, dirRule) - return filepath.Clean(dirRule) + return path.Clean(dirRule) } // GenerateFileName 生成存储文件名 diff --git a/pkg/filesystem/local/handler.go b/pkg/filesystem/local/handler.go index 426ef9a..5b06a4f 100644 --- a/pkg/filesystem/local/handler.go +++ b/pkg/filesystem/local/handler.go @@ -51,6 +51,7 @@ func closeReader(ctx context.Context, closer io.Closer) { // Put 将文件流保存到指定目录 func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error { defer file.Close() + dst = filepath.FromSlash(dst) // 如果目标目录不存在,创建 basePath := filepath.Dir(dst) @@ -82,7 +83,7 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er var retErr error for _, value := range files { - err := os.Remove(value) + err := os.Remove(filepath.FromSlash(value)) if err != nil { util.Log().Warning("无法删除文件,%s", err) retErr = err diff --git a/pkg/filesystem/remote/handler.go b/pkg/filesystem/remote/handler.go index e703c87..281c295 100644 --- a/pkg/filesystem/remote/handler.go +++ b/pkg/filesystem/remote/handler.go @@ -1,6 +1,5 @@ package remote -// TODO 测试 import ( "context" "encoding/base64" @@ -16,6 +15,7 @@ import ( "io" "net/http" "net/url" + "path" "strings" ) @@ -27,7 +27,7 @@ type Handler struct { } // getAPIUrl 获取接口请求地址 -func (handler Handler) getAPIUrl(scope string) string { +func (handler Handler) getAPIUrl(scope string, routes ...string) string { serverURL, err := url.Parse(handler.Policy.Server) if err != nil { return "" @@ -39,6 +39,14 @@ func (handler Handler) getAPIUrl(scope string) string { controller, _ = url.Parse("/api/v3/slave/delete") case "thumb": controller, _ = url.Parse("/api/v3/slave/thumb") + case "remote_callback": + controller, _ = url.Parse("/api/v3/callback/remote") + default: + controller = serverURL + } + + for _, r := range routes { + controller.Path = path.Join(controller.Path, r) } return serverURL.ResolveReference(controller).String() @@ -187,9 +195,7 @@ func (handler Handler) Source( // Token 获取上传策略和认证Token func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serializer.UploadCredential, error) { // 生成回调地址 - siteURL := model.GetSiteURL() - apiBaseURI, _ := url.Parse("/api/v3/callback/remote/" + key) - apiURL := siteURL.ResolveReference(apiBaseURI) + apiURL := handler.getAPIUrl("remote_callback", key) // 生成上传策略 policy := serializer.UploadPolicy{ @@ -198,8 +204,12 @@ func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serial AutoRename: handler.Policy.AutoRename, MaxSize: handler.Policy.MaxSize, AllowedExtension: handler.Policy.OptionsSerialized.FileType, - CallbackURL: apiURL.String(), + CallbackURL: apiURL, } + return handler.getUploadCredential(ctx, policy, TTL) +} + +func (handler Handler) getUploadCredential(ctx context.Context, policy serializer.UploadPolicy, TTL int64) (serializer.UploadCredential, error) { policyEncoded, err := policy.EncodeUploadPolicy() if err != nil { return serializer.UploadCredential{}, err @@ -219,5 +229,4 @@ func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serial }, nil } return serializer.UploadCredential{}, errors.New("无法签名上传策略") - } diff --git a/pkg/filesystem/remote/handler_test.go b/pkg/filesystem/remote/handler_test.go index 1f09954..a0ac249 100644 --- a/pkg/filesystem/remote/handler_test.go +++ b/pkg/filesystem/remote/handler_test.go @@ -29,6 +29,7 @@ func TestHandler_Token(t *testing.T) { OptionsSerialized: model.PolicyOption{ FileType: []string{"txt"}, }, + Server: "http://test.com", }, AuthInstance: auth.HMACAuth{}, } @@ -42,6 +43,7 @@ func TestHandler_Token(t *testing.T) { asserts.NoError(err) policy, err := serializer.DecodeUploadPolicy(credential.Policy) asserts.NoError(err) + asserts.Equal("http://test.com/api/v3/callback/remote/123", policy.CallbackURL) asserts.Equal(uint64(10), policy.MaxSize) asserts.Equal(true, policy.AutoRename) asserts.Equal("dir", policy.SavePath) diff --git a/pkg/filesystem/upload.go b/pkg/filesystem/upload.go index 6072171..3cbb88f 100644 --- a/pkg/filesystem/upload.go +++ b/pkg/filesystem/upload.go @@ -8,7 +8,7 @@ import ( "github.com/HFO4/cloudreve/pkg/serializer" "github.com/HFO4/cloudreve/pkg/util" "github.com/gin-gonic/gin" - "path/filepath" + "path" ) /* ================ @@ -73,7 +73,7 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) { // TODO 完善测试 func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string { if fs.User.Model.ID != 0 { - return filepath.Join( + return path.Join( fs.User.Policy.GeneratePath( fs.User.Model.ID, file.GetVirtualPath(), @@ -95,7 +95,7 @@ func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) str FileNameRule: policy.FileName, } } - return filepath.Join( + return path.Join( anonymousPolicy.GeneratePath( 0, "",