Filename sanitization for Go
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:
00
to 1f
)/ \ ? * : | " < >
Gozaru is a Go port of zaru by @madrobby. Thanks a lot for him.