2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-09-24 19:07:43 +02:00
|
|
|
|
|
|
|
import (
|
2020-01-06 04:24:49 +01:00
|
|
|
"strings"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2018-09-24 19:07:43 +02:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Lens represents camera lens (as extracted from UpdateExif metadata)
|
2018-09-24 19:07:43 +02:00
|
|
|
type Lens struct {
|
2020-01-06 02:14:17 +01:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
LensSlug string `gorm:"type:varbinary(128);unique_index;"`
|
2018-09-24 19:07:43 +02:00
|
|
|
LensModel string
|
|
|
|
LensMake string
|
|
|
|
LensType string
|
|
|
|
LensOwner string
|
|
|
|
LensDescription string `gorm:"type:text;"`
|
|
|
|
LensNotes string `gorm:"type:text;"`
|
2019-12-27 05:18:52 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time `sql:"index"`
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
var UnknownLens = Lens{
|
|
|
|
LensModel: "Unknown",
|
|
|
|
LensMake: "",
|
|
|
|
LensSlug: "zz",
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnknownLens initializes the database with an unknown lens if not exists
|
|
|
|
func CreateUnknownLens(db *gorm.DB) {
|
|
|
|
UnknownLens.FirstOrCreate(db)
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns Lens table identifier "lens"
|
2018-09-24 19:07:43 +02:00
|
|
|
func (Lens) TableName() string {
|
|
|
|
return "lenses"
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewLens creates a new lens in database
|
2018-09-24 19:07:43 +02:00
|
|
|
func NewLens(modelName string, makeName string) *Lens {
|
2020-01-06 04:24:49 +01:00
|
|
|
modelName = strings.TrimSpace(modelName)
|
|
|
|
makeName = strings.TrimSpace(makeName)
|
2020-04-25 14:22:47 +02:00
|
|
|
lensSlug := slug.MakeLang(modelName, "en")
|
2020-01-06 04:24:49 +01:00
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
if modelName == "" {
|
2020-04-25 14:22:47 +02:00
|
|
|
return &UnknownLens
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &Lens{
|
|
|
|
LensModel: modelName,
|
|
|
|
LensMake: makeName,
|
|
|
|
LensSlug: lensSlug,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
// FirstOrCreate checks if the lens already exists in the database
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Lens) FirstOrCreate(db *gorm.DB) *Lens {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2020-01-06 04:24:49 +01:00
|
|
|
if err := db.FirstOrCreate(m, "lens_slug = ?", m.LensSlug).Error; err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("lens: %s", err)
|
|
|
|
}
|
2018-09-24 19:07:43 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
return m
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|