photoprism/internal/entity/photo_optimize.go

64 lines
1.3 KiB
Go
Raw Normal View History

package entity
import (
"errors"
"reflect"
"strings"
"github.com/photoprism/photoprism/pkg/txt"
)
// Optimize photo data, improve if possible.
func (m *Photo) Optimize(mergeMeta, mergeUuid, estimatePlace bool) (updated bool, merged Photos, err error) {
if !m.HasID() {
return false, merged, errors.New("photo: can't maintain, id is empty")
}
current := *m
if m.HasLatLng() && !m.HasLocation() {
m.UpdateLocation()
}
2020-12-12 22:02:14 +01:00
if original, photos, err := m.Merge(mergeMeta, mergeUuid); err != nil {
return updated, merged, err
} else if len(photos) > 0 && original.ID == m.ID {
merged = photos
} else if len(photos) > 0 && original.ID != m.ID {
return false, photos, nil
2020-12-09 21:44:04 +01:00
}
if estimatePlace {
m.EstimatePlace()
}
labels := m.ClassifyLabels()
m.UpdateDateFields()
if err := m.UpdateTitle(labels); err != nil {
log.Info(err)
}
details := m.GetDetails()
w := txt.UniqueWords(txt.Words(details.Keywords))
w = append(w, labels.Keywords()...)
details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
if err := m.IndexKeywords(); err != nil {
log.Errorf("photo: %s", err.Error())
}
m.PhotoQuality = m.QualityScore()
checked := TimeStamp()
if reflect.DeepEqual(*m, current) {
return false, merged, m.Update("CheckedAt", &checked)
}
m.CheckedAt = &checked
return true, merged, m.Save()
}