Modify: use path package in app level

This commit is contained in:
HFO4 2020-01-04 16:21:43 +08:00
parent d94896041e
commit c23d129dbb
5 changed files with 27 additions and 15 deletions

View file

@ -6,7 +6,7 @@ import (
"github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"path/filepath" "path"
"strconv" "strconv"
"time" "time"
) )
@ -96,7 +96,7 @@ func (policy *Policy) SerializeOptions() (err error) {
} }
// GeneratePath 生成存储文件的路径 // GeneratePath 生成存储文件的路径
func (policy *Policy) GeneratePath(uid uint, path string) string { func (policy *Policy) GeneratePath(uid uint, origin string) string {
dirRule := policy.DirNameRule dirRule := policy.DirNameRule
replaceTable := map[string]string{ replaceTable := map[string]string{
"{randomkey16}": util.RandStringRunes(16), "{randomkey16}": util.RandStringRunes(16),
@ -105,10 +105,10 @@ func (policy *Policy) GeneratePath(uid uint, path string) string {
"{uid}": strconv.Itoa(int(uid)), "{uid}": strconv.Itoa(int(uid)),
"{datetime}": time.Now().Format("20060102150405"), "{datetime}": time.Now().Format("20060102150405"),
"{date}": time.Now().Format("20060102"), "{date}": time.Now().Format("20060102"),
"{path}": path + "/", "{path}": origin + "/",
} }
dirRule = util.Replace(replaceTable, dirRule) dirRule = util.Replace(replaceTable, dirRule)
return filepath.Clean(dirRule) return path.Clean(dirRule)
} }
// GenerateFileName 生成存储文件名 // GenerateFileName 生成存储文件名

View file

@ -51,6 +51,7 @@ func closeReader(ctx context.Context, closer io.Closer) {
// Put 将文件流保存到指定目录 // Put 将文件流保存到指定目录
func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error { func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
defer file.Close() defer file.Close()
dst = filepath.FromSlash(dst)
// 如果目标目录不存在,创建 // 如果目标目录不存在,创建
basePath := filepath.Dir(dst) basePath := filepath.Dir(dst)
@ -82,7 +83,7 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er
var retErr error var retErr error
for _, value := range files { for _, value := range files {
err := os.Remove(value) err := os.Remove(filepath.FromSlash(value))
if err != nil { if err != nil {
util.Log().Warning("无法删除文件,%s", err) util.Log().Warning("无法删除文件,%s", err)
retErr = err retErr = err

View file

@ -1,6 +1,5 @@
package remote package remote
// TODO 测试
import ( import (
"context" "context"
"encoding/base64" "encoding/base64"
@ -16,6 +15,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"path"
"strings" "strings"
) )
@ -27,7 +27,7 @@ type Handler struct {
} }
// getAPIUrl 获取接口请求地址 // getAPIUrl 获取接口请求地址
func (handler Handler) getAPIUrl(scope string) string { func (handler Handler) getAPIUrl(scope string, routes ...string) string {
serverURL, err := url.Parse(handler.Policy.Server) serverURL, err := url.Parse(handler.Policy.Server)
if err != nil { if err != nil {
return "" return ""
@ -39,6 +39,14 @@ func (handler Handler) getAPIUrl(scope string) string {
controller, _ = url.Parse("/api/v3/slave/delete") controller, _ = url.Parse("/api/v3/slave/delete")
case "thumb": case "thumb":
controller, _ = url.Parse("/api/v3/slave/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() return serverURL.ResolveReference(controller).String()
@ -187,9 +195,7 @@ func (handler Handler) Source(
// Token 获取上传策略和认证Token // Token 获取上传策略和认证Token
func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serializer.UploadCredential, error) { func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serializer.UploadCredential, error) {
// 生成回调地址 // 生成回调地址
siteURL := model.GetSiteURL() apiURL := handler.getAPIUrl("remote_callback", key)
apiBaseURI, _ := url.Parse("/api/v3/callback/remote/" + key)
apiURL := siteURL.ResolveReference(apiBaseURI)
// 生成上传策略 // 生成上传策略
policy := serializer.UploadPolicy{ policy := serializer.UploadPolicy{
@ -198,8 +204,12 @@ func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serial
AutoRename: handler.Policy.AutoRename, AutoRename: handler.Policy.AutoRename,
MaxSize: handler.Policy.MaxSize, MaxSize: handler.Policy.MaxSize,
AllowedExtension: handler.Policy.OptionsSerialized.FileType, 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() policyEncoded, err := policy.EncodeUploadPolicy()
if err != nil { if err != nil {
return serializer.UploadCredential{}, err return serializer.UploadCredential{}, err
@ -219,5 +229,4 @@ func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serial
}, nil }, nil
} }
return serializer.UploadCredential{}, errors.New("无法签名上传策略") return serializer.UploadCredential{}, errors.New("无法签名上传策略")
} }

View file

@ -29,6 +29,7 @@ func TestHandler_Token(t *testing.T) {
OptionsSerialized: model.PolicyOption{ OptionsSerialized: model.PolicyOption{
FileType: []string{"txt"}, FileType: []string{"txt"},
}, },
Server: "http://test.com",
}, },
AuthInstance: auth.HMACAuth{}, AuthInstance: auth.HMACAuth{},
} }
@ -42,6 +43,7 @@ func TestHandler_Token(t *testing.T) {
asserts.NoError(err) asserts.NoError(err)
policy, err := serializer.DecodeUploadPolicy(credential.Policy) policy, err := serializer.DecodeUploadPolicy(credential.Policy)
asserts.NoError(err) asserts.NoError(err)
asserts.Equal("http://test.com/api/v3/callback/remote/123", policy.CallbackURL)
asserts.Equal(uint64(10), policy.MaxSize) asserts.Equal(uint64(10), policy.MaxSize)
asserts.Equal(true, policy.AutoRename) asserts.Equal(true, policy.AutoRename)
asserts.Equal("dir", policy.SavePath) asserts.Equal("dir", policy.SavePath)

View file

@ -8,7 +8,7 @@ import (
"github.com/HFO4/cloudreve/pkg/serializer" "github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"path/filepath" "path"
) )
/* ================ /* ================
@ -73,7 +73,7 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
// TODO 完善测试 // TODO 完善测试
func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string { func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string {
if fs.User.Model.ID != 0 { if fs.User.Model.ID != 0 {
return filepath.Join( return path.Join(
fs.User.Policy.GeneratePath( fs.User.Policy.GeneratePath(
fs.User.Model.ID, fs.User.Model.ID,
file.GetVirtualPath(), file.GetVirtualPath(),
@ -95,7 +95,7 @@ func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) str
FileNameRule: policy.FileName, FileNameRule: policy.FileName,
} }
} }
return filepath.Join( return path.Join(
anonymousPolicy.GeneratePath( anonymousPolicy.GeneratePath(
0, 0,
"", "",