2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-08-15 09:59:51 +02:00
|
|
|
|
|
|
|
import (
|
2019-05-04 17:34:51 +02:00
|
|
|
"fmt"
|
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-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:"-"`
|
|
|
|
CameraModel string `gorm:"type:varchar(255);" json:"Model" yaml:"Model"`
|
|
|
|
CameraMake string `gorm:"type:varchar(255);" json:"Make" yaml:"Make"`
|
|
|
|
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{
|
|
|
|
CameraModel: "Unknown",
|
|
|
|
CameraMake: "",
|
|
|
|
CameraSlug: "zz",
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-08-15 09:59:51 +02:00
|
|
|
if modelName == "" {
|
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
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
var cameraSlug string
|
|
|
|
|
|
|
|
if makeName != "" {
|
2020-04-26 14:31:33 +02:00
|
|
|
cameraSlug = slug.Make(txt.Clip(makeName+" "+modelName, txt.ClipSlug))
|
2019-05-03 18:57:28 +02:00
|
|
|
} else {
|
2020-04-26 14:31:33 +02:00
|
|
|
cameraSlug = slug.Make(txt.Clip(modelName, txt.ClipSlug))
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
2018-09-13 20:54:34 +02:00
|
|
|
|
2018-08-15 09:59:51 +02:00
|
|
|
result := &Camera{
|
2018-09-12 16:37:30 +02:00
|
|
|
CameraModel: modelName,
|
2018-09-24 19:07:43 +02:00
|
|
|
CameraMake: makeName,
|
2018-09-17 18:40:57 +02:00
|
|
|
CameraSlug: cameraSlug,
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
// FirstOrCreate checks if the camera model exist already in the database
|
|
|
|
func (m *Camera) FirstOrCreate() *Camera {
|
2020-05-01 13:54:47 +02:00
|
|
|
if err := Db().FirstOrCreate(m, "camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).Error; err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("camera: %s", err)
|
|
|
|
}
|
2018-08-15 09:59:51 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
return m
|
2018-09-13 07:05:13 +02:00
|
|
|
}
|
2019-05-04 17:34:51 +02:00
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Camera) Create() error {
|
|
|
|
if err := Db().Create(m).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateCamera inserts a new row if not exists.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if m.CameraMake != "" && m.CameraModel != "" {
|
|
|
|
return fmt.Sprintf("%s %s", m.CameraMake, m.CameraModel)
|
|
|
|
} else if m.CameraModel != "" {
|
|
|
|
return m.CameraModel
|
2019-05-04 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|