fix file naming sanitisation
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user