feat(blob path): diable `{path}` magic var for blob path

pull/2507/merge 4.4.1
Aaron Liu 2025-08-07 11:35:28 +08:00
parent b0375f5a24
commit 4c976b8627
2 changed files with 52 additions and 49 deletions

2
assets

@ -1 +1 @@
Subproject commit 09480ffa21d859a1d2f9bb2421e6f78f113494c4 Subproject commit 3a23464a0f4330e4583e99c1815b84c5240ffc9d

View File

@ -770,66 +770,69 @@ func generateSavePath(policy *ent.StoragePolicy, req *fs.UploadRequest, user *en
currentTime := time.Now() currentTime := time.Now()
originName := req.Props.Uri.Name() originName := req.Props.Uri.Name()
dynamicReplace := func(regPattern string, rule string) string { dynamicReplace := func(regPattern string, rule string, pathAvailable bool) string {
re := regexp.MustCompile(regPattern) re := regexp.MustCompile(regPattern)
return re.ReplaceAllStringFunc(rule, func(match string) string { return re.ReplaceAllStringFunc(rule, func(match string) string {
switch match { switch match {
case "{timestamp}": case "{timestamp}":
return strconv.FormatInt(currentTime.Unix(), 10) return strconv.FormatInt(currentTime.Unix(), 10)
case "{timestamp_nano}": case "{timestamp_nano}":
return strconv.FormatInt(currentTime.UnixNano(), 10) return strconv.FormatInt(currentTime.UnixNano(), 10)
case "{datetime}": case "{datetime}":
return currentTime.Format("20060102150405") return currentTime.Format("20060102150405")
case "{date}": case "{date}":
return currentTime.Format("20060102") return currentTime.Format("20060102")
case "{year}": case "{year}":
return currentTime.Format("2006") return currentTime.Format("2006")
case "{month}": case "{month}":
return currentTime.Format("01") return currentTime.Format("01")
case "{day}": case "{day}":
return currentTime.Format("02") return currentTime.Format("02")
case "{hour}": case "{hour}":
return currentTime.Format("15") return currentTime.Format("15")
case "{minute}": case "{minute}":
return currentTime.Format("04") return currentTime.Format("04")
case "{second}": case "{second}":
return currentTime.Format("05") return currentTime.Format("05")
case "{uid}": case "{uid}":
return strconv.Itoa(user.ID) return strconv.Itoa(user.ID)
case "{randomkey16}": case "{randomkey16}":
return util.RandStringRunes(16) return util.RandStringRunes(16)
case "{randomkey8}": case "{randomkey8}":
return util.RandStringRunes(8) return util.RandStringRunes(8)
case "{randomnum8}": case "{randomnum8}":
return strconv.Itoa(rand.Intn(8)) return strconv.Itoa(rand.Intn(8))
case "{randomnum4}": case "{randomnum4}":
return strconv.Itoa(rand.Intn(4)) return strconv.Itoa(rand.Intn(4))
case "{randomnum3}": case "{randomnum3}":
return strconv.Itoa(rand.Intn(3)) return strconv.Itoa(rand.Intn(3))
case "{randomnum2}": case "{randomnum2}":
return strconv.Itoa(rand.Intn(2)) return strconv.Itoa(rand.Intn(2))
case "{uuid}": case "{uuid}":
return uuid.Must(uuid.NewV4()).String() return uuid.Must(uuid.NewV4()).String()
case "{path}": case "{path}":
if pathAvailable {
return req.Props.Uri.Dir() + fs.Separator return req.Props.Uri.Dir() + fs.Separator
case "{originname}": }
return originName return match
case "{ext}": case "{originname}":
return filepath.Ext(originName) return originName
case "{originname_without_ext}": case "{ext}":
return strings.TrimSuffix(originName, filepath.Ext(originName)) return filepath.Ext(originName)
default: case "{originname_without_ext}":
return match 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 = dynamicReplace(`\{[^{}]+\}`, dirRule) dirRule = dynamicReplace(`\{[^{}]+\}`, dirRule, true)
nameRule := policy.FileNameRule nameRule := policy.FileNameRule
nameRule = dynamicReplace(`\{[^{}]+\}`, nameRule) nameRule = dynamicReplace(`\{[^{}]+\}`, nameRule, false)
return path.Join(path.Clean(dirRule), nameRule) return path.Join(path.Clean(dirRule), nameRule)
} }