2021-09-18 15:32:39 +02:00
|
|
|
package search
|
2020-05-08 15:41:01 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
)
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
// Photo represents a photo search result.
|
|
|
|
type Photo struct {
|
2020-05-27 13:40:21 +02:00
|
|
|
ID uint `json:"-"`
|
2021-01-10 15:30:12 +01:00
|
|
|
CompositeID string `json:"ID"`
|
2020-05-31 14:42:41 +02:00
|
|
|
UUID string `json:"DocumentID,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoUID string `json:"UID"`
|
|
|
|
PhotoType string `json:"Type"`
|
2020-07-14 12:42:31 +02:00
|
|
|
TypeSrc string `json:"TypeSrc"`
|
2020-05-23 20:58:58 +02:00
|
|
|
TakenAt time.Time `json:"TakenAt"`
|
|
|
|
TakenAtLocal time.Time `json:"TakenAtLocal"`
|
|
|
|
TakenSrc string `json:"TakenSrc"`
|
|
|
|
TimeZone string `json:"TimeZone"`
|
|
|
|
PhotoPath string `json:"Path"`
|
|
|
|
PhotoName string `json:"Name"`
|
2020-06-01 09:45:24 +02:00
|
|
|
OriginalName string `json:"OriginalName"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoTitle string `json:"Title"`
|
|
|
|
PhotoDescription string `json:"Description"`
|
|
|
|
PhotoYear int `json:"Year"`
|
|
|
|
PhotoMonth int `json:"Month"`
|
2020-07-06 07:41:33 +02:00
|
|
|
PhotoDay int `json:"Day"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoCountry string `json:"Country"`
|
2020-12-19 19:15:32 +01:00
|
|
|
PhotoStack int8 `json:"Stack"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoFavorite bool `json:"Favorite"`
|
|
|
|
PhotoPrivate bool `json:"Private"`
|
|
|
|
PhotoIso int `json:"Iso"`
|
|
|
|
PhotoFocalLength int `json:"FocalLength"`
|
|
|
|
PhotoFNumber float32 `json:"FNumber"`
|
|
|
|
PhotoExposure string `json:"Exposure"`
|
2021-05-26 09:51:00 +02:00
|
|
|
PhotoFaces int `json:"Faces,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoQuality int `json:"Quality"`
|
|
|
|
PhotoResolution int `json:"Resolution"`
|
2020-12-17 12:32:53 +01:00
|
|
|
PhotoColor uint8 `json:"Color"`
|
2020-07-06 14:35:25 +02:00
|
|
|
PhotoScan bool `json:"Scan"`
|
2020-07-16 13:02:48 +02:00
|
|
|
PhotoPanorama bool `json:"Panorama"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CameraID uint `json:"CameraID"` // Camera
|
2020-07-14 12:42:31 +02:00
|
|
|
CameraSerial string `json:"CameraSerial,omitempty"`
|
|
|
|
CameraSrc string `json:"CameraSrc,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CameraModel string `json:"CameraModel"`
|
|
|
|
CameraMake string `json:"CameraMake"`
|
|
|
|
LensID uint `json:"LensID"` // Lens
|
|
|
|
LensModel string `json:"LensModel"`
|
|
|
|
LensMake string `json:"LensMake"`
|
2020-07-06 10:32:37 +02:00
|
|
|
PhotoAltitude int `json:"Altitude,omitempty"`
|
|
|
|
PhotoLat float32 `json:"Lat"`
|
|
|
|
PhotoLng float32 `json:"Lng"`
|
2020-07-12 08:27:05 +02:00
|
|
|
CellID string `json:"CellID"` // Cell
|
|
|
|
CellAccuracy int `json:"CellAccuracy,omitempty"`
|
|
|
|
PlaceID string `json:"PlaceID"`
|
|
|
|
PlaceSrc string `json:"PlaceSrc"`
|
|
|
|
PlaceLabel string `json:"PlaceLabel"`
|
|
|
|
PlaceCity string `json:"PlaceCity"`
|
|
|
|
PlaceState string `json:"PlaceState"`
|
|
|
|
PlaceCountry string `json:"PlaceCountry"`
|
2020-08-18 00:45:53 +02:00
|
|
|
InstanceID string `json:"InstanceID"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileID uint `json:"-"` // File
|
|
|
|
FileUID string `json:"FileUID"`
|
2020-05-24 22:16:06 +02:00
|
|
|
FileRoot string `json:"FileRoot"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileName string `json:"FileName"`
|
|
|
|
FileHash string `json:"Hash"`
|
|
|
|
FileWidth int `json:"Width"`
|
|
|
|
FileHeight int `json:"Height"`
|
2020-12-16 15:52:44 +01:00
|
|
|
FilePortrait bool `json:"Portrait"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FilePrimary bool `json:"-"`
|
2021-01-27 21:30:10 +01:00
|
|
|
FileSidecar bool `json:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileMissing bool `json:"-"`
|
|
|
|
FileVideo bool `json:"-"`
|
|
|
|
FileDuration time.Duration `json:"-"`
|
|
|
|
FileCodec string `json:"-"`
|
|
|
|
FileType string `json:"-"`
|
|
|
|
FileMime string `json:"-"`
|
|
|
|
FileSize int64 `json:"-"`
|
|
|
|
FileOrientation int `json:"-"`
|
2020-07-16 13:02:48 +02:00
|
|
|
FileProjection string `json:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
FileAspectRatio float32 `json:"-"`
|
|
|
|
FileColors string `json:"-"`
|
|
|
|
FileChroma uint8 `json:"-"`
|
|
|
|
FileLuminance string `json:"-"`
|
|
|
|
FileDiff uint32 `json:"-"`
|
|
|
|
Merged bool `json:"Merged"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt"`
|
2020-06-24 08:34:23 +02:00
|
|
|
EditedAt time.Time `json:"EditedAt,omitempty"`
|
|
|
|
CheckedAt time.Time `json:"CheckedAt,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
DeletedAt time.Time `json:"DeletedAt,omitempty"`
|
|
|
|
|
|
|
|
Files []entity.File `json:"Files"`
|
2020-05-08 15:41:01 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
type PhotoResults []Photo
|
2020-05-08 15:41:01 +02:00
|
|
|
|
2020-06-14 11:39:53 +02:00
|
|
|
// UIDs returns a slice of photo UIDs.
|
|
|
|
func (m PhotoResults) UIDs() []string {
|
|
|
|
result := make([]string, len(m))
|
|
|
|
|
|
|
|
for i, el := range m {
|
|
|
|
result[i] = el.PhotoUID
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
func (m PhotoResults) Merged() (PhotoResults, int, error) {
|
2020-05-08 15:41:01 +02:00
|
|
|
count := len(m)
|
2021-09-18 15:32:39 +02:00
|
|
|
merged := make([]Photo, 0, count)
|
2020-05-08 15:41:01 +02:00
|
|
|
|
|
|
|
var lastId uint
|
|
|
|
var i int
|
|
|
|
|
|
|
|
for _, res := range m {
|
|
|
|
file := entity.File{}
|
|
|
|
|
|
|
|
if err := deepcopier.Copy(&file).From(res); err != nil {
|
|
|
|
return merged, count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file.ID = res.FileID
|
2021-09-18 20:41:30 +02:00
|
|
|
res.CompositeID = fmt.Sprintf("%d-%d", res.ID, res.FileID)
|
2020-05-08 15:41:01 +02:00
|
|
|
|
|
|
|
if lastId == res.ID && i > 0 {
|
|
|
|
merged[i-1].Files = append(merged[i-1].Files, file)
|
|
|
|
merged[i-1].Merged = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Files = append(res.Files, file)
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
merged = append(merged, res)
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
lastId = res.ID
|
2020-05-08 15:41:01 +02:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged, count, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
// ShareBase returns a meaningful file name for sharing.
|
2021-09-18 15:32:39 +02:00
|
|
|
func (m *Photo) ShareBase(seq int) string {
|
2020-05-08 15:41:01 +02:00
|
|
|
var name string
|
|
|
|
|
|
|
|
if m.PhotoTitle != "" {
|
|
|
|
name = strings.Title(slug.MakeLang(m.PhotoTitle, "en"))
|
|
|
|
} else {
|
2020-05-23 20:58:58 +02:00
|
|
|
name = m.PhotoUID
|
2020-05-08 15:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
taken := m.TakenAtLocal.Format("20060102-150405")
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
if seq > 0 {
|
|
|
|
return fmt.Sprintf("%s-%s (%d).%s", taken, name, seq, m.FileType)
|
|
|
|
}
|
2020-05-08 15:41:01 +02:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
return fmt.Sprintf("%s-%s.%s", taken, name, m.FileType)
|
2020-05-08 15:41:01 +02:00
|
|
|
}
|