photoprism/internal/entity/camera.go
Michael Mayer b37d4472e4 Backend: Use original file if thumb size exceeds limit #172
Plus some mutex and config refactoring along the way...

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-01-08 19:51:21 +01:00

73 lines
1.6 KiB
Go

package entity
import (
"fmt"
"strings"
"time"
"github.com/gosimple/slug"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/mutex"
)
// Camera model and make (as extracted from UpdateExif metadata)
type Camera struct {
ID uint `gorm:"primary_key"`
CameraSlug string `gorm:"type:varbinary(128);unique_index;"`
CameraModel string
CameraMake string
CameraType string
CameraOwner string
CameraDescription string `gorm:"type:text;"`
CameraNotes string `gorm:"type:text;"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
func NewCamera(modelName string, makeName string) *Camera {
makeName = strings.TrimSpace(makeName)
if modelName == "" {
modelName = "Unknown"
} else if strings.HasPrefix(modelName, makeName) {
modelName = strings.TrimSpace(modelName[len(makeName):])
}
var cameraSlug string
if makeName != "" {
cameraSlug = slug.Make(makeName + " " + modelName)
} else {
cameraSlug = slug.Make(modelName)
}
result := &Camera{
CameraModel: modelName,
CameraMake: makeName,
CameraSlug: cameraSlug,
}
return result
}
func (m *Camera) FirstOrCreate(db *gorm.DB) *Camera {
mutex.Db.Lock()
defer mutex.Db.Unlock()
if err := db.FirstOrCreate(m, "camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).Error; err != nil {
log.Errorf("camera: %s", err)
}
return m
}
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
}
return ""
}