cheapcash/sanitize.go

30 lines
709 B
Go
Raw Normal View History

2021-11-16 04:56:33 +00:00
package cheapcash
import "strings"
type restriction struct {
Key string
ReplaceWith string
}
2021-11-19 10:55:31 +00:00
// Sanitize path that is a valid string when the user
// submitted it, but it's invalid when it's written
// as a file name on local filesystem.
2021-11-16 04:56:33 +00:00
func sanitizePath(path string) string {
restricted := []restriction{
{Key: " ", ReplaceWith: "_s_"},
{Key: "^", ReplaceWith: "_p_"},
{Key: "*", ReplaceWith: "_a_"},
{Key: "\"", ReplaceWith: "_dq_"},
{Key: "'", ReplaceWith: "_sq_"},
{Key: "?", ReplaceWith: "_qm_"},
{Key: ">", ReplaceWith: "_gt_"},
{Key: "<", ReplaceWith: "_lt_"},
}
for _, v := range restricted {
path = strings.ReplaceAll(path, v.Key, v.ReplaceWith)
}
return path
}