mirror of
https://github.com/photoprism/photoprism.git
synced 2025-09-26 21:01:58 +08:00
35 lines
594 B
Go
35 lines
594 B
Go
package header
|
|
|
|
import "strings"
|
|
|
|
// ID sanitizes identifier tokens, for example, a session ID, a UUID, or some other string ID.
|
|
func ID(s string) string {
|
|
if s == "" || len(s) > 4096 {
|
|
return ""
|
|
}
|
|
|
|
var prev rune
|
|
|
|
// Remove all invalid characters.
|
|
s = strings.Map(func(r rune) rune {
|
|
if r == ' ' && (prev == 0 || prev == ' ') {
|
|
return -1
|
|
}
|
|
|
|
prev = r
|
|
|
|
switch r {
|
|
case ' ', '"', '-', '+', '/', '=', '#', '$', '@', ':', ';', '_', '.':
|
|
return r
|
|
}
|
|
|
|
if (r < '0' || r > '9') && (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
|
|
return -1
|
|
}
|
|
|
|
return r
|
|
}, s)
|
|
|
|
return s
|
|
}
|