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-18 22:18:58 +02:00
|
|
|
ID uint `gorm:"primary_key" yaml:"CameraID"`
|
|
|
|
CameraSlug string `gorm:"type:varbinary(255);unique_index;" yaml:"Slug"`
|
|
|
|
CameraModel string `gorm:"type:varchar(255);" yaml:"Model"`
|
|
|
|
CameraMake string `gorm:"type:varchar(255);" yaml:"Make"`
|
|
|
|
CameraType string `gorm:"type:varchar(255);" yaml:"Type,omitempty"`
|
|
|
|
CameraDescription string `gorm:"type:text;" yaml:"Description,omitempty"`
|
|
|
|
CameraNotes string `gorm:"type:text;" yaml:"Notes,omitempty"`
|
|
|
|
CreatedAt time.Time `yaml:"-"`
|
|
|
|
UpdatedAt time.Time `yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" 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() {
|
|
|
|
UnknownCamera.FirstOrCreate()
|
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-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 ""
|
|
|
|
}
|