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
|
2020-05-30 14:52:47 +02:00
|
|
|
var dateExpr string
|
2020-05-26 21:51:34 +02:00
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
dateExpr = "ABS(DATEDIFF(taken_at, ?)) ASC"
|
|
|
|
case SQLite:
|
|
|
|
dateExpr = "ABS(JulianDay(taken_at) - JulianDay(?)) ASC"
|
|
|
|
default:
|
|
|
|
log.Errorf("photo: unknown sql dialect %s", DbDialect())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().
|
2020-05-30 01:41:47 +02:00
|
|
|
Where("place_id <> '' AND place_id <> 'zz' AND loc_src <> '' AND loc_src <> ?", SrcEstimate).
|
2020-05-30 14:52:47 +02:00
|
|
|
Order(gorm.Expr(dateExpr, m.TakenAt)).
|
|
|
|
Preload("Place").First(&recentPhoto).Error; err != nil {
|
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
} else {
|
|
|
|
if days := recentPhoto.TakenAt.Sub(m.TakenAt) / (time.Hour * 24); days < -7 {
|
|
|
|
log.Debugf("prism: can't estimate position of %s, time difference too big (%d days)", m.PhotoUID, -1*days)
|
|
|
|
return
|
|
|
|
} else if days > -7 {
|
|
|
|
log.Debugf("prism: can't estimate position of %s, time difference too big (%d days)", m.PhotoUID, days)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 21:51:34 +02:00
|
|
|
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
|
|
|
|
}
|