2020-05-27 13:40:21 +02:00
|
|
|
package rnd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PPID returns a unique id with prefix as string.
|
|
|
|
func PPID(prefix byte) string {
|
|
|
|
result := make([]byte, 0, 16)
|
|
|
|
result = append(result, prefix)
|
|
|
|
result = append(result, strconv.FormatInt(time.Now().UTC().Unix(), 36)[0:6]...)
|
|
|
|
result = append(result, Token(9)...)
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPPID returns true if string is a unique id as generated by PhotoPrism.
|
|
|
|
func IsPPID(s string, prefix byte) bool {
|
|
|
|
if len(s) != 16 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if !IsLowerAlnum(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix == 0 || s[0] == prefix
|
2020-06-01 09:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsHex returns true if the string only contains hex numbers, dashes and letters without whitespace.
|
|
|
|
func IsHex(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
2020-06-29 11:10:24 +02:00
|
|
|
if (r < 48 || r > 57) && (r < 97 || r > 102) && (r < 65 || r > 70) && r != 45 {
|
2020-06-01 09:45:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLowerAlnum returns true if the string only contains alphanumeric ascii chars without whitespace.
|
|
|
|
func IsLowerAlnum(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if (r < 48 || r > 57) && (r < 97 || r > 122) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-05-27 13:40:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
// Returns true if the string looks like a standard UUID.
|
|
|
|
func IsUUID(s string) bool {
|
|
|
|
return len(s) == 36 && IsHex(s)
|
|
|
|
}
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
// IsUID returns true if string is a seemingly unique id.
|
|
|
|
func IsUID(s string, prefix byte) bool {
|
|
|
|
// Regular UUID.
|
2020-06-29 11:10:24 +02:00
|
|
|
if IsUUID(s) {
|
2020-05-27 13:40:21 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a known UID format.
|
|
|
|
if len(s) != 16 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsPPID(s, prefix)
|
|
|
|
}
|