photoprism/internal/entity/photo_maintenance.go
Michael Mayer a77b2431d3 Backend: Improve labels, keywords and caching
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-28 21:20:42 +02:00

68 lines
1.7 KiB
Go

package entity
import (
"errors"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/pkg/txt"
)
// EstimatePosition updates the photo with an estimated geolocation if possible.
func (m *Photo) EstimatePosition() {
var recentPhoto Photo
if result := UnscopedDb().
Where("place_uid <> '' && place_uid <> 'zz'").
Order(gorm.Expr("ABS(DATEDIFF(taken_at, ?)) ASC", m.TakenAt)).
Preload("Place").First(&recentPhoto); result.Error == nil {
if recentPhoto.HasPlace() {
m.Place = recentPhoto.Place
m.PlaceUID = recentPhoto.PlaceUID
m.PhotoCountry = recentPhoto.PhotoCountry
m.LocSrc = SrcEstimate
log.Debugf("prism: approximate position of %s is %s", m.PhotoUID, recentPhoto.PlaceUID)
} else if recentPhoto.HasCountry() {
m.PhotoCountry = recentPhoto.PhotoCountry
m.LocSrc = SrcEstimate
log.Debugf("prism: probable country for %s is %s", m.PhotoUID, recentPhoto.PhotoCountry)
}
}
}
// Maintain photo data, improve if possible.
func (m *Photo) Maintain() error {
if !m.HasID() {
return errors.New("photo: can't maintain, id is empty")
}
maintained := time.Now()
m.MaintainedAt = &maintained
if m.UnknownLocation() && (m.LocSrc == SrcAuto || m.LocSrc == SrcEstimate) {
m.EstimatePosition()
}
labels := m.ClassifyLabels()
m.UpdateYearMonth()
if err := m.UpdateTitle(labels); err != nil {
log.Warn(err)
}
if m.DetailsLoaded() {
w := txt.UniqueWords(txt.Words(m.Details.Keywords))
w = append(w, labels.Keywords()...)
m.Details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
}
if err := m.IndexKeywords(); err != nil {
log.Errorf("photo: %s", err.Error())
}
m.PhotoQuality = m.QualityScore()
return UnscopedDb().Save(m).Error
}