From 7ee4d5eda5a5beb38f5fd93652ff9d6effc49a64 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Fri, 15 May 2020 16:06:32 +0200 Subject: [PATCH] Photo: Update year and month whenever takenAt may change Signed-off-by: Michael Mayer --- internal/entity/photo.go | 25 +++++++++++++++++++++++++ internal/photoprism/index_mediafile.go | 8 +------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/entity/photo.go b/internal/entity/photo.go index 65618e06f..9c460d39c 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -75,6 +75,8 @@ func SavePhotoForm(model Photo, form form.Photo, geoApi string) error { return err } + model.UpdateYearMonth() + if form.Description.PhotoID == model.ID { if err := deepcopier.Copy(&model.Description).From(form.Description); err != nil { return err @@ -122,6 +124,8 @@ func (m *Photo) Save() error { db := Db() labels := m.ClassifyLabels() + m.UpdateYearMonth() + if err := m.UpdateTitle(labels); err != nil { log.Warnf("%s (%s)", err.Error(), m.PhotoUUID) } @@ -485,6 +489,27 @@ func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) { if zone != "" { m.TimeZone = zone } + + m.UpdateYearMonth() +} + +// UpdateYearMonth updates internal date fields. +func (m *Photo) UpdateYearMonth() { + if m.TakenAt.IsZero() || m.TakenAt.Year() < 1000 { + return + } + + if m.TakenAtLocal.IsZero() || m.TakenAtLocal.Year() < 1000 { + m.TakenAtLocal = m.TakenAt + } + + if m.TakenSrc == SrcAuto { + m.PhotoYear = YearUnknown + m.PhotoMonth = MonthUnknown + } else { + m.PhotoYear = m.TakenAtLocal.Year() + m.PhotoMonth = int(m.TakenAtLocal.Month()) + } } // SetCoordinates changes the photo lat, lng and altitude if not empty and from the same source. diff --git a/internal/photoprism/index_mediafile.go b/internal/photoprism/index_mediafile.go index 00d612e88..3e4609054 100644 --- a/internal/photoprism/index_mediafile.go +++ b/internal/photoprism/index_mediafile.go @@ -279,13 +279,7 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) ( photo.PhotoCountry = entity.UnknownPlace.CountryCode() } - if photo.TakenSrc == entity.SrcAuto { - photo.PhotoYear = entity.YearUnknown - photo.PhotoMonth = entity.MonthUnknown - } else if !photo.TakenAtLocal.IsZero() { - photo.PhotoYear = photo.TakenAtLocal.Year() - photo.PhotoMonth = int(photo.TakenAtLocal.Month()) - } + photo.UpdateYearMonth() if originalName != "" { file.OriginalName = originalName