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"
|
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-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"`
|
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:"-"`
|
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"`
|
|
|
|
PhotoTitle string `gorm:"type:VARCHAR(255);" json:"Title" yaml:"Title"`
|
|
|
|
TitleSrc string `gorm:"type:VARBINARY(8);" json:"TitleSrc" yaml:"TitleSrc,omitempty"`
|
|
|
|
PhotoDescription string `gorm:"type:TEXT;" json:"Description" yaml:"Description,omitempty"`
|
|
|
|
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:"-"`
|
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"`
|
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:"-"`
|
2020-09-13 17:51:43 +02:00
|
|
|
CameraSerial string `gorm:"type:VARBINARY(255);" json:"CameraSerial" yaml:"CameraSerial,omitempty"`
|
|
|
|
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:"-"`
|
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.
|
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,
|
2020-06-04 17:06:42 +02:00
|
|
|
PhotoType: TypeImage,
|
|
|
|
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() {
|
|
|
|
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-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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
if rnd.IsPPID(m.PhotoUID, 'p') {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the photo to the database.
|
2020-07-06 19:15:57 +02:00
|
|
|
func (m *Photo) SaveLabels() 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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
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-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 {
|
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
|
2021-05-31 15:40:52 +02:00
|
|
|
func (m *Photo) PreloadFiles(markers bool) {
|
2020-10-04 14:21:40 +02:00
|
|
|
q := Db().
|
2019-12-11 19:11:44 +01:00
|
|
|
Table("files").
|
|
|
|
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
|
|
|
|
|
|
|
logError(q.Scan(&m.Files))
|
2021-05-31 15:40:52 +02:00
|
|
|
|
|
|
|
if markers {
|
|
|
|
for i := range m.Files {
|
|
|
|
m.Files[i].PreloadMarkers()
|
|
|
|
}
|
|
|
|
}
|
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() {
|
2021-05-31 15:40:52 +02:00
|
|
|
m.PreloadFiles(true)
|
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
|
|
|
}
|
|
|
|
|
2021-02-11 19:48:33 +01:00
|
|
|
// UnknownLocation tests if the photo has an unknown location.
|
2020-05-28 15:12:18 +02:00
|
|
|
func (m *Photo) UnknownLocation() bool {
|
2021-02-11 19:48:33 +01:00
|
|
|
return m.CellID == "" || m.CellID == UnknownLocation.ID || m.NoLatLng()
|
2019-12-28 12:28:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 19:48:33 +01:00
|
|
|
// HasLocation tests 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
|
|
|
}
|
|
|
|
|
2021-02-11 19:48:33 +01:00
|
|
|
// LocationLoaded tests if the photo has a known location that is currently loaded.
|
2020-05-25 19:10:44 +02:00
|
|
|
func (m *Photo) LocationLoaded() bool {
|
2020-07-12 08:27:05 +02:00
|
|
|
if m.Cell == nil {
|
2020-06-04 22:05:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-12 08:27:05 +02:00
|
|
|
if m.Cell.Place == nil {
|
2020-06-04 22:05:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-12 08:27:05 +02:00
|
|
|
return !m.Cell.Unknown() && m.Cell.ID == m.CellID
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-12 08:27:05 +02:00
|
|
|
var location Cell
|
2020-06-04 22:05:24 +02:00
|
|
|
|
2020-07-12 08:27:05 +02:00
|
|
|
err := Db().Preload("Place").First(&location, "id = ?", m.CellID).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-07-12 08:27:05 +02:00
|
|
|
m.Cell = &location
|
2020-06-04 22:26:00 +02:00
|
|
|
|
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 == ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// 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-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-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 {
|
2021-01-25 20:30:52 +01:00
|
|
|
// Generate title based on photo name, if not generated:
|
2020-07-11 16:46:29 +02:00
|
|
|
if !fs.IsGenerated(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:30:52 +01:00
|
|
|
// Generate title based on original file name, if any:
|
|
|
|
if m.OriginalName != "" {
|
|
|
|
if title := txt.FileTitle(m.OriginalName); !fs.IsGenerated(m.OriginalName) && title != "" {
|
2020-05-31 14:42:41 +02:00
|
|
|
return title
|
2021-01-25 20:30:52 +01:00
|
|
|
} else if title := txt.FileTitle(filepath.Dir(m.OriginalName)); title != "" {
|
2020-05-31 14:42:41 +02:00
|
|
|
return title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:30:52 +01:00
|
|
|
// Generate title based on photo path, if any:
|
|
|
|
if m.PhotoPath != "" && !fs.IsGenerated(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-07-12 08:27:05 +02:00
|
|
|
loc := m.Cell
|
2020-04-18 23:20:54 +02:00
|
|
|
|
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 {
|
2021-08-19 21:12:38 +02:00
|
|
|
m.SetTitle(fmt.Sprintf("%s / %s", UnknownTitle, m.TakenAt.Format("2006")), SrcAuto)
|
2020-06-23 11:12:24 +02:00
|
|
|
} else {
|
2021-08-19 21:12:38 +02:00
|
|
|
m.SetTitle(UnknownTitle, SrcAuto)
|
2020-06-23 11:12:24 +02:00
|
|
|
}
|
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-12-17 10:23:17 +01:00
|
|
|
if (SrcPriority[source] < SrcPriority[m.TitleSrc]) && m.HasTitle() {
|
2020-04-26 16:22:29 +02:00
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTakenAt changes the photo date if not empty and from the same source.
|
|
|
|
func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) {
|
2020-08-18 10:42:09 +02:00
|
|
|
if taken.IsZero() || taken.Year() < 1000 || taken.Year() > txt.YearMax {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:23:17 +01:00
|
|
|
if SrcPriority[source] < SrcPriority[m.TakenSrc] && !m.TakenAt.IsZero() {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:36:23 +01:00
|
|
|
// Remove time zone if time was extracted from file name.
|
|
|
|
if source == SrcName {
|
|
|
|
zone = ""
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:18:37 +01:00
|
|
|
// Round times to avoid jitter.
|
|
|
|
taken = taken.Round(time.Second).UTC()
|
|
|
|
|
2021-01-26 13:00:54 +01:00
|
|
|
// Default local time to taken if zero or invalid.
|
|
|
|
if local.IsZero() || local.Year() < 1000 {
|
|
|
|
local = taken
|
2021-01-26 19:18:37 +01:00
|
|
|
} else {
|
|
|
|
local = local.Round(time.Second)
|
2021-01-26 13:00:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 21:41:57 +01:00
|
|
|
// Don't update older date.
|
2021-01-27 15:17:45 +01:00
|
|
|
if SrcPriority[source] <= SrcPriority[SrcAuto] && !m.TakenAt.IsZero() && taken.After(m.TakenAt) {
|
2021-01-25 21:41:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:00:54 +01:00
|
|
|
// Set UTC time and date source.
|
2021-01-25 21:41:57 +01:00
|
|
|
m.TakenAt = taken
|
2021-01-26 19:18:37 +01:00
|
|
|
m.TakenAtLocal = local
|
2020-04-26 16:22:29 +02:00
|
|
|
m.TakenSrc = source
|
|
|
|
|
2021-01-26 19:18:37 +01:00
|
|
|
if zone == time.UTC.String() && m.TimeZone != "" {
|
|
|
|
// Location exists, set local time from UTC.
|
|
|
|
m.TakenAtLocal = m.GetTakenAtLocal()
|
|
|
|
} else if zone != "" {
|
2021-01-26 13:00:54 +01:00
|
|
|
// Apply new time zone.
|
2020-04-26 16:22:29 +02:00
|
|
|
m.TimeZone = zone
|
2021-01-26 19:18:37 +01:00
|
|
|
m.TakenAt = m.GetTakenAt()
|
|
|
|
} else if m.TimeZone == time.UTC.String() {
|
|
|
|
// Local is UTC.
|
|
|
|
m.TimeZone = zone
|
|
|
|
m.TakenAtLocal = taken
|
|
|
|
} else if m.TimeZone != "" {
|
2021-01-26 13:00:54 +01:00
|
|
|
// Apply existing time zone.
|
2021-01-26 19:18:37 +01:00
|
|
|
m.TakenAtLocal = m.GetTakenAtLocal()
|
2021-01-25 21:41:57 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 07:41:33 +02:00
|
|
|
m.UpdateDateFields()
|
2020-05-15 16:06:32 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 23:39:46 +01:00
|
|
|
// UpdateTimeZone updates the time zone.
|
|
|
|
func (m *Photo) UpdateTimeZone(zone string) {
|
2021-01-27 23:31:13 +01:00
|
|
|
if zone == "" || zone == time.UTC.String() {
|
2021-01-19 13:47:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:47:37 +01:00
|
|
|
if SrcPriority[m.TakenSrc] >= SrcPriority[SrcManual] && m.TimeZone != "" {
|
2021-01-19 13:47:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:18:37 +01:00
|
|
|
if m.TimeZone == time.UTC.String() {
|
|
|
|
m.TimeZone = zone
|
|
|
|
m.TakenAtLocal = m.GetTakenAtLocal()
|
|
|
|
} else {
|
|
|
|
m.TimeZone = zone
|
|
|
|
m.TakenAt = m.GetTakenAt()
|
|
|
|
}
|
2021-01-19 13:47:41 +01: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
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:17:45 +01:00
|
|
|
// Set date to unknown if file system date is about the same as indexing time.
|
|
|
|
if m.TakenSrc == SrcAuto && m.TakenAt.After(m.CreatedAt.Add(-24*time.Hour)) {
|
2021-08-16 00:29:36 +02:00
|
|
|
m.PhotoYear = UnknownYear
|
|
|
|
m.PhotoMonth = UnknownMonth
|
|
|
|
m.PhotoDay = UnknownDay
|
2020-07-06 07:41:33 +02:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:38:35 +01:00
|
|
|
if SrcPriority[source] < SrcPriority[m.PlaceSrc] && m.HasLatLng() {
|
2020-04-26 16:22:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoLat = lat
|
|
|
|
m.PhotoLng = lng
|
|
|
|
m.PhotoAltitude = altitude
|
2020-07-12 08:27:05 +02:00
|
|
|
m.PlaceSrc = source
|
2020-04-26 16:22:29 +02:00
|
|
|
}
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
// SetCamera updates the camera.
|
|
|
|
func (m *Photo) SetCamera(camera *Camera, source string) {
|
|
|
|
if camera == nil {
|
|
|
|
log.Warnf("photo: failed updating camera from source '%s'", source)
|
|
|
|
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 {
|
|
|
|
log.Warnf("photo: failed updating lens from source '%s'", source)
|
|
|
|
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) {
|
|
|
|
if err := UnscopedDb().Where("files.photo_id = ?", m.ID).Find(&files).Error; err != nil {
|
|
|
|
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
|
|
|
|
|
|
|
if err := Db().Model(&PhotoAlbum{}).Where("photo_uid = ?", m.PhotoUID).UpdateColumn("hidden", true).Error; err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
return m.Updates(map[string]interface{}{"DeletedAt": TimeStamp(), "PhotoQuality": -1})
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete permanently deletes the entity from the database.
|
|
|
|
func (m *Photo) DeletePermanently() error {
|
|
|
|
Db().Unscoped().Delete(File{}, "photo_id = ?", m.ID)
|
2020-07-14 11:00:49 +02:00
|
|
|
Db().Unscoped().Delete(Details{}, "photo_id = ?", m.ID)
|
2020-05-07 19:42:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-07-14 11:00:49 +02:00
|
|
|
|
|
|
|
// PrimaryFile returns the primary file for this photo.
|
|
|
|
func (m *Photo) PrimaryFile() (File, error) {
|
|
|
|
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.
|
|
|
|
func (m *Photo) SetPrimary(fileUID string) error {
|
|
|
|
if m.PhotoUID == "" {
|
|
|
|
return fmt.Errorf("photo uid is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
|
|
|
|
if fileUID != "" {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := Db().Model(File{}).
|
|
|
|
Where("photo_uid = ? AND file_missing = 0 AND file_type = 'jpg'", m.PhotoUID).
|
|
|
|
Order("file_width DESC").Limit(1).
|
|
|
|
Pluck("file_uid", &files).Error; err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(files) == 0 {
|
2021-02-08 08:19:30 +01:00
|
|
|
return fmt.Errorf("%s has no jpeg", m.PhotoUID)
|
2021-01-24 20:40:40 +01:00
|
|
|
} else {
|
|
|
|
fileUID = files[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileUID == "" {
|
|
|
|
return fmt.Errorf("file uid is empty")
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:29 +01:00
|
|
|
Db().Model(File{}).Where("photo_uid = ? AND file_uid <> ?", m.PhotoUID, fileUID).UpdateColumn("file_primary", 0)
|
2021-01-24 20:40:40 +01:00
|
|
|
|
2021-02-08 07:39:29 +01:00
|
|
|
if err := Db().Model(File{}).Where("photo_uid = ? AND file_uid = ?", m.PhotoUID, fileUID).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
|
|
|
|
return m.UpdateQuality()
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if val := txt.Clip(s, txt.ClipVarchar); m.NoCameraSerial() && val != "" {
|
|
|
|
m.CameraSerial = val
|
|
|
|
}
|
|
|
|
}
|