Skip to content

Commit

Permalink
Index: Check if YAML sidecar files contain a valid photo UID #4286
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Mayer <michael@photoprism.app>
  • Loading branch information
lastzero committed May 21, 2024
1 parent 885a42f commit e4df87e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
11 changes: 10 additions & 1 deletion internal/entity/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,16 @@ func (m *Photo) HasID() bool {
return false
}

return m.ID > 0 && m.PhotoUID != ""
return m.ID > 0 && m.HasUID()
}

// HasUID checks if the photo has a valid UID.
func (m *Photo) HasUID() bool {
if m == nil {
return false
}

return rnd.IsUID(m.PhotoUID, PhotoUID)
}

// GetUID returns the unique entity id.
Expand Down
15 changes: 14 additions & 1 deletion internal/entity/photo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,21 @@ func TestPhoto_SaveLabels(t *testing.T) {
})
}

func TestPhoto_HasUID(t *testing.T) {
t.Run("True", func(t *testing.T) {
m := PhotoFixtures.Get("Photo01")
assert.True(t, m.HasID())
assert.True(t, m.HasUID())
})
t.Run("False", func(t *testing.T) {
m := Photo{}
assert.False(t, m.HasID())
assert.False(t, m.HasUID())
})
}

func TestPhoto_GetID(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
m := PhotoFixtures.Get("Photo01")
assert.Equal(t, uint(1000001), m.GetID())
})
Expand Down
4 changes: 2 additions & 2 deletions internal/photoprism/index_mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func (ind *Index) UserMediaFile(m *MediaFile, o IndexOptions, originalName, phot
if yamlName := fs.SidecarYAML.FindFirst(m.FileName(), []string{Config().SidecarPath(), fs.PPHiddenPathname}, Config().OriginalsPath(), stripSequence); yamlName != "" {
if err = photo.LoadFromYaml(yamlName); err != nil {
log.Errorf("index: %s in %s (restore from yaml)", err.Error(), logName)
} else {
} else if photo.HasUID() {
photoExists = true
log.Infof("index: uid %s restored from %s", photo.PhotoUID, clean.Log(filepath.Base(yamlName)))
log.Infof("index: metadata of photo uid %s restored from %s", photo.PhotoUID, clean.Log(filepath.Base(yamlName)))
}
}
}
Expand Down

0 comments on commit e4df87e

Please sign in to comment.