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"
|
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
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
CameraSlug string `gorm:"type:varbinary(255);unique_index;" json:"Slug" yaml:"-"`
|
2020-05-29 18:04:30 +02:00
|
|
|
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"`
|
2020-05-23 20:58:58 +02:00
|
|
|
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"`
|
|
|
|
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{
|
|
|
|
CameraSlug: "zz",
|
2020-05-29 18:04:30 +02:00
|
|
|
CameraName: "Unknown",
|
|
|
|
CameraMake: "",
|
|
|
|
CameraModel: "Unknown",
|
|
|
|
}
|
|
|
|
|
|
|
|
var CameraMakes = map[string]string{
|
|
|
|
"OLYMPUS OPTICAL CO.,LTD": "Olympus",
|
|
|
|
}
|
|
|
|
|
|
|
|
var CameraModels = map[string]string{
|
|
|
|
"ELE-L29": "P30",
|
|
|
|
"ELE-AL00": "P30",
|
|
|
|
"ELE-L04": "P30",
|
|
|
|
"ELE-L09": "P30",
|
|
|
|
"ELE-TL00": "P30",
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnknownCamera initializes the database with an unknown camera if not exists
|
2020-04-30 20:07:03 +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-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 {
|
|
|
|
result := Camera{}
|
|
|
|
|
|
|
|
if err := Db().Where("camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
|
|
|
log.Errorf("camera: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if !m.Unknown() {
|
|
|
|
event.EntitiesCreated("cameras", []*Camera{m})
|
|
|
|
|
|
|
|
event.Publish("count.cameras", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// String returns a string designing the given Camera entity
|
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
|
|
|
}
|