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"
|
2020-12-13 15:43:01 +01:00
|
|
|
"sync"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
"github.com/gosimple/slug"
|
2020-05-29 18:04:30 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-09-24 19:07:43 +02:00
|
|
|
)
|
|
|
|
|
2020-12-13 15:43:01 +01:00
|
|
|
var lensMutex = sync.Mutex{}
|
|
|
|
|
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-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"ID"`
|
2020-09-13 17:51:43 +02:00
|
|
|
LensSlug string `gorm:"type:VARBINARY(255);unique_index;" json:"Slug" yaml:"Slug,omitempty"`
|
|
|
|
LensName string `gorm:"type:VARCHAR(255);" json:"Name" yaml:"Name"`
|
2020-05-29 18:04:30 +02:00
|
|
|
LensMake string `json:"Make" yaml:"Make,omitempty"`
|
|
|
|
LensModel string `json:"Model" yaml:"Model,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
LensType string `json:"Type" yaml:"Type,omitempty"`
|
2020-09-13 17:51:43 +02:00
|
|
|
LensDescription string `gorm:"type:TEXT;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
LensNotes string `gorm:"type:TEXT;" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CreatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"-" yaml:"-"`
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
var UnknownLens = Lens{
|
|
|
|
LensSlug: "zz",
|
2020-05-30 21:11:56 +02:00
|
|
|
LensName: "Unknown",
|
2020-05-29 18:04:30 +02:00
|
|
|
LensMake: "",
|
|
|
|
LensModel: "Unknown",
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnknownLens initializes the database with an unknown lens if not exists
|
2020-04-30 20:07:03 +02:00
|
|
|
func CreateUnknownLens() {
|
2020-05-24 22:16:06 +02:00
|
|
|
FirstOrCreateLens(&UnknownLens)
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
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-05-29 18:04:30 +02:00
|
|
|
modelName = txt.Clip(modelName, txt.ClipDefault)
|
|
|
|
makeName = txt.Clip(makeName, txt.ClipDefault)
|
2020-01-06 04:24:49 +01:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if modelName == "" && makeName == "" {
|
2020-04-25 14:22:47 +02:00
|
|
|
return &UnknownLens
|
2020-05-29 18:04:30 +02:00
|
|
|
} else if strings.HasPrefix(modelName, makeName) {
|
|
|
|
modelName = strings.TrimSpace(modelName[len(makeName):])
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := CameraMakes[makeName]; ok {
|
|
|
|
makeName = n
|
|
|
|
}
|
|
|
|
|
|
|
|
var name []string
|
|
|
|
|
|
|
|
if makeName != "" {
|
|
|
|
name = append(name, makeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if modelName != "" {
|
|
|
|
name = append(name, modelName)
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
lensName := strings.Join(name, " ")
|
|
|
|
lensSlug := slug.Make(txt.Clip(lensName, txt.ClipSlug))
|
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
result := &Lens{
|
|
|
|
LensSlug: lensSlug,
|
2020-05-29 18:04:30 +02:00
|
|
|
LensName: lensName,
|
|
|
|
LensMake: makeName,
|
|
|
|
LensModel: modelName,
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Lens) Create() error {
|
2020-12-13 15:43:01 +01:00
|
|
|
lensMutex.Lock()
|
|
|
|
defer lensMutex.Unlock()
|
|
|
|
|
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
|
|
|
// FirstOrCreateLens returns the existing row, inserts a new row or nil in case of errors.
|
2020-05-24 22:16:06 +02:00
|
|
|
func FirstOrCreateLens(m *Lens) *Lens {
|
2020-12-14 19:24:08 +01:00
|
|
|
if m.LensSlug == "" {
|
|
|
|
return &UnknownLens
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
result := Lens{}
|
|
|
|
|
2020-12-13 15:43:01 +01:00
|
|
|
if res := Db().Where("lens_slug = ?", m.LensSlug).First(&result); res.Error == nil {
|
2020-05-24 22:16:06 +02:00
|
|
|
return &result
|
2020-12-13 15:43:01 +01:00
|
|
|
} else if err := m.Create(); err == nil {
|
2020-07-07 20:44:33 +02:00
|
|
|
if !m.Unknown() {
|
|
|
|
event.EntitiesCreated("lenses", []*Lens{m})
|
2018-09-24 19:07:43 +02:00
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
event.Publish("count.lenses", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 18:04:30 +02:00
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return m
|
2020-12-13 15:43:01 +01:00
|
|
|
} else if res := Db().Where("lens_slug = ?", m.LensSlug).First(&result); res.Error == nil {
|
2020-07-07 20:44:33 +02:00
|
|
|
return &result
|
|
|
|
} else {
|
2020-12-13 15:43:01 +01:00
|
|
|
log.Errorf("lens: %s (create %s)", err.Error(), txt.Quote(m.String()))
|
2020-05-29 18:04:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:24:08 +01:00
|
|
|
return &UnknownLens
|
2020-07-07 20:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns an identifier that can be used in logs.
|
|
|
|
func (m *Lens) String() string {
|
|
|
|
return m.LensName
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
2020-05-29 18:04:30 +02:00
|
|
|
|
|
|
|
// Unknown returns true if the lens is not a known make or model.
|
|
|
|
func (m *Lens) Unknown() bool {
|
|
|
|
return m.LensSlug == "" || m.LensSlug == UnknownLens.LensSlug
|
|
|
|
}
|