Filename sanitization for Go
$ go get github.com/subosito/gozaru
Gozaru basically normalizes, filters and truncates given filename to generates safe and cross-platform filename. For example:
package main
import (
"fmt"
"github.com/subosito/gozaru"
)
func main() {
name := gozaru.Sanitize(" what\\ēver//wëird:user:înput:")
fmt.Println(name) // => "whatēverwëirduserînput"
}
You can add extra room for filename by using SanitizePad
, see differences here:
// import "strings"
name := strings.Repeat("A", 400)
gozaru.Sanitize(name)
// => resulting filename is 255 characters long
gozaru.SanitizePad(name, 100)
// => resulting filename is 155 characters long
Best practices for having a safe and cross-platform filenames are:
- Does not contains ASCII control characters (hexadecimal
00
to1f
) - Does not contains Unicode whitespace at the beginning and the end of filename
- Does not contains multiple Unicode whitespaces within the filename
- Does not contains reserved filenames in Windows
- Does not contains following characters (according to wikipedia):
/ \ ? * : | " < >
Gozaru is a Go port of zaru by @madrobby. Thanks a lot for him.