Files
core/sanitize/Sanitize.go
Kleissner a2420b8468 Store user blockchain data in file. Close #30
Refactoring sanitize code into separate sub-package
Refactoring block and block record decoding code
2021-08-19 04:13:08 +02:00

53 lines
1.2 KiB
Go

/*
File Name: Sanitize.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package sanitize
import (
"path"
"strings"
"unicode/utf8"
)
const PATH_MAX_LENGTH = 32767 // Windows Maximum Path Length for UNC paths
// Path sanitizies the directory and filename.
func Path(directory, filename string) (string, string) {
// Enforced forward slashes as directory separator and clean the path.
directory = strings.ReplaceAll(directory, "\\", "/")
directory = path.Clean(directory)
// No slash at the beginning and end to save space.
directory = strings.Trim(directory, "/")
// Slashes in filenames are not encouraged, but not removed.
// Enforce max filename length.
if len(filename) > PATH_MAX_LENGTH {
filename = filename[:PATH_MAX_LENGTH]
}
return directory, filename
}
// Username sanitizes the username.
func Username(input string) string {
if !utf8.ValidString(input) {
return "<invalid encoding>"
}
input = strings.TrimSpace(input)
input = strings.ReplaceAll(input, "\n", " ")
input = strings.ReplaceAll(input, "\r", "")
// Max length for sanitized version is 36, resembling the limit from StackOverflow.
if len(input) > 36 {
input = input[:36]
}
return input
}