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"
|
2018-08-15 09:59:51 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
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-01-06 02:14:17 +01:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CameraSlug string `gorm:"type:varbinary(128);unique_index;"`
|
2020-02-07 17:02:53 +01:00
|
|
|
CameraModel string `gorm:"type:varchar(128);"`
|
|
|
|
CameraMake string `gorm:"type:varchar(128);"`
|
|
|
|
CameraType string `gorm:"type:varchar(128);"`
|
2018-09-19 09:20:57 +02:00
|
|
|
CameraDescription string `gorm:"type:text;"`
|
|
|
|
CameraNotes string `gorm:"type:text;"`
|
2019-12-27 05:18:52 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time `sql:"index"`
|
2018-08-15 09:59:51 +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 {
|
2019-05-16 08:41:16 +02:00
|
|
|
makeName = strings.TrimSpace(makeName)
|
|
|
|
|
2018-08-15 09:59:51 +02:00
|
|
|
if modelName == "" {
|
|
|
|
modelName = "Unknown"
|
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 != "" {
|
2019-05-16 08:41:16 +02:00
|
|
|
cameraSlug = slug.Make(makeName + " " + modelName)
|
2019-05-03 18:57:28 +02:00
|
|
|
} else {
|
2019-05-16 08:41:16 +02:00
|
|
|
cameraSlug = slug.Make(modelName)
|
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-02-21 01:14:45 +01:00
|
|
|
// FirstOrCreate checks wether the camera model exist already in the database
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Camera) FirstOrCreate(db *gorm.DB) *Camera {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2019-12-19 09:37:10 +01:00
|
|
|
if err := db.FirstOrCreate(m, "camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).Error; err != nil {
|
|
|
|
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 ""
|
|
|
|
}
|