Backend: Add PPID() to rnd package
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
d6b459186e
commit
fbea88bd74
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
// Photo album
|
||||
|
@ -31,7 +32,7 @@ type Album struct {
|
|||
}
|
||||
|
||||
func (m *Album) BeforeCreate(scope *gorm.Scope) error {
|
||||
if err := scope.SetColumn("AlbumUUID", ID('a')); err != nil {
|
||||
if err := scope.SetColumn("AlbumUUID", rnd.PPID('a')); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,8 @@ https://github.com/photoprism/photoprism/wiki/Storage
|
|||
package entity
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/event"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
var log = event.Log
|
||||
|
@ -25,12 +21,3 @@ func logError(result *gorm.DB) {
|
|||
log.Error(result.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func ID(prefix rune) string {
|
||||
result := make([]byte, 0, 17)
|
||||
result = append(result, byte(prefix))
|
||||
result = append(result, strconv.FormatInt(time.Now().UTC().Unix(), 36)[0:6]...)
|
||||
result = append(result, rnd.Token(10)...)
|
||||
|
||||
return string(result)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var logBuffer bytes.Buffer
|
||||
|
@ -18,17 +17,3 @@ func TestMain(m *testing.M) {
|
|||
code := m.Run()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestID(t *testing.T) {
|
||||
for n := 0; n < 5; n++ {
|
||||
uuid := ID('x')
|
||||
t.Logf("id: %s", uuid)
|
||||
assert.Equal(t, len(uuid), 17)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkID(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
ID('x')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
// Events
|
||||
|
@ -29,5 +30,5 @@ func (Event) TableName() string {
|
|||
}
|
||||
|
||||
func (e *Event) BeforeCreate(scope *gorm.Scope) error {
|
||||
return scope.SetColumn("EventUUID", ID('e'))
|
||||
return scope.SetColumn("EventUUID", rnd.PPID('e'))
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
// An image or sidecar file that belongs to a photo
|
||||
|
@ -52,7 +53,7 @@ func FindFileByHash(db *gorm.DB, fileHash string) (File, error) {
|
|||
}
|
||||
|
||||
func (m *File) BeforeCreate(scope *gorm.Scope) error {
|
||||
return scope.SetColumn("FileUUID", ID('f'))
|
||||
return scope.SetColumn("FileUUID", rnd.PPID('f'))
|
||||
}
|
||||
|
||||
func (m *File) DownloadFileName() string {
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/gosimple/slug"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/mutex"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
// Labels for photo, album and location categorization
|
||||
|
@ -27,7 +28,7 @@ type Label struct {
|
|||
}
|
||||
|
||||
func (m *Label) BeforeCreate(scope *gorm.Scope) error {
|
||||
if err := scope.SetColumn("LabelUUID", ID('l')); err != nil {
|
||||
if err := scope.SetColumn("LabelUUID", rnd.PPID('l')); err != nil {
|
||||
log.Errorf("label: %s", err)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
"github.com/photoprism/photoprism/internal/txt"
|
||||
)
|
||||
|
||||
|
@ -59,7 +60,7 @@ type Photo struct {
|
|||
}
|
||||
|
||||
func (m *Photo) BeforeCreate(scope *gorm.Scope) error {
|
||||
if err := scope.SetColumn("PhotoUUID", ID('p')); err != nil {
|
||||
if err := scope.SetColumn("PhotoUUID", rnd.PPID('p')); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/rnd"
|
||||
)
|
||||
|
||||
// Shared photos and/or albums
|
||||
|
@ -26,7 +27,7 @@ func (Share) TableName() string {
|
|||
}
|
||||
|
||||
func (s *Share) BeforeCreate(scope *gorm.Scope) error {
|
||||
if err := scope.SetColumn("ShareUUID", ID('s')); err != nil {
|
||||
if err := scope.SetColumn("ShareUUID", rnd.PPID('s')); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -36,8 +36,9 @@ func Password() string {
|
|||
return Token(8)
|
||||
}
|
||||
|
||||
func ID() string {
|
||||
result := make([]byte, 0, 16)
|
||||
func PPID(prefix rune) string {
|
||||
result := make([]byte, 0, 17)
|
||||
result = append(result, byte(prefix))
|
||||
result = append(result, strconv.FormatInt(time.Now().UTC().Unix(), 36)[0:6]...)
|
||||
result = append(result, Token(10)...)
|
||||
|
||||
|
|
|
@ -54,3 +54,18 @@ func BenchmarkRandomToken3(b *testing.B) {
|
|||
Token(3)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestPPID(t *testing.T) {
|
||||
for n := 0; n < 5; n++ {
|
||||
uuid := PPID('x')
|
||||
t.Logf("id: %s", uuid)
|
||||
assert.Equal(t, len(uuid), 17)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPPID(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
PPID('x')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue