2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-07-18 15:17:56 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-18 23:20:54 +02:00
|
|
|
"errors"
|
2020-04-17 14:16:34 +02:00
|
|
|
"fmt"
|
2020-05-31 14:42:41 +02:00
|
|
|
"path"
|
2020-03-26 09:41:33 +01:00
|
|
|
"strings"
|
2018-08-09 23:10:05 +02:00
|
|
|
"time"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-04-16 20:57:00 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2020-05-26 13:43:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-05-31 14:42:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/ulule/deepcopier"
|
2018-07-18 15:17:56 +02:00
|
|
|
)
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
type Photos []Photo
|
|
|
|
|
2020-06-14 11:39:53 +02:00
|
|
|
// UIDs returns a slice of photo UIDs.
|
2020-06-01 09:45:24 +02:00
|
|
|
func (m Photos) UIDs() []string {
|
|
|
|
result := make([]string, len(m))
|
|
|
|
|
|
|
|
for i, el := range m {
|
|
|
|
result[i] = el.PhotoUID
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Photo represents a photo, all its properties, and link to all its images and sidecar files.
|
2018-07-18 15:17:56 +02:00
|
|
|
type Photo struct {
|
2020-05-18 22:18:58 +02:00
|
|
|
ID uint `gorm:"primary_key" yaml:"-"`
|
2020-06-09 19:40:32 +02:00
|
|
|
UUID string `gorm:"type:varbinary(42);index;" json:"DocumentID,omitempty" yaml:"DocumentID,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
TakenAt time.Time `gorm:"type:datetime;index:idx_photos_taken_uid;" json:"TakenAt" yaml:"TakenAt"`
|
2020-05-18 22:18:58 +02:00
|
|
|
TakenAtLocal time.Time `gorm:"type:datetime;" yaml:"-"`
|
|
|
|
TakenSrc string `gorm:"type:varbinary(8);" json:"TakenSrc" yaml:"TakenSrc,omitempty"`
|
2020-06-09 19:40:32 +02:00
|
|
|
PhotoUID string `gorm:"type:varbinary(42);unique_index;index:idx_photos_taken_uid;" json:"UID" yaml:"UID"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoType string `gorm:"type:varbinary(8);default:'image';" json:"Type" yaml:"Type"`
|
|
|
|
PhotoTitle string `gorm:"type:varchar(255);" json:"Title" yaml:"Title"`
|
2020-05-18 22:18:58 +02:00
|
|
|
TitleSrc string `gorm:"type:varbinary(8);" json:"TitleSrc" yaml:"TitleSrc,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoDescription string `gorm:"type:text;" json:"Description" yaml:"Description,omitempty"`
|
2020-05-18 22:18:58 +02:00
|
|
|
DescriptionSrc string `gorm:"type:varbinary(8);" json:"DescriptionSrc" yaml:"DescriptionSrc,omitempty"`
|
|
|
|
Details Details `json:"Details" yaml:"Details"`
|
2020-06-01 09:45:24 +02:00
|
|
|
PhotoPath string `gorm:"type:varbinary(768);index;" json:"Path" yaml:"-"`
|
|
|
|
PhotoName string `gorm:"type:varbinary(255);" json:"Name" yaml:"-"`
|
2020-05-31 14:42:41 +02:00
|
|
|
OriginalName string `gorm:"type:varbinary(768);" json:"OriginalName" yaml:"OriginalName,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoFavorite bool `json:"Favorite" yaml:"Favorite,omitempty"`
|
|
|
|
PhotoPrivate bool `json:"Private" yaml:"Private,omitempty"`
|
2020-07-06 14:35:25 +02:00
|
|
|
PhotoScan bool `json:"Scan" yaml:"Scan,omitempty"`
|
2020-05-18 22:18:58 +02:00
|
|
|
TimeZone string `gorm:"type:varbinary(64);" json:"TimeZone" yaml:"-"`
|
2020-06-09 19:40:32 +02:00
|
|
|
PlaceID string `gorm:"type:varbinary(42);index;" json:"PlaceID" yaml:"-"`
|
|
|
|
LocationID string `gorm:"type:varbinary(42);index;" json:"LocationID" yaml:"-"`
|
2020-06-10 18:26:05 +02:00
|
|
|
LocationSrc string `gorm:"type:varbinary(8);" json:"LocationSrc" yaml:"LocationSrc,omitempty"`
|
2020-07-06 10:32:37 +02:00
|
|
|
GPSAccuracy int `gorm:"column:gps_accuracy" json:"GPSAccuracy" yaml:"GPSAccuracy,omitempty"`
|
|
|
|
PhotoAltitude int `json:"Altitude" yaml:"Altitude,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoLat float32 `gorm:"type:FLOAT;index;" json:"Lat" yaml:"Lat,omitempty"`
|
|
|
|
PhotoLng float32 `gorm:"type:FLOAT;index;" json:"Lng" yaml:"Lng,omitempty"`
|
|
|
|
PhotoCountry string `gorm:"type:varbinary(2);index:idx_photos_country_year_month;default:'zz'" json:"Country" yaml:"-"`
|
2020-07-06 07:41:33 +02:00
|
|
|
PhotoYear int `gorm:"index:idx_photos_country_year_month;" json:"Year" yaml:"Year"`
|
|
|
|
PhotoMonth int `gorm:"index:idx_photos_country_year_month;" json:"Month" yaml:"Month"`
|
|
|
|
PhotoDay int `json:"Day" yaml:"Day"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoIso int `json:"Iso" yaml:"ISO,omitempty"`
|
|
|
|
PhotoExposure string `gorm:"type:varbinary(64);" json:"Exposure" yaml:"Exposure,omitempty"`
|
|
|
|
PhotoFNumber float32 `gorm:"type:FLOAT;" json:"FNumber" yaml:"FNumber,omitempty"`
|
|
|
|
PhotoFocalLength int `json:"FocalLength" yaml:"FocalLength,omitempty"`
|
|
|
|
PhotoQuality int `gorm:"type:SMALLINT" json:"Quality" yaml:"-"`
|
|
|
|
PhotoResolution int `gorm:"type:SMALLINT" json:"Resolution" yaml:"-"`
|
2020-05-18 22:18:58 +02:00
|
|
|
CameraID uint `gorm:"index:idx_photos_camera_lens;" json:"CameraID" yaml:"-"`
|
|
|
|
CameraSerial string `gorm:"type:varbinary(255);" json:"CameraSerial" yaml:"CameraSerial,omitempty"`
|
|
|
|
CameraSrc string `gorm:"type:varbinary(8);" json:"CameraSrc" yaml:"-"`
|
|
|
|
LensID uint `gorm:"index:idx_photos_camera_lens;" json:"LensID" yaml:"-"`
|
2020-05-26 12:46:22 +02:00
|
|
|
Camera *Camera `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Camera" yaml:"-"`
|
|
|
|
Lens *Lens `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Lens" yaml:"-"`
|
2020-05-29 12:56:24 +02:00
|
|
|
Location *Location `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Location" yaml:"-"`
|
|
|
|
Place *Place `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Place" yaml:"-"`
|
2020-05-18 22:18:58 +02:00
|
|
|
Keywords []Keyword `json:"-" yaml:"-"`
|
2020-05-19 09:44:49 +02:00
|
|
|
Albums []Album `json:"-" yaml:"-"`
|
2020-05-18 22:18:58 +02:00
|
|
|
Files []File `yaml:"-"`
|
|
|
|
Labels []PhotoLabel `yaml:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CreatedAt time.Time `yaml:"CreatedAt,omitempty"`
|
|
|
|
UpdatedAt time.Time `yaml:"UpdatedAt,omitempty"`
|
|
|
|
EditedAt *time.Time `yaml:"EditedAt,omitempty"`
|
2020-06-24 08:34:23 +02:00
|
|
|
CheckedAt *time.Time `sql:"index" yaml:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
DeletedAt *time.Time `sql:"index" yaml:"DeletedAt,omitempty"`
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
2020-06-04 17:06:42 +02:00
|
|
|
// NewPhoto creates a photo entity.
|
|
|
|
func NewPhoto() Photo {
|
|
|
|
return Photo{
|
|
|
|
PhotoType: TypeImage,
|
|
|
|
PhotoCountry: UnknownCountry.ID,
|
|
|
|
CameraID: UnknownCamera.ID,
|
|
|
|
LensID: UnknownLens.ID,
|
|
|
|
LocationID: UnknownLocation.ID,
|
|
|
|
PlaceID: UnknownPlace.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
// SavePhotoForm saves a model in the database using form data.
|
2020-04-30 20:07:03 +02:00
|
|
|
func SavePhotoForm(model Photo, form form.Photo, geoApi string) error {
|
2020-05-28 15:12:18 +02:00
|
|
|
locChanged := model.PhotoLat != form.PhotoLat || model.PhotoLng != form.PhotoLng || model.PhotoCountry != form.PhotoCountry
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-02-02 03:36:00 +01:00
|
|
|
if err := deepcopier.Copy(&model).From(form); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:48:32 +02:00
|
|
|
if !model.HasID() {
|
|
|
|
return errors.New("photo: can't save form, id is empty")
|
|
|
|
}
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
model.UpdateDateFields()
|
2020-05-15 16:06:32 +02:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
if form.Details.PhotoID == model.ID {
|
|
|
|
if err := deepcopier.Copy(&model.Details).From(form.Details); err != nil {
|
2020-03-26 09:41:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
model.Details.Keywords = strings.Join(txt.UniqueWords(txt.Words(model.Details.Keywords)), ", ")
|
2020-03-26 09:41:33 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 18:26:05 +02:00
|
|
|
if locChanged && model.LocationSrc == SrcManual {
|
2020-04-30 20:07:03 +02:00
|
|
|
locKeywords, labels := model.UpdateLocation(geoApi)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
model.AddLabels(labels)
|
2020-04-16 20:57:00 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
w := txt.UniqueWords(txt.Words(model.Details.Keywords))
|
2020-04-16 20:57:00 +02:00
|
|
|
w = append(w, locKeywords...)
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
model.Details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
if err := model.SyncKeywordLabels(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
if err := model.UpdateTitle(model.ClassifyLabels()); err != nil {
|
2020-07-02 08:16:27 +02:00
|
|
|
log.Info(err)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := model.IndexKeywords(); err != nil {
|
2020-05-28 21:20:42 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
edited := Timestamp()
|
2020-04-25 14:22:47 +02:00
|
|
|
model.EditedAt = &edited
|
2020-04-24 13:21:18 +02:00
|
|
|
model.PhotoQuality = model.QualityScore()
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if err := UnscopedDb().Save(&model).Error; err != nil {
|
2020-05-11 11:01:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UpdatePhotoCounts(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
// String returns the id or name as string.
|
2020-05-31 17:45:58 +02:00
|
|
|
func (m *Photo) String() string {
|
|
|
|
if m.PhotoUID == "" {
|
|
|
|
if m.PhotoName != "" {
|
|
|
|
return txt.Quote(m.PhotoName)
|
|
|
|
} else if m.OriginalName != "" {
|
|
|
|
return txt.Quote(m.OriginalName)
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
return "(unknown)"
|
2020-05-31 17:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "uid " + txt.Quote(m.PhotoUID)
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:48:32 +02:00
|
|
|
// Save the entity in the database.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) Save() error {
|
2020-05-19 12:48:32 +02:00
|
|
|
if !m.HasID() {
|
|
|
|
return errors.New("photo: can't save to database, id is empty")
|
|
|
|
}
|
|
|
|
|
2020-04-16 20:57:00 +02:00
|
|
|
labels := m.ClassifyLabels()
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
m.UpdateDateFields()
|
2020-05-15 16:06:32 +02:00
|
|
|
|
2020-04-16 20:57:00 +02:00
|
|
|
if err := m.UpdateTitle(labels); err != nil {
|
2020-07-02 08:16:27 +02:00
|
|
|
log.Info(err)
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
if m.DetailsLoaded() {
|
2020-05-28 15:12:18 +02:00
|
|
|
w := txt.UniqueWords(txt.Words(m.Details.Keywords))
|
2020-04-16 20:57:00 +02:00
|
|
|
w = append(w, labels.Keywords()...)
|
2020-05-18 22:18:58 +02:00
|
|
|
m.Details.Keywords = strings.Join(txt.UniqueWords(w), ", ")
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := m.IndexKeywords(); err != nil {
|
2020-05-28 21:20:42 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-17 14:16:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:21:18 +02:00
|
|
|
m.PhotoQuality = m.QualityScore()
|
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
if err := UnscopedDb().Save(m).Error; err != nil {
|
2020-05-11 11:01:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UpdatePhotoCounts(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:16:34 +02:00
|
|
|
// ClassifyLabels returns all associated labels as classify.Labels
|
|
|
|
func (m *Photo) ClassifyLabels() classify.Labels {
|
|
|
|
result := classify.Labels{}
|
|
|
|
|
|
|
|
for _, l := range m.Labels {
|
2020-05-06 10:35:41 +02:00
|
|
|
if l.Label == nil {
|
|
|
|
log.Warnf("photo: empty reference while creating classify labels (%d -> %d)", l.PhotoID, l.LabelID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-17 14:16:34 +02:00
|
|
|
result = append(result, l.ClassifyLabel())
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Photo) BeforeCreate(scope *gorm.Scope) error {
|
2020-01-06 17:50:05 +01:00
|
|
|
if m.TakenAt.IsZero() || m.TakenAtLocal.IsZero() {
|
2020-06-26 12:16:13 +02:00
|
|
|
now := Timestamp()
|
2020-01-06 17:50:05 +01:00
|
|
|
|
|
|
|
if err := scope.SetColumn("TakenAt", now); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:27:39 +01:00
|
|
|
if err := scope.SetColumn("TakenAtLocal", now); err != nil {
|
2020-01-06 17:50:05 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
if rnd.IsUID(m.PhotoUID, 'p') {
|
2020-05-01 12:57:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
return scope.SetColumn("PhotoUID", rnd.PPID('p'))
|
2020-01-06 17:50:05 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// BeforeSave ensures the existence of TakenAt properties before indexing or updating a photo
|
2020-01-06 17:50:05 +01:00
|
|
|
func (m *Photo) BeforeSave(scope *gorm.Scope) error {
|
|
|
|
if m.TakenAt.IsZero() || m.TakenAtLocal.IsZero() {
|
2020-06-26 12:16:13 +02:00
|
|
|
now := Timestamp()
|
2020-01-06 17:50:05 +01:00
|
|
|
|
|
|
|
if err := scope.SetColumn("TakenAt", now); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:27:39 +01:00
|
|
|
if err := scope.SetColumn("TakenAtLocal", now); err != nil {
|
2020-01-06 17:50:05 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 10:26:57 +01:00
|
|
|
return nil
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
// RemoveKeyword removes a word from photo keywords.
|
|
|
|
func (m *Photo) RemoveKeyword(w string) error {
|
|
|
|
if !m.DetailsLoaded() {
|
|
|
|
return fmt.Errorf("can't remove keyword, details not loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
words := txt.RemoveFromWords(txt.Words(m.Details.Keywords), w)
|
|
|
|
|
|
|
|
m.Details.Keywords = strings.Join(words, ", ")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncKeywordLabels maintains the label / photo relationship for existing labels and keywords.
|
|
|
|
func (m *Photo) SyncKeywordLabels() error {
|
|
|
|
keywords := txt.UniqueKeywords(m.Details.Keywords)
|
|
|
|
|
|
|
|
var labelIds []uint
|
|
|
|
|
|
|
|
for _, w := range keywords {
|
|
|
|
if label := FindLabel(w); label != nil {
|
2020-06-02 17:57:12 +02:00
|
|
|
if label.Deleted() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
labelIds = append(labelIds, label.ID)
|
2020-05-29 18:04:30 +02:00
|
|
|
FirstOrCreatePhotoLabel(NewPhotoLabel(m.ID, label.ID, 25, classify.SrcKeyword))
|
2020-05-28 21:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Where("label_src = ? AND photo_id = ? AND label_id NOT IN (?)", classify.SrcKeyword, m.ID, labelIds).Delete(&PhotoLabel{}).Error
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// IndexKeywords adds given keywords to the photo entry
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) IndexKeywords() error {
|
2020-05-18 22:18:58 +02:00
|
|
|
if !m.DetailsLoaded() {
|
2020-05-28 21:20:42 +02:00
|
|
|
return fmt.Errorf("can't index keywords, details not loaded")
|
2020-04-17 14:16:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
db := Db()
|
|
|
|
|
2019-12-08 15:05:35 +01:00
|
|
|
var keywordIds []uint
|
2020-03-25 14:14:00 +01:00
|
|
|
var keywords []string
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-02-07 17:02:53 +01:00
|
|
|
// Add title, description and other keywords
|
2020-01-07 17:36:49 +01:00
|
|
|
keywords = append(keywords, txt.Keywords(m.PhotoTitle)...)
|
2020-05-18 22:18:58 +02:00
|
|
|
keywords = append(keywords, txt.Keywords(m.PhotoDescription)...)
|
|
|
|
keywords = append(keywords, txt.Keywords(m.Details.Keywords)...)
|
|
|
|
keywords = append(keywords, txt.Keywords(m.Details.Subject)...)
|
|
|
|
keywords = append(keywords, txt.Keywords(m.Details.Artist)...)
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-03-25 12:39:07 +01:00
|
|
|
keywords = txt.UniqueWords(keywords)
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
for _, w := range keywords {
|
2020-05-26 11:00:39 +02:00
|
|
|
kw := FirstOrCreateKeyword(NewKeyword(w))
|
|
|
|
|
|
|
|
if kw == nil {
|
2020-05-28 21:20:42 +02:00
|
|
|
log.Errorf("index keyword should not be nil - bug?")
|
2020-05-26 11:00:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
if kw.Skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keywordIds = append(keywordIds, kw.ID)
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
FirstOrCreatePhotoKeyword(NewPhotoKeyword(m.ID, kw.ID))
|
2019-12-08 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:16:34 +02:00
|
|
|
return db.Where("photo_id = ? AND keyword_id NOT IN (?)", m.ID, keywordIds).Delete(&PhotoKeyword{}).Error
|
2019-12-08 15:05:35 +01:00
|
|
|
}
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PreloadFiles prepares gorm scope to retrieve photo file
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) PreloadFiles() {
|
|
|
|
q := Db().NewScope(nil).DB().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("files").
|
|
|
|
Select(`files.*`).
|
|
|
|
Where("files.photo_id = ?", m.ID).
|
2020-04-21 10:23:27 +02:00
|
|
|
Order("files.file_name DESC")
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
logError(q.Scan(&m.Files))
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
/* func (m *Photo) PreloadLabels() {
|
|
|
|
q := Db().NewScope(nil).DB().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("labels").
|
|
|
|
Select(`labels.*`).
|
|
|
|
Joins("JOIN photos_labels ON photos_labels.label_id = labels.id AND photos_labels.photo_id = ?", m.ID).
|
|
|
|
Where("labels.deleted_at IS NULL").
|
|
|
|
Order("labels.label_name ASC")
|
|
|
|
|
|
|
|
logError(q.Scan(&m.Labels))
|
2020-01-29 15:28:20 +01:00
|
|
|
} */
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PreloadKeywords prepares gorm scope to retrieve photo keywords
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) PreloadKeywords() {
|
|
|
|
q := Db().NewScope(nil).DB().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("keywords").
|
|
|
|
Select(`keywords.*`).
|
|
|
|
Joins("JOIN photos_keywords ON photos_keywords.keyword_id = keywords.id AND photos_keywords.photo_id = ?", m.ID).
|
|
|
|
Order("keywords.keyword ASC")
|
|
|
|
|
|
|
|
logError(q.Scan(&m.Keywords))
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PreloadAlbums prepares gorm scope to retrieve photo albums
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) PreloadAlbums() {
|
|
|
|
q := Db().NewScope(nil).DB().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("albums").
|
|
|
|
Select(`albums.*`).
|
2020-05-23 20:58:58 +02:00
|
|
|
Joins("JOIN photos_albums ON photos_albums.album_uid = albums.album_uid AND photos_albums.photo_uid = ?", m.PhotoUID).
|
2019-12-11 19:11:44 +01:00
|
|
|
Where("albums.deleted_at IS NULL").
|
2020-05-26 09:02:19 +02:00
|
|
|
Order("albums.album_title ASC")
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
logError(q.Scan(&m.Albums))
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PreloadMany prepares gorm scope to retrieve photo file, albums and keywords
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) PreloadMany() {
|
|
|
|
m.PreloadFiles()
|
|
|
|
// m.PreloadLabels()
|
|
|
|
m.PreloadKeywords()
|
|
|
|
m.PreloadAlbums()
|
2019-12-11 19:11:44 +01:00
|
|
|
}
|
2019-12-28 12:28:06 +01:00
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// HasID checks if the photo has a database id and uid.
|
2020-05-19 12:48:32 +02:00
|
|
|
func (m *Photo) HasID() bool {
|
2020-05-23 20:58:58 +02:00
|
|
|
return m.ID > 0 && m.PhotoUID != ""
|
2020-05-19 12:48:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// UnknownLocation checks if the photo has an unknown location.
|
|
|
|
func (m *Photo) UnknownLocation() bool {
|
2020-05-29 12:56:24 +02:00
|
|
|
return m.LocationID == "" || m.LocationID == UnknownLocation.ID
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// HasLocation checks if the photo has a known location.
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Photo) HasLocation() bool {
|
2020-05-28 15:12:18 +02:00
|
|
|
return !m.UnknownLocation()
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocationLoaded checks if the photo has a known location that is currently loaded.
|
|
|
|
func (m *Photo) LocationLoaded() bool {
|
2020-06-04 22:05:24 +02:00
|
|
|
if m.Location == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Location.Place == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !m.Location.Unknown() && m.Location.ID == m.LocationID
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadLocation loads the photo location from the database if not done already.
|
|
|
|
func (m *Photo) LoadLocation() error {
|
|
|
|
if m.LocationLoaded() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
if m.UnknownLocation() {
|
|
|
|
return fmt.Errorf("photo: unknown location (%s)", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
var location Location
|
2020-06-04 22:05:24 +02:00
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
err := Db().Preload("Place").First(&location, "id = ?", m.LocationID).Error
|
2020-06-04 22:05:24 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
if location.Place == nil {
|
|
|
|
location.Place = &UnknownPlace
|
|
|
|
location.PlaceID = UnknownPlace.ID
|
2020-06-04 22:05:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
m.Location = &location
|
|
|
|
|
2020-06-04 22:05:24 +02:00
|
|
|
return nil
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PlaceLoaded checks if the photo has a known place that is currently loaded.
|
|
|
|
func (m *Photo) PlaceLoaded() bool {
|
2020-06-04 22:05:24 +02:00
|
|
|
if m.Place == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !m.Place.Unknown() && m.Place.ID == m.PlaceID
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPlace loads the photo place from the database if not done already.
|
|
|
|
func (m *Photo) LoadPlace() error {
|
|
|
|
if m.PlaceLoaded() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
if m.UnknownPlace() {
|
|
|
|
return fmt.Errorf("photo: unknown place (%s)", m)
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
var place Place
|
2020-06-04 14:56:27 +02:00
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
err := Db().First(&place, "id = ?", m.PlaceID).Error
|
2020-06-04 14:56:27 +02:00
|
|
|
|
2020-06-04 22:05:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-04 14:56:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:26:00 +02:00
|
|
|
m.Place = &place
|
2020-06-04 14:56:27 +02:00
|
|
|
|
2020-06-04 22:05:24 +02:00
|
|
|
return nil
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:05:22 +02:00
|
|
|
// HasLatLng checks if the photo has a latitude and longitude.
|
|
|
|
func (m *Photo) HasLatLng() bool {
|
2020-06-05 10:17:00 +02:00
|
|
|
return m.PhotoLat != 0.0 || m.PhotoLng != 0.0
|
2020-04-16 18:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NoLatLng checks if latitude and longitude are missing.
|
|
|
|
func (m *Photo) NoLatLng() bool {
|
|
|
|
return !m.HasLatLng()
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
// UnknownPlace checks if the photo has an unknown place.
|
|
|
|
func (m *Photo) UnknownPlace() bool {
|
2020-05-29 12:56:24 +02:00
|
|
|
return m.PlaceID == "" || m.PlaceID == UnknownPlace.ID
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// HasPlace checks if the photo has a known place.
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Photo) HasPlace() bool {
|
2020-05-28 15:12:18 +02:00
|
|
|
return !m.UnknownPlace()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasCountry checks if the photo has a known country.
|
|
|
|
func (m *Photo) HasCountry() bool {
|
|
|
|
return !m.UnknownCountry()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnknownCountry checks if the photo has an unknown country.
|
|
|
|
func (m *Photo) UnknownCountry() bool {
|
2020-05-31 17:45:58 +02:00
|
|
|
return m.CountryCode() == UnknownCountry.ID
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoTitle checks if the photo has no Title
|
2019-12-28 12:28:06 +01:00
|
|
|
func (m *Photo) NoTitle() bool {
|
|
|
|
return m.PhotoTitle == ""
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NoCameraSerial checks if the photo has no CameraSerial
|
2020-02-07 17:02:53 +01:00
|
|
|
func (m *Photo) NoCameraSerial() bool {
|
|
|
|
return m.CameraSerial == ""
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:21:10 +02:00
|
|
|
// HasTitle checks if the photo has a title.
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Photo) HasTitle() bool {
|
|
|
|
return m.PhotoTitle != ""
|
|
|
|
}
|
2020-04-17 14:16:34 +02:00
|
|
|
|
2020-05-19 09:21:10 +02:00
|
|
|
// HasDescription checks if the photo has a description.
|
|
|
|
func (m *Photo) HasDescription() bool {
|
|
|
|
return m.PhotoDescription != ""
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// DetailsLoaded returns true if photo details exist.
|
|
|
|
func (m *Photo) DetailsLoaded() bool {
|
|
|
|
return m.Details.PhotoID == m.ID
|
2020-04-17 14:16:34 +02:00
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-07-06 11:31:03 +02:00
|
|
|
// FileTitle returns a photo title based on the file name and/or path.
|
|
|
|
func (m *Photo) FileTitle() string {
|
2020-06-29 11:10:24 +02:00
|
|
|
if !fs.IsID(m.PhotoName) {
|
2020-07-06 11:31:03 +02:00
|
|
|
if title := txt.FileTitle(m.PhotoName); title != "" {
|
2020-05-31 14:42:41 +02:00
|
|
|
return title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
if m.OriginalName != "" && !fs.IsID(m.OriginalName) {
|
2020-07-06 11:31:03 +02:00
|
|
|
if title := txt.FileTitle(m.OriginalName); title != "" {
|
2020-05-31 14:42:41 +02:00
|
|
|
return title
|
2020-07-06 11:31:03 +02:00
|
|
|
} else if title := txt.FileTitle(path.Dir(m.OriginalName)); title != "" {
|
2020-05-31 14:42:41 +02:00
|
|
|
return title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.PhotoPath != "" {
|
2020-07-06 11:31:03 +02:00
|
|
|
return txt.FileTitle(m.PhotoPath)
|
2020-05-31 14:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// UpdateTitle updated the photo title based on location and labels.
|
|
|
|
func (m *Photo) UpdateTitle(labels classify.Labels) error {
|
|
|
|
if m.TitleSrc != SrcAuto && m.HasTitle() {
|
2020-05-26 21:51:34 +02:00
|
|
|
return fmt.Errorf("photo: won't update title, %s was modified", m.PhotoUID)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 21:51:34 +02:00
|
|
|
var knownLocation bool
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
oldTitle := m.PhotoTitle
|
2020-07-06 11:31:03 +02:00
|
|
|
fileTitle := m.FileTitle()
|
2020-05-28 15:12:18 +02:00
|
|
|
|
2020-05-26 21:51:34 +02:00
|
|
|
if m.LocationLoaded() {
|
|
|
|
knownLocation = true
|
2020-04-18 23:20:54 +02:00
|
|
|
loc := m.Location
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
// TODO: User defined title format
|
|
|
|
if title := labels.Title(loc.Name()); title != "" {
|
2020-06-29 13:16:55 +02:00
|
|
|
log.Debugf("photo: using label %s to create title for %s", txt.Quote(title), m.PhotoUID)
|
2020-04-18 23:20:54 +02:00
|
|
|
if loc.NoCity() || loc.LongCity() || loc.CityContains(title) {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", txt.Title(title), loc.CountryName(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
} else {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", txt.Title(title), loc.City(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
} else if loc.Name() != "" && loc.City() != "" {
|
|
|
|
if len(loc.Name()) > 45 {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(txt.Title(loc.Name()), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
} else if len(loc.Name()) > 20 || len(loc.City()) > 16 || strings.Contains(loc.Name(), loc.City()) {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", loc.Name(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
} else {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", loc.Name(), loc.City(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
} else if loc.City() != "" && loc.CountryName() != "" {
|
|
|
|
if len(loc.City()) > 20 {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", loc.City(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
} else {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", loc.City(), loc.CountryName(), m.TakenAt.Format("2006")), SrcAuto)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 21:51:34 +02:00
|
|
|
} else if m.PlaceLoaded() {
|
|
|
|
knownLocation = true
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if title := labels.Title(fileTitle); title != "" {
|
2020-06-29 13:16:55 +02:00
|
|
|
log.Debugf("photo: using label %s to create title for %s", txt.Quote(title), m.PhotoUID)
|
2020-05-26 21:51:34 +02:00
|
|
|
if m.Place.NoCity() || m.Place.LongCity() || m.Place.CityContains(title) {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", txt.Title(title), m.Place.CountryName(), m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
} else {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", txt.Title(title), m.Place.City(), m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
}
|
|
|
|
} else if m.Place.City() != "" && m.Place.CountryName() != "" {
|
|
|
|
if len(m.Place.City()) > 20 {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", m.Place.City(), m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
} else {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s / %s", m.Place.City(), m.Place.CountryName(), m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
}
|
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if !knownLocation || m.NoTitle() {
|
2020-06-23 11:12:24 +02:00
|
|
|
if fileTitle == "" && len(labels) > 0 && labels[0].Priority >= -1 && labels[0].Uncertainty <= 85 && labels[0].Name != "" {
|
2020-05-15 15:29:56 +02:00
|
|
|
if m.TakenSrc != SrcAuto {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", txt.Title(labels[0].Name), m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
} else {
|
|
|
|
m.SetTitle(txt.Title(labels[0].Name), SrcAuto)
|
|
|
|
}
|
2020-06-23 11:12:24 +02:00
|
|
|
} else if fileTitle != "" && len(fileTitle) <= 20 && !m.TakenAtLocal.IsZero() && m.TakenSrc != SrcAuto {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", fileTitle, m.TakenAtLocal.Format("2006")), SrcAuto)
|
2020-06-23 11:12:24 +02:00
|
|
|
} else if fileTitle != "" {
|
2020-05-28 15:12:18 +02:00
|
|
|
m.SetTitle(fileTitle, SrcAuto)
|
2020-06-23 11:12:24 +02:00
|
|
|
} else {
|
|
|
|
if m.TakenSrc != SrcAuto {
|
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", TitleUnknown, m.TakenAt.Format("2006")), SrcAuto)
|
|
|
|
} else {
|
|
|
|
m.SetTitle(TitleUnknown, SrcAuto)
|
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
if m.PhotoTitle != oldTitle {
|
2020-06-29 13:16:55 +02:00
|
|
|
log.Debugf("photo: changed title of %s to %s", m.PhotoUID, txt.Quote(m.PhotoTitle))
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLabels updates the entity with additional or updated label information.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) AddLabels(labels classify.Labels) {
|
2020-05-26 11:00:39 +02:00
|
|
|
for _, classifyLabel := range labels {
|
|
|
|
labelEntity := FirstOrCreateLabel(NewLabel(classifyLabel.Title(), classifyLabel.Priority))
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if labelEntity == nil {
|
2020-06-02 17:57:12 +02:00
|
|
|
log.Errorf("index: label %s should not be nil - bug? (%s)", txt.Quote(classifyLabel.Title()), m)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if labelEntity.Deleted() {
|
|
|
|
log.Debugf("index: skipping deleted label %s (%s)", txt.Quote(classifyLabel.Title()), m)
|
2020-05-26 11:00:39 +02:00
|
|
|
continue
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if err := labelEntity.UpdateClassify(classifyLabel); err != nil {
|
2020-04-18 23:20:54 +02:00
|
|
|
log.Errorf("index: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
photoLabel := FirstOrCreatePhotoLabel(NewPhotoLabel(m.ID, labelEntity.ID, classifyLabel.Uncertainty, classifyLabel.Source))
|
|
|
|
|
|
|
|
if photoLabel == nil {
|
2020-06-02 17:57:12 +02:00
|
|
|
log.Errorf("index: photo-label %d should not be nil - bug? (%s)", labelEntity.ID, m)
|
2020-05-26 11:00:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if photoLabel.Uncertainty > classifyLabel.Uncertainty && photoLabel.Uncertainty < 100 {
|
|
|
|
if err := photoLabel.Updates(map[string]interface{}{
|
|
|
|
"Uncertainty": classifyLabel.Uncertainty,
|
|
|
|
"LabelSrc": classifyLabel.Source,
|
|
|
|
}); err != nil {
|
2020-04-18 23:20:54 +02:00
|
|
|
log.Errorf("index: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:35:41 +02:00
|
|
|
Db().Set("gorm:auto_preload", true).Model(m).Related(&m.Labels)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
2020-04-26 14:31:33 +02:00
|
|
|
|
2020-04-26 16:22:29 +02:00
|
|
|
// SetTitle changes the photo title and clips it to 300 characters.
|
2020-04-26 14:31:33 +02:00
|
|
|
func (m *Photo) SetTitle(title, source string) {
|
|
|
|
newTitle := txt.Clip(title, txt.ClipDefault)
|
|
|
|
|
|
|
|
if newTitle == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 16:22:29 +02:00
|
|
|
if m.TitleSrc != SrcAuto && m.TitleSrc != source && source != SrcManual && m.HasTitle() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
m.PhotoTitle = newTitle
|
|
|
|
m.TitleSrc = source
|
|
|
|
}
|
2020-04-26 16:22:29 +02:00
|
|
|
|
|
|
|
// SetDescription changes the photo description if not empty and from the same source.
|
|
|
|
func (m *Photo) SetDescription(desc, source string) {
|
|
|
|
newDesc := txt.Clip(desc, txt.ClipDescription)
|
|
|
|
|
|
|
|
if newDesc == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
if m.DescriptionSrc != SrcAuto && m.DescriptionSrc != source && source != SrcManual && m.PhotoDescription != "" {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
m.PhotoDescription = newDesc
|
2020-04-26 16:22:29 +02:00
|
|
|
m.DescriptionSrc = source
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTakenAt changes the photo date if not empty and from the same source.
|
|
|
|
func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) {
|
|
|
|
if taken.IsZero() || taken.Year() < 1000 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.TakenSrc != SrcAuto && m.TakenSrc != source && source != SrcManual {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.TakenAt = taken.Round(time.Second).UTC()
|
|
|
|
m.TakenSrc = source
|
|
|
|
|
|
|
|
if local.IsZero() || local.Year() < 1000 {
|
|
|
|
m.TakenAtLocal = m.TakenAt
|
|
|
|
} else {
|
|
|
|
m.TakenAtLocal = local.Round(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone != "" {
|
|
|
|
m.TimeZone = zone
|
|
|
|
}
|
2020-05-15 16:06:32 +02:00
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
m.UpdateDateFields()
|
2020-05-15 16:06:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
// UpdateDateFields updates internal date fields.
|
|
|
|
func (m *Photo) UpdateDateFields() {
|
2020-05-15 16:06:32 +02:00
|
|
|
if m.TakenAt.IsZero() || m.TakenAt.Year() < 1000 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.TakenAtLocal.IsZero() || m.TakenAtLocal.Year() < 1000 {
|
|
|
|
m.TakenAtLocal = m.TakenAt
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.TakenSrc == SrcAuto {
|
|
|
|
m.PhotoYear = YearUnknown
|
|
|
|
m.PhotoMonth = MonthUnknown
|
2020-07-06 07:41:33 +02:00
|
|
|
m.PhotoDay = DayUnknown
|
|
|
|
} else if m.TakenSrc != SrcManual {
|
2020-05-15 16:06:32 +02:00
|
|
|
m.PhotoYear = m.TakenAtLocal.Year()
|
|
|
|
m.PhotoMonth = int(m.TakenAtLocal.Month())
|
2020-07-06 07:41:33 +02:00
|
|
|
m.PhotoDay = m.TakenAtLocal.Day()
|
2020-05-15 16:06:32 +02:00
|
|
|
}
|
2020-04-26 16:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetCoordinates changes the photo lat, lng and altitude if not empty and from the same source.
|
|
|
|
func (m *Photo) SetCoordinates(lat, lng float32, altitude int, source string) {
|
2020-06-05 10:17:00 +02:00
|
|
|
if lat == 0.0 && lng == 0.0 {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:26:05 +02:00
|
|
|
if m.LocationSrc != SrcAuto && m.LocationSrc != source && source != SrcManual {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoLat = lat
|
|
|
|
m.PhotoLng = lng
|
|
|
|
m.PhotoAltitude = altitude
|
2020-06-10 18:26:05 +02:00
|
|
|
m.LocationSrc = source
|
2020-04-26 16:22:29 +02:00
|
|
|
}
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
// AllFilesMissing returns true, if all files for this photo are missing.
|
|
|
|
func (m *Photo) AllFilesMissing() bool {
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
if err := Db().Model(&File{}).
|
|
|
|
Where("photo_id = ? AND b.file_missing = 0", m.ID).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2020-05-07 19:42:04 +02:00
|
|
|
// Delete deletes the entity from the database.
|
|
|
|
func (m *Photo) Delete(permanently bool) error {
|
|
|
|
if permanently {
|
|
|
|
return m.DeletePermanently()
|
|
|
|
}
|
|
|
|
|
|
|
|
Db().Delete(File{}, "photo_id = ?", m.ID)
|
|
|
|
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete permanently deletes the entity from the database.
|
|
|
|
func (m *Photo) DeletePermanently() error {
|
|
|
|
Db().Unscoped().Delete(File{}, "photo_id = ?", m.ID)
|
|
|
|
Db().Unscoped().Delete(PhotoKeyword{}, "photo_id = ?", m.ID)
|
|
|
|
Db().Unscoped().Delete(PhotoLabel{}, "photo_id = ?", m.ID)
|
2020-05-23 20:58:58 +02:00
|
|
|
Db().Unscoped().Delete(PhotoAlbum{}, "photo_uid = ?", m.PhotoUID)
|
2020-05-07 19:42:04 +02:00
|
|
|
|
|
|
|
return Db().Unscoped().Delete(m).Error
|
|
|
|
}
|
2020-05-18 22:18:58 +02:00
|
|
|
|
|
|
|
// NoDescription returns true if the photo has no description.
|
|
|
|
func (m *Photo) NoDescription() bool {
|
|
|
|
return m.PhotoDescription == ""
|
|
|
|
}
|
2020-05-25 19:10:44 +02:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Updates a column in the database.
|
2020-05-25 19:10:44 +02:00
|
|
|
func (m *Photo) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
2020-05-26 13:43:16 +02:00
|
|
|
|
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *Photo) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFavorite updates the favorite status of a photo.
|
|
|
|
func (m *Photo) SetFavorite(favorite bool) error {
|
|
|
|
changed := m.PhotoFavorite != favorite
|
|
|
|
m.PhotoFavorite = favorite
|
|
|
|
m.PhotoQuality = m.QualityScore()
|
|
|
|
|
|
|
|
if err := m.Updates(map[string]interface{}{"PhotoFavorite": m.PhotoFavorite, "PhotoQuality": m.PhotoQuality}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update counters if changed and not deleted.
|
2020-06-04 17:33:00 +02:00
|
|
|
if changed && m.PhotoPrivate == false && m.DeletedAt == nil {
|
2020-05-26 13:43:16 +02:00
|
|
|
if favorite {
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-09 09:20:20 +02:00
|
|
|
|
|
|
|
// Approve approves a photo in review.
|
|
|
|
func (m *Photo) Approve() error {
|
|
|
|
if m.PhotoQuality >= 3 {
|
|
|
|
// Nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:16:13 +02:00
|
|
|
edited := Timestamp()
|
2020-06-09 09:20:20 +02:00
|
|
|
m.EditedAt = &edited
|
|
|
|
m.PhotoQuality = m.QualityScore()
|
|
|
|
|
|
|
|
if err := Db().Model(m).Updates(Photo{EditedAt: m.EditedAt, PhotoQuality: m.PhotoQuality}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UpdatePhotoCounts(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("count.review", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
// Links returns all share links for this entity.
|
|
|
|
func (m *Photo) Links() Links {
|
|
|
|
return FindLinks("", m.PhotoUID)
|
|
|
|
}
|