2020-05-26 21:51:34 +02:00
|
|
|
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().
|
2020-05-29 12:56:24 +02:00
|
|
|
Where("place_id <> '' AND place_id <> 'zz'").
|
2020-05-26 21:51:34 +02:00
|
|
|
Order(gorm.Expr("ABS(DATEDIFF(taken_at, ?)) ASC", m.TakenAt)).
|
|
|
|
Preload("Place").First(&recentPhoto); result.Error == nil {
|
|
|
|
if recentPhoto.HasPlace() {
|
|
|
|
m.Place = recentPhoto.Place
|
2020-05-29 12:56:24 +02:00
|
|
|
m.PlaceID = recentPhoto.PlaceID
|
2020-05-26 21:51:34 +02:00
|
|
|
m.PhotoCountry = recentPhoto.PhotoCountry
|
|
|
|
m.LocSrc = SrcEstimate
|
2020-05-29 12:56:24 +02:00
|
|
|
log.Debugf("prism: approximate position of %s is %s", m.PhotoUID, recentPhoto.PlaceID)
|
2020-05-28 15:12:18 +02:00
|
|
|
} else if recentPhoto.HasCountry() {
|
|
|
|
m.PhotoCountry = recentPhoto.PhotoCountry
|
|
|
|
m.LocSrc = SrcEstimate
|
|
|
|
log.Debugf("prism: probable country for %s is %s", m.PhotoUID, recentPhoto.PhotoCountry)
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.UnknownCountry() && m.LocSrc == SrcAuto || m.UnknownLocation() && m.LocSrc == SrcEstimate {
|
2020-05-26 21:51:34 +02:00
|
|
|
m.EstimatePosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := m.ClassifyLabels()
|
|
|
|
|
|
|
|
m.UpdateYearMonth()
|
|
|
|
|
|
|
|
if err := m.UpdateTitle(labels); err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.DetailsLoaded() {
|
2020-05-28 15:12:18 +02:00
|
|
|
w := txt.UniqueWords(txt.Words(m.Details.Keywords))
|
2020-05-26 21:51:34 +02:00
|
|
|
w = append(w, labels.Keywords()...)
|
|
|
|
m.Details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.IndexKeywords(); err != nil {
|
2020-05-28 21:20:42 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoQuality = m.QualityScore()
|
|
|
|
|
|
|
|
return UnscopedDb().Save(m).Error
|
|
|
|
}
|