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-12-14 13:31:18 +01:00
|
|
|
"path/filepath"
|
2020-12-04 19:51:51 +01:00
|
|
|
"strconv"
|
2020-03-26 09:41:33 +01:00
|
|
|
"strings"
|
2020-12-14 13:31:18 +01:00
|
|
|
"sync"
|
2018-08-09 23:10:05 +02:00
|
|
|
"time"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2021-11-18 13:13:48 +01:00
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
|
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"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-07-18 15:17:56 +02:00
|
|
|
)
|
|
|
|
|
2021-11-18 02:23:25 +01:00
|
|
|
var MetadataUpdateInterval = 24 * 3 * time.Hour // 3 Days
|
|
|
|
var MetadataEstimateInterval = 24 * 7 * time.Hour // 7 Days
|
|
|
|
|
2020-12-14 13:31:18 +01:00
|
|
|
var photoMutex = sync.Mutex{}
|
|
|
|
|
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-12-04 19:51:51 +01:00
|
|
|
// MapKey returns a key referencing time and location for indexing.
|
|
|
|
func MapKey(takenAt time.Time, cellId string) string {
|
|
|
|
return path.Join(strconv.FormatInt(takenAt.Unix(), 36), cellId)
|
|
|
|
}
|
|
|
|
|
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-09-13 17:51:43 +02:00
|
|
|
UUID string `gorm:"type:VARBINARY(42);index;" json:"DocumentID,omitempty" yaml:"DocumentID,omitempty"`
|
2022-03-30 20:36:25 +02:00
|
|
|
TakenAt time.Time `gorm:"type:DATETIME;index:idx_photos_taken_uid;" json:"TakenAt" yaml:"TakenAt"`
|
|
|
|
TakenAtLocal time.Time `gorm:"type:DATETIME;" yaml:"-"`
|
2020-09-13 17:51:43 +02:00
|
|
|
TakenSrc string `gorm:"type:VARBINARY(8);" json:"TakenSrc" yaml:"TakenSrc,omitempty"`
|
|
|
|
PhotoUID string `gorm:"type:VARBINARY(42);unique_index;index:idx_photos_taken_uid;" json:"UID" yaml:"UID"`
|
|
|
|
PhotoType string `gorm:"type:VARBINARY(8);default:'image';" json:"Type" yaml:"Type"`
|
|
|
|
TypeSrc string `gorm:"type:VARBINARY(8);" json:"TypeSrc" yaml:"TypeSrc,omitempty"`
|
2021-09-23 23:46:17 +02:00
|
|
|
PhotoTitle string `gorm:"type:VARCHAR(200);" json:"Title" yaml:"Title"`
|
2020-09-13 17:51:43 +02:00
|
|
|
TitleSrc string `gorm:"type:VARBINARY(8);" json:"TitleSrc" yaml:"TitleSrc,omitempty"`
|
2022-03-30 20:36:25 +02:00
|
|
|
PhotoDescription string `gorm:"type:VARCHAR(4096);" json:"Description" yaml:"Description,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
DescriptionSrc string `gorm:"type:VARBINARY(8);" json:"DescriptionSrc" yaml:"DescriptionSrc,omitempty"`
|
2020-12-15 20:14:06 +01:00
|
|
|
PhotoPath string `gorm:"type:VARBINARY(500);index:idx_photos_path_name;" json:"Path" yaml:"-"`
|
2020-12-12 22:02:14 +01:00
|
|
|
PhotoName string `gorm:"type:VARBINARY(255);index:idx_photos_path_name;" json:"Name" yaml:"-"`
|
2020-12-15 20:14:06 +01:00
|
|
|
OriginalName string `gorm:"type:VARBINARY(755);" json:"OriginalName" yaml:"OriginalName,omitempty"`
|
2020-12-21 18:43:31 +01:00
|
|
|
PhotoStack int8 `json:"Stack" yaml:"Stack,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-07-16 13:02:48 +02:00
|
|
|
PhotoPanorama bool `json:"Panorama" yaml:"Panorama,omitempty"`
|
2021-02-05 16:32:08 +01:00
|
|
|
TimeZone string `gorm:"type:VARBINARY(64);" json:"TimeZone" yaml:"TimeZone,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
PlaceID string `gorm:"type:VARBINARY(42);index;default:'zz'" json:"PlaceID" yaml:"-"`
|
|
|
|
PlaceSrc string `gorm:"type:VARBINARY(8);" json:"PlaceSrc" yaml:"PlaceSrc,omitempty"`
|
|
|
|
CellID string `gorm:"type:VARBINARY(42);index;default:'zz'" json:"CellID" yaml:"-"`
|
2020-07-12 08:27:05 +02:00
|
|
|
CellAccuracy int `json:"CellAccuracy" yaml:"CellAccuracy,omitempty"`
|
2020-07-06 10:32:37 +02:00
|
|
|
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"`
|
2020-09-13 17:51:43 +02:00
|
|
|
PhotoCountry string `gorm:"type:VARBINARY(2);index:idx_photos_country_year_month;default:'zz'" json:"Country" yaml:"-"`
|
2021-09-18 20:41:30 +02:00
|
|
|
PhotoYear int `gorm:"index:idx_photos_ymd;index:idx_photos_country_year_month;" json:"Year" yaml:"Year"`
|
|
|
|
PhotoMonth int `gorm:"index:idx_photos_ymd;index:idx_photos_country_year_month;" json:"Month" yaml:"Month"`
|
|
|
|
PhotoDay int `gorm:"index:idx_photos_ymd" json:"Day" yaml:"Day"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoIso int `json:"Iso" yaml:"ISO,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
PhotoExposure string `gorm:"type:VARBINARY(64);" json:"Exposure" yaml:"Exposure,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoFNumber float32 `gorm:"type:FLOAT;" json:"FNumber" yaml:"FNumber,omitempty"`
|
|
|
|
PhotoFocalLength int `json:"FocalLength" yaml:"FocalLength,omitempty"`
|
2021-02-05 16:32:08 +01:00
|
|
|
PhotoQuality int `gorm:"type:SMALLINT" json:"Quality" yaml:"Quality,omitempty"`
|
2021-05-26 09:51:00 +02:00
|
|
|
PhotoFaces int `json:"Faces,omitempty" yaml:"Faces,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoResolution int `gorm:"type:SMALLINT" json:"Resolution" yaml:"-"`
|
2020-12-17 12:32:53 +01:00
|
|
|
PhotoColor uint8 `json:"Color" yaml:"-"`
|
2020-07-12 04:48:17 +02:00
|
|
|
CameraID uint `gorm:"index:idx_photos_camera_lens;default:1" json:"CameraID" yaml:"-"`
|
2021-09-23 23:46:17 +02:00
|
|
|
CameraSerial string `gorm:"type:VARBINARY(160);" json:"CameraSerial" yaml:"CameraSerial,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
CameraSrc string `gorm:"type:VARBINARY(8);" json:"CameraSrc" yaml:"-"`
|
2020-07-12 04:48:17 +02:00
|
|
|
LensID uint `gorm:"index:idx_photos_camera_lens;default:1" json:"LensID" yaml:"-"`
|
2020-07-06 19:15:57 +02:00
|
|
|
Details *Details `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Details" yaml:"Details"`
|
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-07-12 08:27:05 +02:00
|
|
|
Cell *Cell `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Cell" yaml:"-"`
|
2020-05-29 12:56:24 +02:00
|
|
|
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:"-"`
|
2021-11-18 02:23:25 +01:00
|
|
|
EstimatedAt *time.Time `json:"EstimatedAt,omitempty" 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
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (Photo) TableName() string {
|
|
|
|
return "photos"
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:06:42 +02:00
|
|
|
// NewPhoto creates a photo entity.
|
2020-12-19 19:15:32 +01:00
|
|
|
func NewPhoto(stackable bool) Photo {
|
|
|
|
m := Photo{
|
2021-08-19 21:12:38 +02:00
|
|
|
PhotoTitle: UnknownTitle,
|
2022-04-13 22:17:59 +02:00
|
|
|
PhotoType: MediaImage,
|
2020-06-04 17:06:42 +02:00
|
|
|
PhotoCountry: UnknownCountry.ID,
|
|
|
|
CameraID: UnknownCamera.ID,
|
|
|
|
LensID: UnknownLens.ID,
|
2020-07-12 08:27:05 +02:00
|
|
|
CellID: UnknownLocation.ID,
|
2020-06-04 17:06:42 +02:00
|
|
|
PlaceID: UnknownPlace.ID,
|
2020-07-06 19:15:57 +02:00
|
|
|
Camera: &UnknownCamera,
|
|
|
|
Lens: &UnknownLens,
|
2020-07-12 08:27:05 +02:00
|
|
|
Cell: &UnknownLocation,
|
2020-07-06 19:15:57 +02:00
|
|
|
Place: &UnknownPlace,
|
2020-06-04 17:06:42 +02:00
|
|
|
}
|
2020-12-19 19:15:32 +01:00
|
|
|
|
|
|
|
if stackable {
|
|
|
|
m.PhotoStack = IsStackable
|
|
|
|
} else {
|
|
|
|
m.PhotoStack = IsUnstacked
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
2020-06-04 17:06:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
// SavePhotoForm saves a model in the database using form data.
|
2020-12-05 00:13:44 +01:00
|
|
|
func SavePhotoForm(model Photo, form form.Photo) 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() {
|
2022-01-05 11:40:44 +01:00
|
|
|
return errors.New("cannot save form when photo id is missing")
|
2020-05-19 12:48:32 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 19:23:10 +01:00
|
|
|
// Update time fields.
|
|
|
|
if model.TimeZoneUTC() {
|
|
|
|
model.TakenAtLocal = model.TakenAt
|
|
|
|
} else {
|
|
|
|
model.TakenAt = model.GetTakenAt()
|
|
|
|
}
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
model.UpdateDateFields()
|
2020-05-15 16:06:32 +02:00
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
details := model.GetDetails()
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
if form.Details.PhotoID == model.ID {
|
2020-07-06 19:15:57 +02:00
|
|
|
if err := deepcopier.Copy(details).From(form.Details); err != nil {
|
2020-03-26 09:41:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
details.Keywords = strings.Join(txt.UniqueWords(txt.Words(details.Keywords)), ", ")
|
2020-03-26 09:41:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 08:27:05 +02:00
|
|
|
if locChanged && model.PlaceSrc == SrcManual {
|
2020-12-05 00:13:44 +01:00
|
|
|
locKeywords, labels := model.UpdateLocation()
|
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-07-06 19:15:57 +02:00
|
|
|
w := txt.UniqueWords(txt.Words(details.Keywords))
|
2020-04-16 20:57:00 +02:00
|
|
|
w = append(w, locKeywords...)
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
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 {
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Errorf("photo: %s %s while syncing keywords and labels", model.String(), err)
|
2020-05-28 21:20:42 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Errorf("photo: %s %s while indexing keywords", model.String(), err.Error())
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +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-07-06 19:15:57 +02:00
|
|
|
if err := model.Save(); err != nil {
|
2020-05-11 11:01:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
if err := UpdateCounts(); err != nil {
|
|
|
|
log.Warnf("index: %s (update counts)", err)
|
2020-05-11 11:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
return clean.Log(m.PhotoName)
|
2020-05-31 17:45:58 +02:00
|
|
|
} else if m.OriginalName != "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
return clean.Log(m.OriginalName)
|
2020-05-31 17:45:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
return "(unknown)"
|
2020-05-31 17:45:58 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
return "uid " + clean.Log(m.PhotoUID)
|
2020-05-31 17:45:58 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:09:39 +02:00
|
|
|
// FirstOrCreate fetches an existing row from the database or inserts a new one.
|
|
|
|
func (m *Photo) FirstOrCreate() error {
|
|
|
|
if err := m.Create(); err == nil {
|
|
|
|
return nil
|
2020-12-14 13:31:18 +01:00
|
|
|
} else if fErr := m.Find(); fErr != nil {
|
|
|
|
name := filepath.Join(m.PhotoPath, m.PhotoName)
|
|
|
|
log.Debugf("photo: %s in %s (create)", err, name)
|
|
|
|
log.Debugf("photo: %s in %s (find after create failed)", fErr, name)
|
|
|
|
return fmt.Errorf("%s / %s", err, fErr)
|
2020-07-10 17:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:20:38 +02:00
|
|
|
// Create inserts a new photo to the database.
|
2020-07-06 19:15:57 +02:00
|
|
|
func (m *Photo) Create() error {
|
2020-12-14 13:31:18 +01:00
|
|
|
photoMutex.Lock()
|
|
|
|
defer photoMutex.Unlock()
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
if err := UnscopedDb().Create(m).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:59:47 +02:00
|
|
|
if err := m.SaveDetails(); err != nil {
|
2020-07-06 19:15:57 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:20:38 +02:00
|
|
|
// Save updates an existing photo or inserts a new one.
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Photo) Save() error {
|
2020-12-14 20:56:33 +01:00
|
|
|
photoMutex.Lock()
|
|
|
|
defer photoMutex.Unlock()
|
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
if err := Save(m, "ID", "PhotoUID"); err != nil {
|
2020-07-07 12:59:47 +02:00
|
|
|
return err
|
2020-07-06 19:15:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:59:47 +02:00
|
|
|
if err := m.SaveDetails(); err != nil {
|
2020-07-06 19:15:57 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:21:13 +01:00
|
|
|
return m.ResolvePrimary()
|
2020-07-06 19:15:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:20:38 +02:00
|
|
|
// Find returns a photo from the database.
|
|
|
|
func (m *Photo) Find() error {
|
|
|
|
if m.PhotoUID == "" && m.ID == 0 {
|
|
|
|
return fmt.Errorf("photo: id and uid must not be empty (find)")
|
|
|
|
}
|
|
|
|
|
|
|
|
q := UnscopedDb().
|
|
|
|
Preload("Labels", func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
|
|
|
|
}).
|
|
|
|
Preload("Labels.Label").
|
|
|
|
Preload("Camera").
|
|
|
|
Preload("Lens").
|
|
|
|
Preload("Details").
|
|
|
|
Preload("Place").
|
2020-07-12 08:27:05 +02:00
|
|
|
Preload("Cell").
|
|
|
|
Preload("Cell.Place")
|
2020-07-07 12:20:38 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
if rnd.EntityUID(m.PhotoUID, 'p') {
|
2020-07-07 12:20:38 +02:00
|
|
|
if err := q.First(m, "photo_uid = ?", m.PhotoUID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err := q.First(m, "id = ?", m.ID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
// SaveLabels updates the photo after labels have changed.
|
2020-07-06 19:15:57 +02:00
|
|
|
func (m *Photo) SaveLabels() error {
|
2020-05-19 12:48:32 +02:00
|
|
|
if !m.HasID() {
|
2022-01-05 11:40:44 +01:00
|
|
|
return errors.New("photo: cannot save to database, id is empty")
|
2020-05-19 12:48:32 +02:00
|
|
|
}
|
|
|
|
|
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-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-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-07-06 19:15:57 +02:00
|
|
|
if err := m.Save(); err != nil {
|
2020-05-11 11:01:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
if err := UpdateCounts(); err != nil {
|
|
|
|
log.Warnf("index: %s (update counts)", err)
|
2020-05-11 11:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-08-29 13:26:05 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
if rnd.ValidID(m.PhotoUID, 'p') {
|
2020-05-01 12:57:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
return scope.SetColumn("PhotoUID", rnd.GenerateUID('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() {
|
2021-08-29 13:26:05 +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 {
|
2020-07-06 19:15:57 +02:00
|
|
|
details := m.GetDetails()
|
2020-05-28 21:20:42 +02:00
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
words := txt.RemoveFromWords(txt.Words(details.Keywords), w)
|
|
|
|
details.Keywords = strings.Join(words, ", ")
|
2020-05-28 21:20:42 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncKeywordLabels maintains the label / photo relationship for existing labels and keywords.
|
|
|
|
func (m *Photo) SyncKeywordLabels() error {
|
2020-07-06 19:15:57 +02:00
|
|
|
details := m.GetDetails()
|
|
|
|
keywords := txt.UniqueKeywords(details.Keywords)
|
2020-05-28 21:20:42 +02:00
|
|
|
|
|
|
|
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-07-06 19:15:57 +02:00
|
|
|
db := UnscopedDb()
|
|
|
|
details := m.GetDetails()
|
2020-04-30 20:07:03 +02:00
|
|
|
|
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)...)
|
2021-09-29 20:09:34 +02:00
|
|
|
keywords = append(keywords, m.SubjectKeywords()...)
|
2021-05-01 11:06:44 +02:00
|
|
|
keywords = append(keywords, txt.Words(details.Keywords)...)
|
2020-07-06 19:15:57 +02:00
|
|
|
keywords = append(keywords, txt.Keywords(details.Subject)...)
|
|
|
|
keywords = append(keywords, txt.Keywords(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 {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("index keyword should not be nil - possible 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
|
2021-09-02 11:12:42 +02:00
|
|
|
func (m *Photo) PreloadFiles() {
|
2020-10-04 14:21:40 +02:00
|
|
|
q := Db().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("files").
|
2022-03-30 20:36:25 +02:00
|
|
|
Select("files.*").
|
2021-05-31 15:40:52 +02:00
|
|
|
Where("files.photo_id = ? AND files.deleted_at IS NULL", m.ID).
|
2020-04-21 10:23:27 +02:00
|
|
|
Order("files.file_name DESC")
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2021-11-25 14:52:26 +01:00
|
|
|
Log("photo", "preload files", q.Scan(&m.Files).Error)
|
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")
|
|
|
|
|
2021-11-25 14:52:26 +01:00
|
|
|
Log("photo", "preload files", q.Scan(&m.Keywords).Error)
|
2019-12-11 19:11:44 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-25 14:52:26 +01:00
|
|
|
Log("photo", "preload albums", q.Scan(&m.Albums).Error)
|
2019-12-11 19:11:44 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-09-02 11:12:42 +02:00
|
|
|
m.PreloadFiles()
|
2020-04-30 20:07:03 +02:00
|
|
|
m.PreloadKeywords()
|
|
|
|
m.PreloadAlbums()
|
2019-12-11 19:11:44 +01:00
|
|
|
}
|
2019-12-28 12:28:06 +01:00
|
|
|
|
2021-02-11 19:48:33 +01:00
|
|
|
// HasID tests 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-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 == ""
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
// UnknownCamera test if the camera is unknown.
|
|
|
|
func (m *Photo) UnknownCamera() bool {
|
|
|
|
return m.CameraID == 0 || m.CameraID == UnknownCamera.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnknownLens test if the lens is unknown.
|
|
|
|
func (m *Photo) UnknownLens() bool {
|
|
|
|
return m.LensID == 0 || m.LensID == UnknownLens.ID
|
|
|
|
}
|
|
|
|
|
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-07-06 19:15:57 +02:00
|
|
|
// GetDetails returns the photo description details.
|
|
|
|
func (m *Photo) GetDetails() *Details {
|
2020-07-06 20:20:03 +02:00
|
|
|
if m.Details != nil {
|
|
|
|
m.Details.PhotoID = m.ID
|
|
|
|
return m.Details
|
|
|
|
} else if !m.HasID() {
|
|
|
|
m.Details = &Details{}
|
2020-07-06 19:15:57 +02:00
|
|
|
return m.Details
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:20:03 +02:00
|
|
|
m.Details = &Details{PhotoID: m.ID}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
if details := FirstOrCreateDetails(m.Details); details != nil {
|
|
|
|
m.Details = details
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Details
|
2020-04-17 14:16:34 +02:00
|
|
|
}
|
2020-04-18 23:20:54 +02:00
|
|
|
|
2020-07-07 12:59:47 +02:00
|
|
|
// SaveDetails writes photo details to the database.
|
2020-07-07 20:44:33 +02:00
|
|
|
func (m *Photo) SaveDetails() error {
|
2020-07-07 12:59:47 +02:00
|
|
|
if err := m.GetDetails().Save(); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if details := FirstOrCreateDetails(m.GetDetails()); details != nil {
|
|
|
|
m.Details = details
|
|
|
|
return nil
|
|
|
|
} else {
|
2020-07-09 17:45:56 +02:00
|
|
|
log.Errorf("photo: %s (save details for %d)", err, m.ID)
|
2020-07-07 12:59:47 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// 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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("index: label %s should not be nil - possible bug (%s)", clean.Log(classifyLabel.Title()), m)
|
2020-06-02 17:57:12 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if labelEntity.Deleted() {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("index: skipping deleted label %s (%s)", clean.Log(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 {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("index: failed updating label %s (%s)", clean.Log(classifyLabel.Title()), err)
|
2020-04-18 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
photoLabel := FirstOrCreatePhotoLabel(NewPhotoLabel(m.ID, labelEntity.ID, classifyLabel.Uncertainty, classifyLabel.Source))
|
|
|
|
|
|
|
|
if photoLabel == nil {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("index: photo-label %d should not be nil - possible 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
|
|
|
// SetDescription changes the photo description if not empty and from the same source.
|
|
|
|
func (m *Photo) SetDescription(desc, source string) {
|
2022-03-30 20:36:25 +02:00
|
|
|
newDesc := txt.Clip(desc, txt.ClipLongText)
|
2020-04-26 16:22:29 +02:00
|
|
|
|
|
|
|
if newDesc == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:23:17 +01:00
|
|
|
if (SrcPriority[source] < SrcPriority[m.DescriptionSrc]) && m.HasDescription() {
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
// SetCamera updates the camera.
|
|
|
|
func (m *Photo) SetCamera(camera *Camera, source string) {
|
|
|
|
if camera == nil {
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Warnf("photo: %s failed updating camera from source %s", m.String(), SrcString(source))
|
2021-02-06 16:30:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if camera.Unknown() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if SrcPriority[source] < SrcPriority[m.CameraSrc] && !m.UnknownCamera() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.CameraID = camera.ID
|
|
|
|
m.Camera = camera
|
|
|
|
m.CameraSrc = source
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLens updates the lens.
|
|
|
|
func (m *Photo) SetLens(lens *Lens, source string) {
|
|
|
|
if lens == nil {
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Warnf("photo: %s failed updating lens from source %s", m.String(), SrcString(source))
|
2021-02-06 16:30:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if lens.Unknown() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if SrcPriority[source] < SrcPriority[m.CameraSrc] && !m.UnknownLens() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.LensID = lens.ID
|
|
|
|
m.Lens = lens
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetExposure updates the photo exposure details.
|
|
|
|
func (m *Photo) SetExposure(focalLength int, fNumber float32, iso int, exposure, source string) {
|
|
|
|
hasPriority := SrcPriority[source] >= SrcPriority[m.CameraSrc]
|
|
|
|
|
|
|
|
if focalLength > 0 && (hasPriority || m.PhotoFocalLength <= 0) {
|
|
|
|
m.PhotoFocalLength = focalLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if fNumber > 0 && (hasPriority || m.PhotoFNumber <= 0) {
|
|
|
|
m.PhotoFNumber = fNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if iso > 0 && (hasPriority || m.PhotoIso <= 0) {
|
|
|
|
m.PhotoIso = iso
|
|
|
|
}
|
|
|
|
|
|
|
|
if exposure != "" && (hasPriority || m.PhotoExposure == "") {
|
|
|
|
m.PhotoExposure = exposure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{}).
|
2020-12-11 17:21:13 +01:00
|
|
|
Where("photo_id = ? AND file_missing = 0", m.ID).
|
2020-05-13 15:36:42 +02:00
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:28:03 +01:00
|
|
|
// AllFiles returns all files of this photo.
|
|
|
|
func (m *Photo) AllFiles() (files Files) {
|
2021-09-24 13:13:59 +02:00
|
|
|
if err := UnscopedDb().Where("photo_id = ?", m.ID).Find(&files).Error; err != nil {
|
2020-11-21 23:28:03 +01:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:32:08 +01:00
|
|
|
// Archive removes the photo from albums and flags it as archived (soft delete).
|
|
|
|
func (m *Photo) Archive() error {
|
2021-08-29 13:26:05 +02:00
|
|
|
deletedAt := TimeStamp()
|
2021-02-05 16:32:08 +01:00
|
|
|
|
2022-04-04 08:54:03 +02:00
|
|
|
if err := Db().Model(&PhotoAlbum{}).Where("photo_uid = ?", m.PhotoUID).UpdateColumn("hidden", true).Error; err != nil {
|
2021-02-05 16:32:08 +01:00
|
|
|
return err
|
|
|
|
} else if err := m.Update("deleted_at", deletedAt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.DeletedAt = &deletedAt
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore removes the archive flag (undo soft delete).
|
|
|
|
func (m *Photo) Restore() error {
|
|
|
|
if err := m.Update("deleted_at", gorm.Expr("NULL")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.DeletedAt = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
// Delete deletes the photo from the index.
|
|
|
|
func (m *Photo) Delete(permanently bool) (files Files, err error) {
|
2021-09-30 15:50:10 +02:00
|
|
|
if m.ID < 1 || m.PhotoUID == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
return files, fmt.Errorf("invalid photo id %d / uid %s", m.ID, clean.Log(m.PhotoUID))
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 19:42:04 +02:00
|
|
|
if permanently {
|
|
|
|
return m.DeletePermanently()
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
files = m.AllFiles()
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
for _, file := range files {
|
|
|
|
if err := file.Delete(false); err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove file)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, m.Updates(map[string]interface{}{"DeletedAt": TimeStamp(), "PhotoQuality": -1})
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// DeletePermanently permanently removes a photo from the index.
|
2021-09-24 13:13:59 +02:00
|
|
|
func (m *Photo) DeletePermanently() (files Files, err error) {
|
2021-09-30 15:50:10 +02:00
|
|
|
if m.ID < 1 || m.PhotoUID == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
return files, fmt.Errorf("invalid photo id %d / uid %s", m.ID, clean.Log(m.PhotoUID))
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
files = m.AllFiles()
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if err := file.DeletePermanently(); err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove file)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(Details{}, "photo_id = ?", m.ID).Error; err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove details)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(PhotoKeyword{}, "photo_id = ?", m.ID).Error; err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove keywords)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(PhotoLabel{}, "photo_id = ?", m.ID).Error; err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove labels)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(PhotoAlbum{}, "photo_uid = ?", m.PhotoUID).Error; err != nil {
|
2021-09-30 15:50:10 +02:00
|
|
|
log.Errorf("photo: %s (remove albums)", err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
return files, UnscopedDb().Delete(m).Error
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
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
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
// Update a column in the database.
|
2020-05-25 19:10:44 +02:00
|
|
|
func (m *Photo) Update(attr string, value interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-05-26 13:43:16 +02:00
|
|
|
|
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *Photo) Updates(values interface{}) error {
|
2022-04-04 08:54:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
2020-05-26 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 14:33:49 +01:00
|
|
|
// SetFavorite updates the favorite flag of a photo.
|
2020-05-26 13:43:16 +02:00
|
|
|
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
|
|
|
|
2022-01-06 14:33:49 +01:00
|
|
|
// SetStack updates the stack flag of a photo.
|
|
|
|
func (m *Photo) SetStack(stack int8) {
|
|
|
|
if m.PhotoStack != stack {
|
|
|
|
m.PhotoStack = stack
|
|
|
|
Log("photo", "update stack flag", m.Update("PhotoStack", m.PhotoStack))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +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
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
if err := UpdateCounts(); err != nil {
|
|
|
|
log.Warnf("index: %s (update counts)", err)
|
2020-06-09 09:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-07-14 11:00:49 +02:00
|
|
|
|
|
|
|
// PrimaryFile returns the primary file for this photo.
|
2021-09-02 14:23:40 +02:00
|
|
|
func (m *Photo) PrimaryFile() (*File, error) {
|
2020-07-14 11:00:49 +02:00
|
|
|
return PrimaryFile(m.PhotoUID)
|
|
|
|
}
|
2020-12-04 19:51:51 +01:00
|
|
|
|
2021-01-24 20:40:40 +01:00
|
|
|
// SetPrimary sets a new primary file.
|
2022-03-30 20:36:25 +02:00
|
|
|
func (m *Photo) SetPrimary(fileUID string) (err error) {
|
2021-01-24 20:40:40 +01:00
|
|
|
if m.PhotoUID == "" {
|
|
|
|
return fmt.Errorf("photo uid is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
|
|
|
|
if fileUID != "" {
|
|
|
|
// Do nothing.
|
2022-03-30 20:36:25 +02:00
|
|
|
} else if err = Db().Model(File{}).
|
2021-10-05 18:42:39 +02:00
|
|
|
Where("photo_uid = ? AND file_type = 'jpg' AND file_missing = 0 AND file_error = ''", m.PhotoUID).
|
2022-01-05 16:37:19 +01:00
|
|
|
Order("file_width DESC, file_hdr DESC").Limit(1).
|
2021-01-24 20:40:40 +01:00
|
|
|
Pluck("file_uid", &files).Error; err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(files) == 0 {
|
2021-10-05 18:42:39 +02:00
|
|
|
return fmt.Errorf("found no valid jpeg for %s", m.PhotoUID)
|
2021-01-24 20:40:40 +01:00
|
|
|
} else {
|
|
|
|
fileUID = files[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileUID == "" {
|
|
|
|
return fmt.Errorf("file uid is empty")
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
if err = Db().Model(File{}).
|
|
|
|
Where("photo_uid = ? AND file_uid <> ?", m.PhotoUID, fileUID).
|
2022-04-04 08:54:03 +02:00
|
|
|
UpdateColumn("file_primary", 0).Error; err != nil {
|
2022-03-30 20:36:25 +02:00
|
|
|
return err
|
|
|
|
} else if err = Db().Model(File{}).Where("photo_uid = ? AND file_uid = ?", m.PhotoUID, fileUID).
|
2022-04-04 08:54:03 +02:00
|
|
|
UpdateColumn("file_primary", 1).Error; err != nil {
|
2021-01-24 20:40:40 +01:00
|
|
|
return err
|
|
|
|
} else if m.PhotoQuality < 0 {
|
|
|
|
m.PhotoQuality = 0
|
2022-03-30 20:36:25 +02:00
|
|
|
err = m.UpdateQuality()
|
2021-01-24 20:40:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Regenerate file search index.
|
|
|
|
File{PhotoID: m.ID, PhotoUID: m.PhotoUID}.RegenerateIndex()
|
|
|
|
|
2021-01-24 20:40:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:51:51 +01:00
|
|
|
// MapKey returns a key referencing time and location for indexing.
|
|
|
|
func (m *Photo) MapKey() string {
|
|
|
|
return MapKey(m.TakenAt, m.CellID)
|
|
|
|
}
|
2020-12-31 13:51:31 +01:00
|
|
|
|
|
|
|
// SetCameraSerial updates the camera serial number.
|
|
|
|
func (m *Photo) SetCameraSerial(s string) {
|
2021-09-23 23:46:17 +02:00
|
|
|
if s = txt.Clip(s, txt.ClipDefault); m.NoCameraSerial() && s != "" {
|
|
|
|
m.CameraSerial = s
|
2020-12-31 13:51:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 14:23:40 +02:00
|
|
|
|
|
|
|
// FaceCount returns the current number of faces on the primary picture.
|
|
|
|
func (m *Photo) FaceCount() int {
|
|
|
|
if f, err := m.PrimaryFile(); err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
2021-09-23 23:46:17 +02:00
|
|
|
return f.ValidFaceCount()
|
2021-09-02 14:23:40 +02:00
|
|
|
}
|
|
|
|
}
|