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",
|
2020-06-04 14:56:27 +02:00
|
|
|
"samsung": "Samsung",
|
2020-05-29 18:04:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var CameraModels = map[string]string{
|
2020-06-04 14:56:27 +02:00
|
|
|
"ELE-L29": "P30",
|
|
|
|
"ELE-AL00": "P30",
|
|
|
|
"ELE-L04": "P30",
|
|
|
|
"ELE-L09": "P30",
|
|
|
|
"ELE-TL00": "P30",
|
|
|
|
"VOG-L29": "P30 Pro",
|
|
|
|
"VOG-L09": "P30 Pro",
|
|
|
|
"VOG-L04": "P30 Pro",
|
|
|
|
"VOG-AL00": "P30 Pro",
|
|
|
|
"VOG-AL10": "P30 Pro",
|
|
|
|
"VOG-TL00": "P30 Pro",
|
|
|
|
"MAR-L01A": "P30 Lite",
|
|
|
|
"MAR-L21A": "P30 Lite",
|
|
|
|
"MAR-LX1A": "P30 Lite",
|
|
|
|
"MAR-LX1M": "P30 Lite",
|
|
|
|
"MAR-LX2": "P30 Lite",
|
|
|
|
"MAR-L21MEA": "P30 Lite",
|
|
|
|
"MAR-L22A": "P30 Lite",
|
|
|
|
"MAR-L22B": "P30 Lite",
|
|
|
|
"MAR-LX3A": "P30 Lite",
|
|
|
|
"ANA-AN00": "P40",
|
|
|
|
"ANA-TN00": "P40",
|
|
|
|
"ELS-AN00": "P40 Pro",
|
|
|
|
"ELS-TN00": "P40 Pro",
|
|
|
|
"ELS-NX9": "P40 Pro",
|
|
|
|
"ELS-N04": "P40 Pro",
|
|
|
|
"JNY-L21A": "P40 Lite",
|
|
|
|
"JNY-L01A": "P40 Lite",
|
|
|
|
"JNY-L21B": "P40 Lite",
|
|
|
|
"JNY-L22A": "P40 Lite",
|
|
|
|
"JNY-L02A": "P40 Lite",
|
|
|
|
"JNY-L22B": "P40 Lite",
|
|
|
|
"STK-LX1": "Honor 9X",
|
|
|
|
"HLK-AL00": "Honor 9X",
|
|
|
|
"HLK-TL00": "Honor 9X",
|
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-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
|
2020-07-07 20:44:33 +02:00
|
|
|
} else if createErr := m.Create(); createErr == nil {
|
|
|
|
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
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return m
|
|
|
|
} else if err := Db().Where("camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else {
|
|
|
|
log.Errorf("camera: %s (first or create %s)", createErr, m.String())
|
2020-05-29 18:04:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return nil
|
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
|
|
|
}
|