photoprism/pkg/rnd/uid.go
Theresa Gresch ad9167360d
Feature/246 (#345)
* Import: Implement "add to album" in backend #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Import: Implement "add to album" in frontend #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Add OriginalName to photo search result

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Add json tags to PhotoName and PhotoPath

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Photo: Use EstimateCountry() in UpdateLocation()

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Photo: Set OriginalName earlier while indexing

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Ignore whitespace when stripping sequence from filename #335

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Fix labels count for SQLite

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Import: Show name of new albums #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Frontend: Add acceptance test files

Co-authored-by: Michael Mayer <michael@liquidbytes.net>
2020-06-01 09:45:24 +02:00

71 lines
1.4 KiB
Go

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
}
return s[0] == prefix && IsLowerAlnum(s)
}
// 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 {
if (r < 48 || r > 57) && (r < 97 || r > 102) && (r < 65 || r > 90) && r != 45{
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
}
// IsUID returns true if string is a seemingly unique id.
func IsUID(s string, prefix byte) bool {
// Regular UUID.
if len(s) == 36 && IsHex(s) {
return true
}
// Not a known UID format.
if len(s) != 16 {
return false
}
return IsPPID(s, prefix)
}