2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-07-18 15:17:56 +02:00
|
|
|
|
|
|
|
import (
|
2019-05-14 18:16:35 +02:00
|
|
|
"fmt"
|
2020-12-16 11:59:16 +01:00
|
|
|
"path/filepath"
|
2021-09-06 05:13:53 +02:00
|
|
|
"sort"
|
2019-05-16 04:03:55 +02:00
|
|
|
"strings"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
|
|
|
|
2019-05-14 18:16:35 +02:00
|
|
|
"github.com/gosimple/slug"
|
2018-07-18 15:17:56 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2021-09-24 22:46:03 +02:00
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
2020-05-25 19:10:44 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2021-09-24 22:46:03 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-07-18 15:17:56 +02:00
|
|
|
)
|
|
|
|
|
2020-12-16 11:59:16 +01:00
|
|
|
type DownloadName string
|
|
|
|
|
|
|
|
const (
|
|
|
|
DownloadNameFile DownloadName = "file"
|
|
|
|
DownloadNameOriginal DownloadName = "original"
|
|
|
|
DownloadNameShare DownloadName = "share"
|
|
|
|
DownloadNameDefault = DownloadNameFile
|
|
|
|
)
|
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
type Files []File
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
// File represents an image or sidecar file that belongs to a photo.
|
2018-07-18 15:17:56 +02:00
|
|
|
type File struct {
|
2020-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"-" yaml:"-"`
|
|
|
|
Photo *Photo `json:"-" yaml:"-"`
|
|
|
|
PhotoID uint `gorm:"index;" json:"-" yaml:"-"`
|
2020-09-13 17:51:43 +02:00
|
|
|
PhotoUID string `gorm:"type:VARBINARY(42);index;" json:"PhotoUID" yaml:"PhotoUID"`
|
|
|
|
InstanceID string `gorm:"type:VARBINARY(42);index;" json:"InstanceID,omitempty" yaml:"InstanceID,omitempty"`
|
|
|
|
FileUID string `gorm:"type:VARBINARY(42);unique_index;" json:"UID" yaml:"UID"`
|
2020-12-15 20:14:06 +01:00
|
|
|
FileName string `gorm:"type:VARBINARY(755);unique_index:idx_files_name_root;" json:"Name" yaml:"Name"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FileRoot string `gorm:"type:VARBINARY(16);default:'/';unique_index:idx_files_name_root;" json:"Root" yaml:"Root,omitempty"`
|
2020-12-15 20:14:06 +01:00
|
|
|
OriginalName string `gorm:"type:VARBINARY(755);" json:"OriginalName" yaml:"OriginalName,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FileHash string `gorm:"type:VARBINARY(128);index" json:"Hash" yaml:"Hash,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileSize int64 `json:"Size" yaml:"Size,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FileCodec string `gorm:"type:VARBINARY(32)" json:"Codec" yaml:"Codec,omitempty"`
|
|
|
|
FileType string `gorm:"type:VARBINARY(32)" json:"Type" yaml:"Type,omitempty"`
|
|
|
|
FileMime string `gorm:"type:VARBINARY(64)" json:"Mime" yaml:"Mime,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FilePrimary bool `json:"Primary" yaml:"Primary,omitempty"`
|
|
|
|
FileSidecar bool `json:"Sidecar" yaml:"Sidecar,omitempty"`
|
|
|
|
FileMissing bool `json:"Missing" yaml:"Missing,omitempty"`
|
|
|
|
FilePortrait bool `json:"Portrait" yaml:"Portrait,omitempty"`
|
|
|
|
FileVideo bool `json:"Video" yaml:"Video,omitempty"`
|
|
|
|
FileDuration time.Duration `json:"Duration" yaml:"Duration,omitempty"`
|
|
|
|
FileWidth int `json:"Width" yaml:"Width,omitempty"`
|
|
|
|
FileHeight int `json:"Height" yaml:"Height,omitempty"`
|
|
|
|
FileOrientation int `json:"Orientation" yaml:"Orientation,omitempty"`
|
2021-09-03 19:02:26 +02:00
|
|
|
FileProjection string `gorm:"type:VARBINARY(32);" json:"Projection,omitempty" yaml:"Projection,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileAspectRatio float32 `gorm:"type:FLOAT;" json:"AspectRatio" yaml:"AspectRatio,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
FileMainColor string `gorm:"type:VARBINARY(16);index;" json:"MainColor" yaml:"MainColor,omitempty"`
|
|
|
|
FileColors string `gorm:"type:VARBINARY(9);" json:"Colors" yaml:"Colors,omitempty"`
|
|
|
|
FileLuminance string `gorm:"type:VARBINARY(9);" json:"Luminance" yaml:"Luminance,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileDiff uint32 `json:"Diff" yaml:"Diff,omitempty"`
|
|
|
|
FileChroma uint8 `json:"Chroma" yaml:"Chroma,omitempty"`
|
2020-12-15 20:14:06 +01:00
|
|
|
FileError string `gorm:"type:VARBINARY(512)" json:"Error" yaml:"Error,omitempty"`
|
2020-07-17 16:09:55 +02:00
|
|
|
ModTime int64 `json:"ModTime" yaml:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
|
|
|
|
CreatedIn int64 `json:"CreatedIn" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
|
|
|
UpdatedIn int64 `json:"UpdatedIn" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"DeletedAt,omitempty" yaml:"-"`
|
2020-07-17 16:09:55 +02:00
|
|
|
Share []FileShare `json:"-" yaml:"-"`
|
|
|
|
Sync []FileSync `json:"-" yaml:"-"`
|
2021-09-02 11:12:42 +02:00
|
|
|
markers *Markers
|
2019-05-14 18:16:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 08:56:35 +02:00
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (File) TableName() string {
|
|
|
|
return "files"
|
|
|
|
}
|
|
|
|
|
2020-05-16 17:07:44 +02:00
|
|
|
type FileInfos struct {
|
|
|
|
FileWidth int
|
|
|
|
FileHeight int
|
|
|
|
FileOrientation int
|
|
|
|
FileAspectRatio float32
|
|
|
|
FileMainColor string
|
|
|
|
FileColors string
|
|
|
|
FileLuminance string
|
|
|
|
FileDiff uint32
|
|
|
|
FileChroma uint8
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// FirstFileByHash gets a file in db from its hash
|
2020-04-30 20:07:03 +02:00
|
|
|
func FirstFileByHash(fileHash string) (File, error) {
|
2019-07-03 09:27:30 +02:00
|
|
|
var file File
|
|
|
|
|
2021-02-05 21:12:40 +01:00
|
|
|
res := Db().Unscoped().First(&file, "file_hash = ?", fileHash)
|
2019-07-03 09:27:30 +02:00
|
|
|
|
2021-02-05 21:12:40 +01:00
|
|
|
return file, res.Error
|
2019-07-03 09:27:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
// PrimaryFile returns the primary file for a photo uid.
|
2021-09-02 14:23:40 +02:00
|
|
|
func PrimaryFile(photoUID string) (*File, error) {
|
|
|
|
file := File{}
|
2020-07-14 11:00:49 +02:00
|
|
|
|
2021-02-05 21:12:40 +01:00
|
|
|
res := Db().Unscoped().First(&file, "file_primary = 1 AND photo_uid = ?", photoUID)
|
2020-07-14 11:00:49 +02:00
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
return &file, res.Error
|
2020-07-14 11:00:49 +02:00
|
|
|
}
|
|
|
|
|
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 *File) BeforeCreate(scope *gorm.Scope) error {
|
2020-05-27 13:40:21 +02:00
|
|
|
if rnd.IsUID(m.FileUID, 'f') {
|
2020-05-01 12:57:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
return scope.SetColumn("FileUID", rnd.PPID('f'))
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
// DownloadName returns the download file name.
|
|
|
|
func (m *File) DownloadName(n DownloadName, seq int) string {
|
|
|
|
switch n {
|
|
|
|
case DownloadNameFile:
|
|
|
|
return m.Base(seq)
|
|
|
|
case DownloadNameOriginal:
|
|
|
|
return m.OriginalBase(seq)
|
|
|
|
default:
|
|
|
|
return m.ShareBase(seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:59:16 +01:00
|
|
|
// Base returns the file name without path.
|
2021-01-27 21:30:10 +01:00
|
|
|
func (m *File) Base(seq int) string {
|
2020-12-16 11:59:16 +01:00
|
|
|
if m.FileName == "" {
|
2021-01-27 21:30:10 +01:00
|
|
|
return m.ShareBase(seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
base := filepath.Base(m.FileName)
|
|
|
|
|
|
|
|
if seq > 0 {
|
|
|
|
return fmt.Sprintf("%s (%d)%s", fs.StripExt(base), seq, filepath.Ext(base))
|
2020-12-16 11:59:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
return base
|
2020-12-16 11:59:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OriginalBase returns the original file name without path.
|
2021-01-27 21:30:10 +01:00
|
|
|
func (m *File) OriginalBase(seq int) string {
|
2020-12-16 11:59:16 +01:00
|
|
|
if m.OriginalName == "" {
|
2021-01-27 21:30:10 +01:00
|
|
|
return m.Base(seq)
|
2020-12-16 11:59:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
base := filepath.Base(m.OriginalName)
|
|
|
|
|
|
|
|
if seq > 0 {
|
|
|
|
return fmt.Sprintf("%s (%d)%s", fs.StripExt(base), seq, filepath.Ext(base))
|
|
|
|
}
|
|
|
|
|
|
|
|
return base
|
2020-12-16 11:59:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
// ShareBase returns a meaningful file name for sharing.
|
|
|
|
func (m *File) ShareBase(seq int) string {
|
2020-05-25 19:10:44 +02:00
|
|
|
photo := m.RelatedPhoto()
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if photo == nil {
|
|
|
|
return fmt.Sprintf("%s.%s", m.FileHash, m.FileType)
|
|
|
|
} else if len(m.FileHash) < 8 {
|
|
|
|
return fmt.Sprintf("%s.%s", rnd.UUID(), m.FileType)
|
|
|
|
} else if photo.TakenAtLocal.IsZero() || photo.PhotoTitle == "" {
|
|
|
|
return fmt.Sprintf("%s.%s", m.FileHash, m.FileType)
|
2019-05-16 04:03:55 +02:00
|
|
|
}
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
name := strings.Title(slug.MakeLang(photo.PhotoTitle, "en"))
|
|
|
|
taken := photo.TakenAtLocal.Format("20060102-150405")
|
2019-05-16 04:03:55 +02:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
if seq > 0 {
|
|
|
|
return fmt.Sprintf("%s-%s (%d).%s", taken, name, seq, m.FileType)
|
|
|
|
}
|
2019-05-14 18:16:35 +02:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
return fmt.Sprintf("%s-%s.%s", taken, name, m.FileType)
|
2018-07-18 15:17:56 +02:00
|
|
|
}
|
2020-02-01 20:52:28 +01:00
|
|
|
|
2020-03-25 12:39:07 +01:00
|
|
|
// Changed returns true if new and old file size or modified time are different.
|
2020-07-17 16:09:55 +02:00
|
|
|
func (m File) Changed(fileSize int64, modTime time.Time) bool {
|
2020-11-20 17:25:46 +01:00
|
|
|
// File size has changed.
|
2020-02-01 20:52:28 +01:00
|
|
|
if m.FileSize != fileSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:25:46 +01:00
|
|
|
// Modification time has changed.
|
2020-07-17 16:09:55 +02:00
|
|
|
if m.ModTime == modTime.Unix() {
|
2020-04-14 15:08:39 +02:00
|
|
|
return false
|
2020-02-01 20:52:28 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:08:39 +02:00
|
|
|
return true
|
2020-02-01 20:52:28 +01:00
|
|
|
}
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2020-11-21 15:43:13 +01:00
|
|
|
// Missing returns true if this file is current missing or marked as deleted.
|
|
|
|
func (m File) Missing() bool {
|
|
|
|
return m.FileMissing || m.DeletedAt != nil
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// DeletePermanently permanently removes a file from the index.
|
2020-10-04 14:21:40 +02:00
|
|
|
func (m *File) DeletePermanently() error {
|
2021-09-30 15:50:10 +02:00
|
|
|
if m.ID < 1 || m.FileUID == "" {
|
|
|
|
return fmt.Errorf("invalid file id %d / uid %s", m.ID, txt.Quote(m.FileUID))
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
if err := UnscopedDb().Delete(Marker{}, "file_uid = ?", m.FileUID).Error; err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while removing markers", txt.Quote(m.FileUID), err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(FileShare{}, "file_id = ?", m.ID).Error; err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while removing share info", txt.Quote(m.FileUID), err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Delete(FileSync{}, "file_id = ?", m.ID).Error; err != nil {
|
2021-10-05 22:44:27 +02:00
|
|
|
log.Errorf("file %s: %s while removing remote sync info", txt.Quote(m.FileUID), err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.ReplaceHash(""); err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while removing covers", txt.Quote(m.FileUID), err)
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
2020-10-04 14:21:40 +02:00
|
|
|
|
2021-09-24 13:13:59 +02:00
|
|
|
return UnscopedDb().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceHash updates file hash references.
|
2021-09-24 22:46:03 +02:00
|
|
|
func (m *File) ReplaceHash(newHash string) error {
|
|
|
|
if m.FileHash == newHash {
|
2021-09-24 13:13:59 +02:00
|
|
|
// Nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:46:03 +02:00
|
|
|
// Log values.
|
|
|
|
if m.FileHash != "" && newHash == "" {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Tracef("file %s: removing hash %s", txt.Quote(m.FileUID), txt.Quote(m.FileHash))
|
2021-09-24 22:46:03 +02:00
|
|
|
} else if m.FileHash != "" && newHash != "" {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Tracef("file %s: hash %s changed to %s", txt.Quote(m.FileUID), txt.Quote(m.FileHash), txt.Quote(newHash))
|
2021-10-05 20:51:18 +02:00
|
|
|
// Reset error when hash changes.
|
|
|
|
m.FileError = ""
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:46:03 +02:00
|
|
|
// Set file hash to new value.
|
|
|
|
oldHash := m.FileHash
|
|
|
|
m.FileHash = newHash
|
|
|
|
|
|
|
|
// Ok to skip updating related tables?
|
2021-09-24 13:13:59 +02:00
|
|
|
if m.NoJPEG() || m.FileHash == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entities := Types{
|
2021-09-24 13:34:37 +02:00
|
|
|
"albums": Album{},
|
|
|
|
"labels": Label{},
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search related tables for references and update them.
|
|
|
|
for name, entity := range entities {
|
|
|
|
start := time.Now()
|
|
|
|
|
2021-09-24 22:46:03 +02:00
|
|
|
if res := UnscopedDb().Model(entity).Where("thumb = ?", oldHash).UpdateColumn("thumb", newHash); res.Error != nil {
|
2021-09-24 13:13:59 +02:00
|
|
|
return res.Error
|
2021-10-01 00:05:49 +02:00
|
|
|
} else if res.RowsAffected > 0 {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Infof("%s: updated %s [%s]", name, english.Plural(int(res.RowsAffected), "cover", "covers"), time.Since(start))
|
2021-09-24 13:13:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-10-04 14:21:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the entity from the database.
|
|
|
|
func (m *File) Delete(permanently bool) error {
|
2021-09-30 15:50:10 +02:00
|
|
|
if m.ID < 1 || m.FileUID == "" {
|
|
|
|
return fmt.Errorf("invalid file id %d / uid %s", m.ID, txt.Quote(m.FileUID))
|
|
|
|
}
|
|
|
|
|
2020-10-04 14:21:40 +02:00
|
|
|
if permanently {
|
|
|
|
return m.DeletePermanently()
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
2020-05-07 19:42:04 +02:00
|
|
|
// Purge removes a file from the index by marking it as missing.
|
|
|
|
func (m *File) Purge() error {
|
2021-08-29 13:26:05 +02:00
|
|
|
deletedAt := TimeStamp()
|
2020-12-11 17:21:13 +01:00
|
|
|
m.FileMissing = true
|
|
|
|
m.FilePrimary = false
|
2021-01-24 20:40:40 +01:00
|
|
|
m.DeletedAt = &deletedAt
|
|
|
|
return UnscopedDb().Exec("UPDATE files SET file_missing = 1, file_primary = 0, deleted_at = ? WHERE id = ?", &deletedAt, m.ID).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found restores a previously purged file.
|
|
|
|
func (m *File) Found() error {
|
|
|
|
m.FileMissing = false
|
|
|
|
m.DeletedAt = nil
|
|
|
|
return UnscopedDb().Exec("UPDATE files SET file_missing = 0, deleted_at = NULL WHERE id = ?", m.ID).Error
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
2020-05-13 15:36:42 +02:00
|
|
|
|
|
|
|
// AllFilesMissing returns true, if all files for the photo of this file are missing.
|
|
|
|
func (m *File) AllFilesMissing() bool {
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
if err := Db().Model(&File{}).
|
2020-05-18 16:36:24 +02:00
|
|
|
Where("photo_id = ? AND file_missing = 0", m.PhotoID).
|
2020-05-13 15:36:42 +02:00
|
|
|
Count(&count).Error; err != nil {
|
2020-05-18 16:36:24 +02:00
|
|
|
log.Errorf("file: %s", err.Error())
|
2020-05-13 15:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *File) Create() error {
|
|
|
|
if m.PhotoID == 0 {
|
2021-10-05 18:42:39 +02:00
|
|
|
return fmt.Errorf("file: can't create file with empty photo id")
|
2020-07-06 19:15:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
if err := UnscopedDb().Create(m).Error; err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file: %s while saving", err)
|
2020-07-07 10:51:55 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
if _, err := m.SaveMarkers(); err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while saving markers", txt.Quote(m.FileUID), err)
|
2021-05-26 14:41:59 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
return nil
|
2020-07-06 19:15:57 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 17:21:13 +01:00
|
|
|
// ResolvePrimary ensures there is only one primary file for a photo..
|
|
|
|
func (m *File) ResolvePrimary() error {
|
|
|
|
if m.FilePrimary {
|
|
|
|
return UnscopedDb().Exec("UPDATE `files` SET file_primary = (id = ?) WHERE photo_id = ?", m.ID, m.PhotoID).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
// Save stores the file in the database.
|
2020-05-13 15:36:42 +02:00
|
|
|
func (m *File) Save() error {
|
2020-05-18 17:24:54 +02:00
|
|
|
if m.PhotoID == 0 {
|
2021-10-05 18:42:39 +02:00
|
|
|
return fmt.Errorf("file %s: can't save file with empty photo id", m.FileUID)
|
2020-07-07 10:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnscopedDb().Save(m).Error; err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while saving", txt.Quote(m.FileUID), err)
|
2020-07-07 10:51:55 +02:00
|
|
|
return err
|
2020-05-18 17:24:54 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
if _, err := m.SaveMarkers(); err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Errorf("file %s: %s while saving markers", txt.Quote(m.FileUID), err)
|
2021-05-26 14:41:59 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:21:13 +01:00
|
|
|
return m.ResolvePrimary()
|
2020-05-13 15:36:42 +02:00
|
|
|
}
|
2020-05-16 17:07:44 +02:00
|
|
|
|
|
|
|
// UpdateVideoInfos updates related video infos based on this file.
|
|
|
|
func (m *File) UpdateVideoInfos() error {
|
|
|
|
values := FileInfos{}
|
|
|
|
|
|
|
|
if err := deepcopier.Copy(&values).From(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Model(File{}).Where("photo_id = ? AND file_video = 1", m.PhotoID).Updates(values).Error
|
|
|
|
}
|
2020-05-25 19:10:44 +02:00
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
// Update updates a column in the database.
|
2020-05-25 19:10:44 +02:00
|
|
|
func (m *File) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
2020-11-15 15:15:56 +01:00
|
|
|
// Updates multiple columns in the database.
|
2020-11-20 17:25:46 +01:00
|
|
|
func (m *File) Updates(values interface{}) error {
|
2020-11-15 15:15:56 +01:00
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:43:13 +01:00
|
|
|
// Rename updates the name and path of this file.
|
2020-11-20 17:25:46 +01:00
|
|
|
func (m *File) Rename(fileName, rootName, filePath, fileBase string) error {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Debugf("file %s: renaming %s to %s", txt.Quote(m.FileUID), txt.Quote(m.FileName), txt.Quote(fileName))
|
2020-11-21 18:33:19 +01:00
|
|
|
|
2020-11-21 15:43:13 +01:00
|
|
|
// Update database row.
|
2020-11-20 17:25:46 +01:00
|
|
|
if err := m.Updates(map[string]interface{}{
|
2020-11-21 17:36:41 +01:00
|
|
|
"FileName": fileName,
|
|
|
|
"FileRoot": rootName,
|
2020-11-21 15:43:13 +01:00
|
|
|
"FileMissing": false,
|
2020-11-21 17:36:41 +01:00
|
|
|
"DeletedAt": nil,
|
|
|
|
}); err != nil {
|
2020-11-20 17:25:46 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:43:13 +01:00
|
|
|
m.FileName = fileName
|
|
|
|
m.FileRoot = rootName
|
|
|
|
m.FileMissing = false
|
|
|
|
m.DeletedAt = nil
|
|
|
|
|
2020-11-20 17:25:46 +01:00
|
|
|
// Update photo path and name if possible.
|
|
|
|
if p := m.RelatedPhoto(); p != nil {
|
|
|
|
return p.Updates(map[string]interface{}{
|
|
|
|
"PhotoPath": filePath,
|
|
|
|
"PhotoName": fileBase,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-15 15:15:56 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 15:43:13 +01:00
|
|
|
// Undelete removes the missing flag from this file.
|
|
|
|
func (m *File) Undelete() error {
|
|
|
|
if !m.Missing() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update database row.
|
|
|
|
err := m.Updates(map[string]interface{}{
|
|
|
|
"FileMissing": false,
|
2020-11-21 17:36:41 +01:00
|
|
|
"DeletedAt": nil,
|
2020-11-21 15:43:13 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Debugf("file %s: removed missing flag from %s", txt.Quote(m.FileUID), txt.Quote(m.FileName))
|
2020-11-21 15:43:13 +01:00
|
|
|
|
|
|
|
m.FileMissing = false
|
|
|
|
m.DeletedAt = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// RelatedPhoto returns the related photo entity.
|
|
|
|
func (m *File) RelatedPhoto() *Photo {
|
|
|
|
if m.Photo != nil {
|
|
|
|
return m.Photo
|
|
|
|
}
|
|
|
|
|
|
|
|
photo := Photo{}
|
|
|
|
|
|
|
|
UnscopedDb().Model(m).Related(&photo)
|
|
|
|
|
|
|
|
return &photo
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoJPEG returns true if the file is not a JPEG image file.
|
|
|
|
func (m *File) NoJPEG() bool {
|
2020-12-12 17:20:31 +01:00
|
|
|
return m.FileType != string(fs.FormatJpeg)
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
// Links returns all share links for this entity.
|
|
|
|
func (m *File) Links() Links {
|
|
|
|
return FindLinks("", m.FileUID)
|
|
|
|
}
|
2020-07-16 13:02:48 +02:00
|
|
|
|
|
|
|
// Panorama tests if the file seems to be a panorama image.
|
|
|
|
func (m *File) Panorama() bool {
|
|
|
|
if m.FileSidecar || m.FileWidth <= 1000 || m.FileHeight <= 500 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-03 19:02:26 +02:00
|
|
|
return m.Projection() != ProjDefault || (m.FileWidth/m.FileHeight) >= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Projection returns the panorama projection type string.
|
|
|
|
func (m *File) Projection() string {
|
|
|
|
return SanitizeTypeString(m.FileProjection)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetProjection sets the panorama projection type string.
|
|
|
|
func (m *File) SetProjection(projType string) {
|
|
|
|
m.FileProjection = SanitizeTypeString(projType)
|
2020-07-16 13:02:48 +02:00
|
|
|
}
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
// AddFaces adds face markers to the file.
|
|
|
|
func (m *File) AddFaces(faces face.Faces) {
|
2021-09-06 05:13:53 +02:00
|
|
|
sort.Slice(faces, func(i, j int) bool {
|
|
|
|
return faces[i].Size() > faces[j].Size()
|
|
|
|
})
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
for _, f := range faces {
|
|
|
|
m.AddFace(f, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFace adds a face marker to the file.
|
2021-09-17 14:26:12 +02:00
|
|
|
func (m *File) AddFace(f face.Face, subjUID string) {
|
2021-09-30 13:44:23 +02:00
|
|
|
// Only add faces with exactly one embedding so that they can be compared and clustered.
|
|
|
|
if !f.Embeddings.One() {
|
2021-09-22 19:33:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new marker from face.
|
|
|
|
marker := NewFaceMarker(f, *m, subjUID)
|
2021-09-02 11:12:42 +02:00
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
// Failed creating new marker?
|
|
|
|
if marker == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append marker if it doesn't conflict with existing marker.
|
|
|
|
if markers := m.Markers(); !markers.Contains(*marker) {
|
2021-09-30 13:44:23 +02:00
|
|
|
markers.AppendWithEmbedding(*marker)
|
2021-05-31 15:40:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// ValidFaceCount returns the number of valid face markers.
|
|
|
|
func (m *File) ValidFaceCount() (c int) {
|
|
|
|
return ValidFaceCount(m.FileUID)
|
2021-09-22 19:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePhotoFaceCount updates the faces count in the index and returns it if the file is primary.
|
|
|
|
func (m *File) UpdatePhotoFaceCount() (c int, err error) {
|
|
|
|
// Primary file of an existing photo?
|
|
|
|
if !m.FilePrimary || m.PhotoID == 0 {
|
|
|
|
return 0, nil
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
2021-09-22 19:33:41 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
c = m.ValidFaceCount()
|
2021-09-22 19:33:41 +02:00
|
|
|
|
|
|
|
err = UnscopedDb().Model(Photo{}).
|
|
|
|
Where("id = ?", m.PhotoID).
|
|
|
|
UpdateColumn("photo_faces", c).Error
|
|
|
|
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveMarkers updates markers in the index.
|
|
|
|
func (m *File) SaveMarkers() (count int, err error) {
|
|
|
|
if m.markers == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.markers.Save(m)
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 11:12:42 +02:00
|
|
|
// Markers finds and returns existing file markers.
|
|
|
|
func (m *File) Markers() *Markers {
|
|
|
|
if m.markers != nil {
|
|
|
|
return m.markers
|
2021-09-22 19:33:41 +02:00
|
|
|
} else if m.FileUID == "" {
|
|
|
|
m.markers = &Markers{}
|
|
|
|
} else if res, err := FindMarkers(m.FileUID); err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Warnf("file %s: %s while loading markers", txt.Quote(m.FileUID), err)
|
2021-09-02 11:12:42 +02:00
|
|
|
m.markers = &Markers{}
|
2021-05-31 15:40:52 +02:00
|
|
|
} else {
|
2021-09-02 11:12:42 +02:00
|
|
|
m.markers = &res
|
2021-05-31 15:40:52 +02:00
|
|
|
}
|
2021-09-02 11:12:42 +02:00
|
|
|
|
|
|
|
return m.markers
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
// UnsavedMarkers tests if any marker hasn't been saved yet.
|
|
|
|
func (m *File) UnsavedMarkers() bool {
|
|
|
|
if m.markers == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.markers.Unsaved()
|
|
|
|
}
|
|
|
|
|
2021-09-02 11:12:42 +02:00
|
|
|
// SubjectNames returns all known subject names.
|
|
|
|
func (m *File) SubjectNames() []string {
|
|
|
|
return m.Markers().SubjectNames()
|
2021-05-26 14:41:59 +02:00
|
|
|
}
|