Modify: use path package in app level
This commit is contained in:
parent
d94896041e
commit
c23d129dbb
5 changed files with 27 additions and 15 deletions
|
@ -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 生成存储文件名
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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("无法签名上传策略")
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
"",
|
||||
|
|
Loading…
Add table
Reference in a new issue