diff --git a/internal/entity/album.go b/internal/entity/album.go index 896247edb..1003c2fec 100644 --- a/internal/entity/album.go +++ b/internal/entity/album.go @@ -32,11 +32,11 @@ type Album struct { // BeforeCreate computes a random UUID when a new album is created in database func (m *Album) BeforeCreate(scope *gorm.Scope) error { - if err := scope.SetColumn("AlbumUUID", rnd.PPID('a')); err != nil { - return err + if rnd.IsPPID(m.AlbumUUID, 'a') { + return nil } - return nil + return scope.SetColumn("AlbumUUID", rnd.PPID('a')) } // NewAlbum creates a new album; default name is current month and year diff --git a/internal/entity/file.go b/internal/entity/file.go index ea4b1f20a..572b0c469 100644 --- a/internal/entity/file.go +++ b/internal/entity/file.go @@ -62,6 +62,10 @@ func FirstFileByHash(fileHash string) (File, error) { // BeforeCreate computes a random UUID when a new file is created in database func (m *File) BeforeCreate(scope *gorm.Scope) error { + if rnd.IsPPID(m.FileUUID, 'f') { + return nil + } + return scope.SetColumn("FileUUID", rnd.PPID('f')) } diff --git a/internal/entity/label.go b/internal/entity/label.go index 61279a6fd..2ecdb2068 100644 --- a/internal/entity/label.go +++ b/internal/entity/label.go @@ -31,12 +31,11 @@ type Label struct { // BeforeCreate computes a random UUID when a new label is created in database func (m *Label) BeforeCreate(scope *gorm.Scope) error { - if err := scope.SetColumn("LabelUUID", rnd.PPID('l')); err != nil { - log.Errorf("label: %s", err) - return err + if rnd.IsPPID(m.LabelUUID, 'l') { + return nil } - return nil + return scope.SetColumn("LabelUUID", rnd.PPID('l')) } // NewLabel creates a label in database with a given name and priority diff --git a/internal/entity/photo.go b/internal/entity/photo.go index a29116ee4..ed8b1c5dd 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -146,10 +146,6 @@ func (m *Photo) ClassifyLabels() classify.Labels { // BeforeCreate computes a unique UUID, and set a default takenAt before indexing a new photo func (m *Photo) BeforeCreate(scope *gorm.Scope) error { - if err := scope.SetColumn("PhotoUUID", rnd.PPID('p')); err != nil { - return err - } - if m.TakenAt.IsZero() || m.TakenAtLocal.IsZero() { now := time.Now() @@ -162,7 +158,11 @@ func (m *Photo) BeforeCreate(scope *gorm.Scope) error { } } - return nil + if rnd.IsPPID(m.PhotoUUID, 'p') { + return nil + } + + return scope.SetColumn("PhotoUUID", rnd.PPID('p')) } // BeforeSave ensures the existence of TakenAt properties before indexing or updating a photo diff --git a/pkg/rnd/ppid.go b/pkg/rnd/ppid.go index 571c9d727..79cf963ad 100644 --- a/pkg/rnd/ppid.go +++ b/pkg/rnd/ppid.go @@ -6,11 +6,20 @@ import ( ) // PPID returns a unique id with prefix as string. -func PPID(prefix rune) string { +func PPID(prefix byte) string { result := make([]byte, 0, 16) - result = append(result, byte(prefix)) + 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 the id seems to be a PhotoPrism unique id. +func IsPPID(id string, prefix byte) bool { + if len(id) != 16 { + return false + } + + return id[0] == prefix +} diff --git a/pkg/rnd/ppid_test.go b/pkg/rnd/ppid_test.go new file mode 100644 index 000000000..7d870f001 --- /dev/null +++ b/pkg/rnd/ppid_test.go @@ -0,0 +1,32 @@ +package rnd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPPID(t *testing.T) { + for n := 0; n < 5; n++ { + uuid := PPID('x') + t.Logf("id: %s", uuid) + assert.Equal(t, len(uuid), 16) + } +} + +func BenchmarkPPID(b *testing.B) { + for n := 0; n < b.N; n++ { + PPID('x') + } +} + +func TestIsPPID(t *testing.T) { + prefix := byte('x') + + for n := 0; n < 10; n++ { + id := PPID(prefix) + assert.True(t, IsPPID(id, prefix)) + } + + assert.True(t, IsPPID("lt9k3pw1wowuy3c2", 'l')) +} diff --git a/pkg/rnd/token_test.go b/pkg/rnd/token_test.go index 741c0bf3d..870561bde 100644 --- a/pkg/rnd/token_test.go +++ b/pkg/rnd/token_test.go @@ -54,17 +54,3 @@ 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), 16) - } -} - -func BenchmarkPPID(b *testing.B) { - for n := 0; n < b.N; n++ { - PPID('x') - } -}