mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-11 16:24:11 +01:00
157 lines
3.5 KiB
Go
157 lines
3.5 KiB
Go
package rnd
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
// TypeEmpty marks an empty identifier.
|
|
TypeEmpty Type = "empty"
|
|
// TypeMixed marks a mixed collection of identifier types.
|
|
TypeMixed Type = "mixed"
|
|
// TypeUUID identifies standard UUID strings.
|
|
TypeUUID Type = "UUID"
|
|
// TypeUID identifies PhotoPrism UID strings.
|
|
TypeUID Type = "UID"
|
|
// TypeRefID identifies reference IDs (RID).
|
|
TypeRefID Type = "RID"
|
|
// TypeSessionID identifies session IDs.
|
|
TypeSessionID Type = "SID"
|
|
// TypeCrcToken identifies CRC tokens.
|
|
TypeCrcToken Type = "CRC"
|
|
// TypeMD5 identifies MD5 hashes.
|
|
TypeMD5 Type = "MD5"
|
|
// TypeSHA1 identifies SHA1 hashes.
|
|
TypeSHA1 Type = "SHA1"
|
|
// TypeSHA224 identifies SHA224 hashes.
|
|
TypeSHA224 Type = "SHA224"
|
|
// TypeSHA256 identifies SHA256 hashes.
|
|
TypeSHA256 Type = "SHA256"
|
|
// TypeSHA384 identifies SHA384 hashes.
|
|
TypeSHA384 Type = "SHA384"
|
|
// TypeSHA512 identifies SHA512 hashes.
|
|
TypeSHA512 Type = "SHA512"
|
|
// TypeUnknown marks identifiers that cannot be classified.
|
|
TypeUnknown Type = "unknown"
|
|
)
|
|
|
|
// IdType checks what kind of random ID a string contains
|
|
// and returns it along with the id prefix, if any.
|
|
func IdType(id string) (Type, byte) {
|
|
if l := len(id); l == 0 {
|
|
return TypeEmpty, PrefixNone
|
|
} else if l < 14 || l > 128 {
|
|
return TypeUnknown, PrefixNone
|
|
}
|
|
|
|
switch {
|
|
case IsUID(id, 0):
|
|
return TypeUID, id[0]
|
|
case IsUUID(id):
|
|
return TypeUUID, PrefixNone
|
|
case IsSHA1(id):
|
|
return TypeSHA1, PrefixNone
|
|
case IsRefID(id):
|
|
return TypeRefID, PrefixNone
|
|
case IsAuthToken(id):
|
|
return TypeSessionID, PrefixNone
|
|
case ValidateCrcToken(id):
|
|
return TypeCrcToken, PrefixNone
|
|
case IsSHA224(id):
|
|
return TypeSHA224, PrefixNone
|
|
case IsSHA256(id):
|
|
return TypeSHA256, PrefixNone
|
|
case IsSHA384(id):
|
|
return TypeSHA384, PrefixNone
|
|
case IsSHA512(id):
|
|
return TypeSHA512, PrefixNone
|
|
case IsMD5(id):
|
|
return TypeMD5, PrefixNone
|
|
default:
|
|
return TypeUnknown, PrefixNone
|
|
}
|
|
}
|
|
|
|
// Type represents a random id type.
|
|
type Type string
|
|
|
|
// String returns the type as string.
|
|
func (t Type) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
// Equal checks if the type matches.
|
|
func (t Type) Equal(s string) bool {
|
|
return strings.EqualFold(s, t.String())
|
|
}
|
|
|
|
// NotEqual checks if the type is different.
|
|
func (t Type) NotEqual(s string) bool {
|
|
return !t.Equal(s)
|
|
}
|
|
|
|
// EntityID reports whether the type represents an entity identifier.
|
|
func (t Type) EntityID() bool {
|
|
switch t {
|
|
case TypeUID, TypeUUID, TypeRefID, TypeCrcToken, TypeSessionID:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// SessionID reports whether the type is a session ID.
|
|
func (t Type) SessionID() bool {
|
|
return t == TypeSessionID
|
|
}
|
|
|
|
// CrcToken reports whether the type is a CRC token.
|
|
func (t Type) CrcToken() bool {
|
|
switch t {
|
|
case TypeCrcToken:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Hash reports whether the type represents a hash value.
|
|
func (t Type) Hash() bool {
|
|
switch t {
|
|
case TypeMD5:
|
|
return true
|
|
default:
|
|
return t.SHA()
|
|
}
|
|
}
|
|
|
|
// SHA reports whether the type is a SHA-family hash (SHA1 or SHA2).
|
|
func (t Type) SHA() bool {
|
|
return t.SHA1() || t.SHA2()
|
|
}
|
|
|
|
// SHA1 reports whether the type is a SHA1 hash.
|
|
func (t Type) SHA1() bool {
|
|
switch t {
|
|
case TypeSHA1:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// SHA2 reports whether the type is a SHA2 family hash.
|
|
func (t Type) SHA2() bool {
|
|
switch t {
|
|
case TypeSHA224, TypeSHA256, TypeSHA384, TypeSHA512:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Unknown checks if the type is unknown.
|
|
func (t Type) Unknown() bool {
|
|
return t == TypeUnknown
|
|
}
|