mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 18:57:50 +01:00
* added upload status * added changes for progress bar with more logs and bug fixes, Documentation yet to be added * huge changes that need more doucmenting * added possibility to get profile using NodeID * added fix profile listing user profile information * removed profile image from the explore reult struct * saving current changes * added filter to search based on NodeID * Monday bug fixing * updates to the profile * changes for tracing the blockchain profile image not shown * added condition to ensure TAG is not sent and removed debug prints * updated webapi docs
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
/*
|
|
File Username: 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
|
|
|
|
// PathDirectory sanitizes a directory path (without filename)
|
|
func PathDirectory(directory 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, "/")
|
|
|
|
// Enforce max length.
|
|
if len(directory) > PATH_MAX_LENGTH {
|
|
directory = directory[:PATH_MAX_LENGTH]
|
|
}
|
|
|
|
return directory
|
|
}
|
|
|
|
// PathFile sanitizes the filename.
|
|
func PathFile(filename string) string {
|
|
// Enforce max filename length.
|
|
if len(filename) > PATH_MAX_LENGTH {
|
|
filename = filename[:PATH_MAX_LENGTH]
|
|
}
|
|
|
|
return 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
|
|
}
|