mirror of https://github.com/cloudreve/Cloudreve
fix(blob path): Random variables in blob save path be wrongly fixed (#2741)
* fix(blob path): Random variables in blob save path be wrongly fixed * feat(blob path): Use regex to match all magic variablespull/2746/head
parent
80b25e88ee
commit
7654ce889c
|
@ -7,6 +7,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -765,44 +766,69 @@ func (f *DBFS) navigatorId(path *fs.URI) string {
|
||||||
|
|
||||||
// generateSavePath generates the physical save path for the upload request.
|
// generateSavePath generates the physical save path for the upload request.
|
||||||
func generateSavePath(policy *ent.StoragePolicy, req *fs.UploadRequest, user *ent.User) string {
|
func generateSavePath(policy *ent.StoragePolicy, req *fs.UploadRequest, user *ent.User) string {
|
||||||
baseTable := map[string]string{
|
currentTime := time.Now()
|
||||||
"{randomkey16}": util.RandStringRunes(16),
|
originName := req.Props.Uri.Name()
|
||||||
"{randomkey8}": util.RandStringRunes(8),
|
|
||||||
"{timestamp}": strconv.FormatInt(time.Now().Unix(), 10),
|
dynamicReplace := func(regPattern string, rule string) string {
|
||||||
"{timestamp_nano}": strconv.FormatInt(time.Now().UnixNano(), 10),
|
re := regexp.MustCompile(regPattern)
|
||||||
"{randomnum2}": strconv.Itoa(rand.Intn(2)),
|
return re.ReplaceAllStringFunc(rule, func(match string) string {
|
||||||
"{randomnum3}": strconv.Itoa(rand.Intn(3)),
|
switch match {
|
||||||
"{randomnum4}": strconv.Itoa(rand.Intn(4)),
|
case "{timestamp}":
|
||||||
"{randomnum8}": strconv.Itoa(rand.Intn(8)),
|
return strconv.FormatInt(currentTime.Unix(), 10)
|
||||||
"{uid}": strconv.Itoa(user.ID),
|
case "{timestamp_nano}":
|
||||||
"{datetime}": time.Now().Format("20060102150405"),
|
return strconv.FormatInt(currentTime.UnixNano(), 10)
|
||||||
"{date}": time.Now().Format("20060102"),
|
case "{datetime}":
|
||||||
"{year}": time.Now().Format("2006"),
|
return currentTime.Format("20060102150405")
|
||||||
"{month}": time.Now().Format("01"),
|
case "{date}":
|
||||||
"{day}": time.Now().Format("02"),
|
return currentTime.Format("20060102")
|
||||||
"{hour}": time.Now().Format("15"),
|
case "{year}":
|
||||||
"{minute}": time.Now().Format("04"),
|
return currentTime.Format("2006")
|
||||||
"{second}": time.Now().Format("05"),
|
case "{month}":
|
||||||
|
return currentTime.Format("01")
|
||||||
|
case "{day}":
|
||||||
|
return currentTime.Format("02")
|
||||||
|
case "{hour}":
|
||||||
|
return currentTime.Format("15")
|
||||||
|
case "{minute}":
|
||||||
|
return currentTime.Format("04")
|
||||||
|
case "{second}":
|
||||||
|
return currentTime.Format("05")
|
||||||
|
case "{uid}":
|
||||||
|
return strconv.Itoa(user.ID)
|
||||||
|
case "{randomkey16}":
|
||||||
|
return util.RandStringRunes(16)
|
||||||
|
case "{randomkey8}":
|
||||||
|
return util.RandStringRunes(8)
|
||||||
|
case "{randomnum8}":
|
||||||
|
return strconv.Itoa(rand.Intn(8))
|
||||||
|
case "{randomnum4}":
|
||||||
|
return strconv.Itoa(rand.Intn(4))
|
||||||
|
case "{randomnum3}":
|
||||||
|
return strconv.Itoa(rand.Intn(3))
|
||||||
|
case "{randomnum2}":
|
||||||
|
return strconv.Itoa(rand.Intn(2))
|
||||||
|
case "{uuid}":
|
||||||
|
return uuid.Must(uuid.NewV4()).String()
|
||||||
|
case "{path}":
|
||||||
|
return req.Props.Uri.Dir() + fs.Separator
|
||||||
|
case "{originname}":
|
||||||
|
return originName
|
||||||
|
case "{ext}":
|
||||||
|
return filepath.Ext(originName)
|
||||||
|
case "{originname_without_ext}":
|
||||||
|
return strings.TrimSuffix(originName, filepath.Ext(originName))
|
||||||
|
default:
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
dirRule := policy.DirNameRule
|
dirRule := policy.DirNameRule
|
||||||
dirRule = filepath.ToSlash(dirRule)
|
dirRule = filepath.ToSlash(dirRule)
|
||||||
dirRule = util.Replace(baseTable, dirRule)
|
dirRule = dynamicReplace(`\{[^{}]+\}`, dirRule)
|
||||||
dirRule = util.Replace(map[string]string{
|
|
||||||
"{path}": req.Props.Uri.Dir() + fs.Separator,
|
|
||||||
}, dirRule)
|
|
||||||
|
|
||||||
originName := req.Props.Uri.Name()
|
|
||||||
nameTable := map[string]string{
|
|
||||||
"{originname}": originName,
|
|
||||||
"{ext}": filepath.Ext(originName),
|
|
||||||
"{originname_without_ext}": strings.TrimSuffix(originName, filepath.Ext(originName)),
|
|
||||||
"{uuid}": uuid.Must(uuid.NewV4()).String(),
|
|
||||||
}
|
|
||||||
|
|
||||||
nameRule := policy.FileNameRule
|
nameRule := policy.FileNameRule
|
||||||
nameRule = util.Replace(baseTable, nameRule)
|
nameRule = dynamicReplace(`\{[^{}]+\}`, nameRule)
|
||||||
nameRule = util.Replace(nameTable, nameRule)
|
|
||||||
|
|
||||||
return path.Join(path.Clean(dirRule), nameRule)
|
return path.Join(path.Clean(dirRule), nameRule)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue