README
¶
gozaru
Filename sanitization for Go
Installation
$ go get github.com/subosito/gozaru
Usage
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
Filenames overview
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):
/ \ ? * : | " < >
Credits
Gozaru is a Go port of zaru by @madrobby. Thanks a lot for him.
Documentation
¶
Index ¶
Constants ¶
View Source
const ( CharacterFilter = `[\x00-\x1F\/\\:\*\?\"<>\|]` UnicodeWhitespace = `[[:space:]]+` )
Variables ¶
View Source
var ( FallbackFilename = "file" WindowsReservedNames = [...]string{ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", } )
Functions ¶
func SanitizeFallback ¶
func SanitizePad ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.