2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-08-15 09:59:51 +02:00
|
|
|
|
|
|
|
import (
|
2019-05-16 08:41:16 +02:00
|
|
|
"strings"
|
2020-12-13 15:43:01 +01:00
|
|
|
"sync"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
2019-05-04 17:34:51 +02:00
|
|
|
|
2018-09-13 20:54:34 +02:00
|
|
|
"github.com/gosimple/slug"
|
2020-05-29 18:04:30 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-26 14:31:33 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-08-15 09:59:51 +02:00
|
|
|
)
|
|
|
|
|
2020-12-13 15:43:01 +01:00
|
|
|
var cameraMutex = sync.Mutex{}
|
|
|
|
|
2021-09-02 14:45:26 +02:00
|
|
|
// Cameras represents a list of cameras.
|
|
|
|
type Cameras []Camera
|
|
|
|
|
2019-12-09 08:04:41 +01:00
|
|
|
// Camera model and make (as extracted from UpdateExif metadata)
|
2018-08-15 09:59:51 +02:00
|
|
|
type Camera struct {
|
2020-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"ID"`
|
2020-09-13 17:51:43 +02:00
|
|
|
CameraSlug string `gorm:"type:VARBINARY(255);unique_index;" json:"Slug" yaml:"-"`
|
|
|
|
CameraName string `gorm:"type:VARCHAR(255);" json:"Name" yaml:"Name"`
|
|
|
|
CameraMake string `gorm:"type:VARCHAR(255);" json:"Make" yaml:"Make,omitempty"`
|
|
|
|
CameraModel string `gorm:"type:VARCHAR(255);" json:"Model" yaml:"Model,omitempty"`
|
|
|
|
CameraType string `gorm:"type:VARCHAR(255);" json:"Type,omitempty" yaml:"Type,omitempty"`
|
|
|
|
CameraDescription string `gorm:"type:TEXT;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
CameraNotes string `gorm:"type:TEXT;" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CreatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"-" yaml:"-"`
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
var UnknownCamera = Camera{
|
2021-08-19 21:12:38 +02:00
|
|
|
CameraSlug: UnknownID,
|
2020-05-29 18:04:30 +02:00
|
|
|
CameraName: "Unknown",
|
|
|
|
CameraMake: "",
|
|
|
|
CameraModel: "Unknown",
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
// CreateUnknownCamera initializes the database with an unknown camera if not exists
|
2020-07-13 15:23:54 +02:00
|
|
|
func CreateUnknownCamera() {
|
2020-05-24 22:16:06 +02:00
|
|
|
FirstOrCreateCamera(&UnknownCamera)
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewCamera creates a camera entity from a model name and a make name.
|
2018-09-24 19:07:43 +02:00
|
|
|
func NewCamera(modelName string, makeName string) *Camera {
|
2020-04-26 14:31:33 +02:00
|
|
|
modelName = txt.Clip(modelName, txt.ClipDefault)
|
|
|
|
makeName = txt.Clip(makeName, txt.ClipDefault)
|
2019-05-16 08:41:16 +02:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if modelName == "" && makeName == "" {
|
2020-04-25 14:22:47 +02:00
|
|
|
return &UnknownCamera
|
2019-05-16 08:41:16 +02:00
|
|
|
} else if strings.HasPrefix(modelName, makeName) {
|
|
|
|
modelName = strings.TrimSpace(modelName[len(makeName):])
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if n, ok := CameraMakes[makeName]; ok {
|
|
|
|
makeName = n
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := CameraModels[modelName]; ok {
|
|
|
|
modelName = n
|
|
|
|
}
|
|
|
|
|
|
|
|
var name []string
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
if makeName != "" {
|
2020-05-29 18:04:30 +02:00
|
|
|
name = append(name, makeName)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
2018-09-13 20:54:34 +02:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if modelName != "" {
|
|
|
|
name = append(name, modelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
cameraName := strings.Join(name, " ")
|
|
|
|
cameraSlug := slug.Make(txt.Clip(cameraName, txt.ClipSlug))
|
|
|
|
|
2018-08-15 09:59:51 +02:00
|
|
|
result := &Camera{
|
2018-09-17 18:40:57 +02:00
|
|
|
CameraSlug: cameraSlug,
|
2020-05-29 18:04:30 +02:00
|
|
|
CameraName: cameraName,
|
|
|
|
CameraMake: makeName,
|
|
|
|
CameraModel: modelName,
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Camera) Create() error {
|
2020-12-13 15:43:01 +01:00
|
|
|
cameraMutex.Lock()
|
|
|
|
defer cameraMutex.Unlock()
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
return Db().Create(m).Error
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// FirstOrCreateCamera returns the existing row, inserts a new row or nil in case of errors.
|
2020-05-24 22:16:06 +02:00
|
|
|
func FirstOrCreateCamera(m *Camera) *Camera {
|
2020-12-14 19:24:08 +01:00
|
|
|
if m.CameraSlug == "" {
|
|
|
|
return &UnknownCamera
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
if cacheData, ok := cameraCache.Get(m.CameraSlug); ok {
|
|
|
|
log.Debugf("camera: cache hit for %s", m.CameraSlug)
|
|
|
|
|
|
|
|
return cacheData.(*Camera)
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
result := Camera{}
|
|
|
|
|
2020-12-14 19:24:08 +01:00
|
|
|
if res := Db().Where("camera_slug = ?", m.CameraSlug).First(&result); res.Error == nil {
|
2021-02-06 16:30:30 +01:00
|
|
|
cameraCache.SetDefault(m.CameraSlug, &result)
|
2020-05-24 22:16:06 +02:00
|
|
|
return &result
|
2020-12-13 15:43:01 +01:00
|
|
|
} else if err := m.Create(); err == nil {
|
2020-07-07 20:44:33 +02:00
|
|
|
if !m.Unknown() {
|
|
|
|
event.EntitiesCreated("cameras", []*Camera{m})
|
2020-05-24 22:16:06 +02:00
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
event.Publish("count.cameras", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 18:04:30 +02:00
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
cameraCache.SetDefault(m.CameraSlug, m)
|
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return m
|
2020-12-14 19:24:08 +01:00
|
|
|
} else if res := Db().Where("camera_slug = ?", m.CameraSlug).First(&result); res.Error == nil {
|
2021-02-06 16:30:30 +01:00
|
|
|
cameraCache.SetDefault(m.CameraSlug, &result)
|
2020-07-07 20:44:33 +02:00
|
|
|
return &result
|
|
|
|
} else {
|
2020-12-13 15:43:01 +01:00
|
|
|
log.Errorf("camera: %s (create %s)", err.Error(), txt.Quote(m.String()))
|
2020-05-29 18:04:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:24:08 +01:00
|
|
|
return &UnknownCamera
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
// String returns an identifier that can be used in logs.
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Camera) String() string {
|
2020-05-29 18:04:30 +02:00
|
|
|
return m.CameraName
|
|
|
|
}
|
2019-05-04 17:34:51 +02:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
// Unknown returns true if the camera is not a known make or model.
|
|
|
|
func (m *Camera) Unknown() bool {
|
|
|
|
return m.CameraSlug == "" || m.CameraSlug == UnknownCamera.CameraSlug
|
2019-05-04 17:34:51 +02:00
|
|
|
}
|