mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: {path} marker in name rule representing the virtual path of the file
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/jinzhu/gorm"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
@@ -69,7 +70,7 @@ func (policy *Policy) SerializeOptions() (err error) {
|
||||
}
|
||||
|
||||
// GeneratePath 生成存储文件的路径
|
||||
func (policy *Policy) GeneratePath(uid uint) string {
|
||||
func (policy *Policy) GeneratePath(uid uint, path string) string {
|
||||
dirRule := policy.DirNameRule
|
||||
replaceTable := map[string]string{
|
||||
"{randomkey16}": util.RandStringRunes(16),
|
||||
@@ -78,9 +79,10 @@ func (policy *Policy) GeneratePath(uid uint) string {
|
||||
"{uid}": strconv.Itoa(int(uid)),
|
||||
"{datetime}": time.Now().Format("20060102150405"),
|
||||
"{date}": time.Now().Format("20060102"),
|
||||
"{path}": path + "/",
|
||||
}
|
||||
dirRule = util.Replace(replaceTable, dirRule)
|
||||
return dirRule
|
||||
return filepath.Clean(dirRule)
|
||||
}
|
||||
|
||||
// GenerateFileName 生成存储文件名
|
||||
|
||||
@@ -46,25 +46,32 @@ func TestPolicy_GeneratePath(t *testing.T) {
|
||||
testPolicy := Policy{}
|
||||
|
||||
testPolicy.DirNameRule = "{randomkey16}"
|
||||
asserts.Len(testPolicy.GeneratePath(1), 16)
|
||||
asserts.Len(testPolicy.GeneratePath(1, "/"), 16)
|
||||
|
||||
testPolicy.DirNameRule = "{randomkey8}"
|
||||
asserts.Len(testPolicy.GeneratePath(1), 8)
|
||||
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
|
||||
|
||||
testPolicy.DirNameRule = "{timestamp}"
|
||||
asserts.Equal(testPolicy.GeneratePath(1), strconv.FormatInt(time.Now().Unix(), 10))
|
||||
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
testPolicy.DirNameRule = "{uid}"
|
||||
asserts.Equal(testPolicy.GeneratePath(1), strconv.Itoa(int(1)))
|
||||
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.Itoa(int(1)))
|
||||
|
||||
testPolicy.DirNameRule = "{datetime}"
|
||||
asserts.Len(testPolicy.GeneratePath(1), 14)
|
||||
asserts.Len(testPolicy.GeneratePath(1, "/"), 14)
|
||||
|
||||
testPolicy.DirNameRule = "{date}"
|
||||
asserts.Len(testPolicy.GeneratePath(1), 8)
|
||||
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
|
||||
|
||||
testPolicy.DirNameRule = "123{date}ss{datetime}"
|
||||
asserts.Len(testPolicy.GeneratePath(1), 27)
|
||||
asserts.Len(testPolicy.GeneratePath(1, "/"), 27)
|
||||
|
||||
testPolicy.DirNameRule = "/1/{path}/456"
|
||||
asserts.Condition(func() (success bool) {
|
||||
res := testPolicy.GeneratePath(1, "/23")
|
||||
return res == "/1/23/456" || res == "\\1\\23\\456"
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestPolicy_GenerateFileName(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user