fix file naming sanitisation

This commit is contained in:
2026-03-23 17:46:27 +01:00
parent 5bcca61d59
commit 82eb9de5f1
4 changed files with 32 additions and 35 deletions

View File

@@ -1,6 +1,9 @@
package util
import "fmt"
import (
"fmt"
"strings"
)
func HumanSize(size int64) string {
const unit = 1024
@@ -17,3 +20,24 @@ func HumanSize(size int64) string {
"KMGTPE"[exp],
)
}
func SafeFilename(name string) string {
name = strings.TrimSpace(name)
out := make([]rune, 0, len(name))
for _, r := range name {
// block control chars (incl CR/LF/TAB), DEL, quotes, backslash
if r < 32 || r == 127 || r == '"' || r == '\\' {
continue
}
out = append(out, r)
}
if len(out) == 0 {
return "file"
}
// optional: cap length
if len(out) > 200 {
out = out[:200]
}
return string(out)
}