/* 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 "" } 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 }