2020-05-26 21:51:34 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-06-29 13:16:55 +02:00
|
|
|
"reflect"
|
2020-05-26 21:51:34 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-05-31 17:45:58 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-26 21:51:34 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2020-05-31 17:45:58 +02:00
|
|
|
// EstimateCountry updates the photo with an estimated country if possible.
|
|
|
|
func (m *Photo) EstimateCountry() {
|
2020-06-10 18:26:05 +02:00
|
|
|
if m.HasLatLng() || m.HasLocation() || m.HasPlace() || m.HasCountry() && m.LocationSrc != SrcAuto && m.LocationSrc != SrcEstimate {
|
2020-05-31 17:45:58 +02:00
|
|
|
// Do nothing.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
unknown := UnknownCountry.ID
|
|
|
|
countryCode := unknown
|
|
|
|
|
|
|
|
if code := txt.CountryCode(m.PhotoTitle); code != unknown {
|
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
if countryCode == unknown && !fs.IsID(m.PhotoName) {
|
2020-05-31 17:45:58 +02:00
|
|
|
if code := txt.CountryCode(m.PhotoName); code != unknown {
|
|
|
|
countryCode = code
|
|
|
|
} else if code := txt.CountryCode(m.PhotoPath); code != unknown {
|
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
if countryCode == unknown && m.OriginalName != "" && !fs.IsID(m.OriginalName) {
|
2020-05-31 17:45:58 +02:00
|
|
|
if code := txt.CountryCode(m.OriginalName); code != UnknownCountry.ID {
|
|
|
|
countryCode = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if countryCode != unknown {
|
|
|
|
m.PhotoCountry = countryCode
|
2020-06-10 18:26:05 +02:00
|
|
|
m.LocationSrc = SrcEstimate
|
2020-05-31 17:45:58 +02:00
|
|
|
log.Debugf("photo: probable country for %s is %s", m, txt.Quote(m.CountryName()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimatePlace updates the photo with an estimated place and country if possible.
|
|
|
|
func (m *Photo) EstimatePlace() {
|
2020-06-10 18:26:05 +02:00
|
|
|
if m.HasLatLng() || m.HasLocation() || m.HasPlace() && m.LocationSrc != SrcAuto && m.LocationSrc != SrcEstimate {
|
2020-05-31 17:45:58 +02:00
|
|
|
// Do nothing.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 21:51:34 +02:00
|
|
|
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-06-16 09:54:48 +02:00
|
|
|
Where("place_id <> '' AND place_id <> 'zz' AND location_src <> '' AND location_src <> ?", SrcEstimate).
|
2020-05-30 14:52:47 +02:00
|
|
|
Order(gorm.Expr(dateExpr, m.TakenAt)).
|
|
|
|
Preload("Place").First(&recentPhoto).Error; err != nil {
|
2020-05-31 17:45:58 +02:00
|
|
|
log.Errorf("photo: %s (estimate place)", err.Error())
|
|
|
|
m.EstimateCountry()
|
2020-05-30 14:52:47 +02:00
|
|
|
} else {
|
2020-07-07 13:18:34 +02:00
|
|
|
if days := recentPhoto.TakenAt.Sub(m.TakenAt) / (time.Hour * 24); days < -7 || days > 7 {
|
2020-05-31 17:45:58 +02:00
|
|
|
log.Debugf("photo: can't estimate position of %s, %d days time difference", m, days)
|
|
|
|
} else if recentPhoto.HasPlace() {
|
2020-05-26 21:51:34 +02:00
|
|
|
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
|
2020-06-10 18:26:05 +02:00
|
|
|
m.LocationSrc = SrcEstimate
|
2020-05-31 17:45:58 +02:00
|
|
|
log.Debugf("photo: approximate position of %s is %s (id %s)", m, txt.Quote(m.CountryName()), recentPhoto.PlaceID)
|
2020-05-28 15:12:18 +02:00
|
|
|
} else if recentPhoto.HasCountry() {
|
|
|
|
m.PhotoCountry = recentPhoto.PhotoCountry
|
2020-06-10 18:26:05 +02:00
|
|
|
m.LocationSrc = SrcEstimate
|
2020-05-31 17:45:58 +02:00
|
|
|
log.Debugf("photo: probable country for %s is %s", m, txt.Quote(m.CountryName()))
|
|
|
|
} else {
|
|
|
|
m.EstimateCountry()
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
// Optimize photo data, improve if possible.
|
|
|
|
func (m *Photo) Optimize() (updated bool, err error) {
|
2020-05-26 21:51:34 +02:00
|
|
|
if !m.HasID() {
|
2020-06-29 13:16:55 +02:00
|
|
|
return false, errors.New("photo: can't maintain, id is empty")
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
current := *m
|
2020-05-26 21:51:34 +02:00
|
|
|
|
2020-05-31 17:45:58 +02:00
|
|
|
m.EstimatePlace()
|
2020-05-26 21:51:34 +02:00
|
|
|
|
|
|
|
labels := m.ClassifyLabels()
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
m.UpdateDateFields()
|
2020-05-26 21:51:34 +02:00
|
|
|
|
|
|
|
if err := m.UpdateTitle(labels); err != nil {
|
2020-07-02 08:16:27 +02:00
|
|
|
log.Info(err)
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
details := m.GetDetails()
|
|
|
|
w := txt.UniqueWords(txt.Words(details.Keywords))
|
|
|
|
w = append(w, labels.Keywords()...)
|
|
|
|
details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
|
2020-05-26 21:51:34 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
checked := Timestamp()
|
|
|
|
|
|
|
|
if reflect.DeepEqual(*m, current) {
|
|
|
|
return false, m.Update("CheckedAt", &checked)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.CheckedAt = &checked
|
|
|
|
|
|
|
|
return true, UnscopedDb().Save(m).Error
|
2020-05-26 21:51:34 +02:00
|
|
|
}
|