Fix json filename generation (#2887)

This commit is contained in:
WithoutPants
2022-09-06 13:10:24 +10:00
parent 0c513a604d
commit 13bdba5b24
9 changed files with 63 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
@@ -116,3 +117,25 @@ func Touch(path string) error {
}
return nil
}
var (
replaceCharsRE = regexp.MustCompile(`[&=\\/:*"?_ ]`)
removeCharsRE = regexp.MustCompile(`[^[:alnum:]-.]`)
multiHyphenRE = regexp.MustCompile(`\-+`)
)
// SanitiseBasename returns a file basename removing any characters that are illegal or problematic to use in the filesystem.
func SanitiseBasename(v string) string {
v = strings.TrimSpace(v)
// replace illegal filename characters with -
v = replaceCharsRE.ReplaceAllString(v, "-")
// remove other characters
v = removeCharsRE.ReplaceAllString(v, "")
// remove multiple hyphens
v = multiHyphenRE.ReplaceAllString(v, "-")
return strings.TrimSpace(v)
}