Этот коммит содержится в:
Ben Schumacher
2025-01-13 20:23:09 +01:00
коммит произвёл GitHub
родитель 091d1bba8b
Коммит 8d4bf4bae0
50 изменённых файлов: 2429 добавлений и 745 удалений

Просмотреть файл

@@ -6,6 +6,8 @@ package utils
import (
"os"
"path/filepath"
"regexp"
"strings"
)
func CommonBaseSearchPaths() []string {
@@ -111,3 +113,25 @@ func FindDirRelBinary(dir string) (string, bool) {
}
return found, true
}
// Valid characters are: alphanumeric, dash, underscore
var safeFileNameRegex = regexp.MustCompile(`[^\w\-\_]`)
// SanitizeFileName takes a string and returns a safe file name without an extension.
func SanitizeFileName(input string) string {
// Trim leading or trailing dots or spaces
safeName := strings.Trim(input, ". ")
// Replace dots with nothing
safeName = strings.ReplaceAll(safeName, ".", "")
// Replace all invalid characters with an underscore
safeName = safeFileNameRegex.ReplaceAllString(safeName, "_")
// Limit length
const maxLength = 100
if len(safeName) > maxLength {
safeName = safeName[:maxLength]
}
return safeName
}