From 10859ee69512576ac32d517f7f4c59a7376a9b52 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 27 Sep 2018 08:59:53 +0200 Subject: [PATCH] Moved perceptual hash from photo to file model; code clean-up (go fmt) - Files might show different details of the full photo --- cmd/photoprism/photoprism.go | 2 +- internal/api/photos.go | 4 +-- internal/commands/export.go | 6 ++-- internal/commands/migrate.go | 6 ++-- internal/models/album.go | 4 +-- internal/models/file.go | 27 +++++++++------- internal/models/photo.go | 57 +++++++++++++++++----------------- internal/photoprism/exif.go | 4 +-- internal/photoprism/indexer.go | 19 +++++------- internal/photoprism/search.go | 56 +++++++++++++++++---------------- internal/server/server.go | 2 +- 11 files changed, 93 insertions(+), 94 deletions(-) diff --git a/cmd/photoprism/photoprism.go b/cmd/photoprism/photoprism.go index 757b117bd..b78614e0a 100644 --- a/cmd/photoprism/photoprism.go +++ b/cmd/photoprism/photoprism.go @@ -27,4 +27,4 @@ func main() { } app.Run(os.Args) -} \ No newline at end of file +} diff --git a/internal/api/photos.go b/internal/api/photos.go index 02c3e3eac..eb7345a7c 100644 --- a/internal/api/photos.go +++ b/internal/api/photos.go @@ -9,7 +9,7 @@ import ( "strconv" ) -func GetPhotos (router *gin.RouterGroup, conf *photoprism.Config) { +func GetPhotos(router *gin.RouterGroup, conf *photoprism.Config) { router.GET("/photos", func(c *gin.Context) { var form forms.PhotoSearchForm @@ -28,4 +28,4 @@ func GetPhotos (router *gin.RouterGroup, conf *photoprism.Config) { c.JSON(http.StatusOK, result) }) -} \ No newline at end of file +} diff --git a/internal/commands/export.go b/internal/commands/export.go index 1fd4ae3a6..3e06ee3e8 100644 --- a/internal/commands/export.go +++ b/internal/commands/export.go @@ -9,9 +9,9 @@ import ( ) var ExportCommand = cli.Command{ - Name: "export", - Usage: "Exports photos as JPEG", - Flags: exportFlags, + Name: "export", + Usage: "Exports photos as JPEG", + Flags: exportFlags, Action: exportAction, } diff --git a/internal/commands/migrate.go b/internal/commands/migrate.go index 65a4f96d1..3102c19d2 100644 --- a/internal/commands/migrate.go +++ b/internal/commands/migrate.go @@ -7,12 +7,12 @@ import ( ) var MigrateCommand = cli.Command{ - Name: "migrate", - Usage: "Automatically migrates / initializes database", + Name: "migrate", + Usage: "Automatically migrates / initializes database", Action: migrateAction, } -func migrateAction (context *cli.Context) error { +func migrateAction(context *cli.Context) error { conf := photoprism.NewConfig(context) fmt.Println("Migrating database...") diff --git a/internal/models/album.go b/internal/models/album.go index f182c7eef..f5a676615 100644 --- a/internal/models/album.go +++ b/internal/models/album.go @@ -8,8 +8,8 @@ type Album struct { gorm.Model AlbumSlug string AlbumName string - AlbumDescription string `gorm:"type:text;"` - AlbumNotes string `gorm:"type:text;"` + AlbumDescription string `gorm:"type:text;"` + AlbumNotes string `gorm:"type:text;"` AlbumPhoto *Photo AlbumPhotoID uint Photos []Photo `gorm:"many2many:album_photos;"` diff --git a/internal/models/file.go b/internal/models/file.go index fa2265c7f..7ce0541d4 100644 --- a/internal/models/file.go +++ b/internal/models/file.go @@ -6,16 +6,19 @@ import ( type File struct { gorm.Model - Photo *Photo - PhotoID uint - FilePrimary bool - FileName string - FileType string `gorm:"type:varchar(30)"` - FileMime string `gorm:"type:varchar(50)"` - FileWidth int - FileHeight int - FileOrientation int - FileAspectRatio float64 - FileHash string `gorm:"type:varchar(100);unique_index"` - FileNotes string `gorm:"type:text;"` + Photo *Photo + PhotoID uint + FilePrimary bool + FileMissing bool + FileName string + FileType string `gorm:"type:varchar(30)"` + FileMime string `gorm:"type:varchar(50)"` + FileWidth int + FileHeight int + FileOrientation int + FileAspectRatio float64 + FileHash string `gorm:"type:varchar(100);unique_index"` + FilePerceptualHash string + + FileNotes string `gorm:"type:text;"` } diff --git a/internal/models/photo.go b/internal/models/photo.go index 971f9381b..50dfa86a9 100644 --- a/internal/models/photo.go +++ b/internal/models/photo.go @@ -7,33 +7,32 @@ import ( type Photo struct { gorm.Model - TakenAt time.Time - PhotoTitle string - PhotoTitleChanged bool - PhotoDescription string `gorm:"type:text;"` - PhotoNotes string `gorm:"type:text;"` - PhotoArtist string - PhotoColors string - PhotoVibrantColor string - PhotoMutedColor string - PhotoCanonicalName string - PhotoPerceptualHash string - PhotoFavorite bool - PhotoLat float64 - PhotoLong float64 - PhotoFocalLength float64 - PhotoAperture float64 - Camera *Camera - CameraID uint - Lens *Lens - LensID uint - Country *Country - CountryID string - CountryChanged bool - Location *Location - LocationID uint - LocationChanged bool - Tags []*Tag `gorm:"many2many:photo_tags;"` - Files []*File - Albums []*Album `gorm:"many2many:album_photos;"` + TakenAt time.Time + PhotoTitle string + PhotoTitleChanged bool + PhotoDescription string `gorm:"type:text;"` + PhotoNotes string `gorm:"type:text;"` + PhotoArtist string + PhotoColors string + PhotoVibrantColor string + PhotoMutedColor string + PhotoCanonicalName string + PhotoFavorite bool + PhotoLat float64 + PhotoLong float64 + PhotoFocalLength float64 + PhotoAperture float64 + Camera *Camera + CameraID uint + Lens *Lens + LensID uint + Country *Country + CountryID string + CountryChanged bool + Location *Location + LocationID uint + LocationChanged bool + Tags []*Tag `gorm:"many2many:photo_tags;"` + Files []*File + Albums []*Album `gorm:"many2many:album_photos;"` } diff --git a/internal/photoprism/exif.go b/internal/photoprism/exif.go index 56528b784..bf5fc350f 100644 --- a/internal/photoprism/exif.go +++ b/internal/photoprism/exif.go @@ -87,7 +87,7 @@ func (m *MediaFile) GetExifData() (*ExifData, error) { value := float64(number) / float64(denom) - m.exifData.Aperture = math.Round(value * 1000) / 1000 + m.exifData.Aperture = math.Round(value*1000) / 1000 } if focal, err := x.Get(exif.FocalLength); err == nil { @@ -99,7 +99,7 @@ func (m *MediaFile) GetExifData() (*ExifData, error) { value := float64(number) / float64(denom) - m.exifData.FocalLength = math.Round(value * 1000) / 1000 + m.exifData.FocalLength = math.Round(value*1000) / 1000 } if tm, err := x.DateTime(); err == nil { diff --git a/internal/photoprism/indexer.go b/internal/photoprism/indexer.go index adc1572e7..4b1bda7df 100644 --- a/internal/photoprism/indexer.go +++ b/internal/photoprism/indexer.go @@ -81,11 +81,6 @@ func (i *Indexer) IndexMediaFile(mediaFile *MediaFile) string { if photoQuery.Error != nil { if jpeg, err := mediaFile.GetJpeg(); err == nil { - // Perceptual Hash - if perceptualHash, err := jpeg.GetPerceptualHash(); err == nil { - photo.PhotoPerceptualHash = perceptualHash - } - // Geo Location if exifData, err := jpeg.GetExifData(); err == nil { photo.PhotoLat = exifData.Lat @@ -159,13 +154,6 @@ func (i *Indexer) IndexMediaFile(mediaFile *MediaFile) string { i.db.Create(&photo) } else if time.Now().Sub(photo.UpdatedAt).Minutes() > 10 { // If updated more than 10 minutes ago if jpeg, err := mediaFile.GetJpeg(); err == nil { - // Perceptual Hash - if photo.PhotoPerceptualHash == "" { - if perceptualHash, err := jpeg.GetPerceptualHash(); err == nil { - photo.PhotoPerceptualHash = perceptualHash - } - } - // PhotoColors colorNames, photo.PhotoVibrantColor, photo.PhotoMutedColor = jpeg.GetColors() @@ -206,6 +194,13 @@ func (i *Indexer) IndexMediaFile(mediaFile *MediaFile) string { file.FileMime = mediaFile.GetMimeType() file.FileOrientation = mediaFile.GetOrientation() + // Perceptual Hash + if file.FilePerceptualHash == "" && mediaFile.IsJpeg() { + if perceptualHash, err := mediaFile.GetPerceptualHash(); err == nil { + file.FilePerceptualHash = perceptualHash + } + } + if mediaFile.GetWidth() > 0 && mediaFile.GetHeight() > 0 { file.FileWidth = mediaFile.GetWidth() file.FileHeight = mediaFile.GetHeight() diff --git a/internal/photoprism/search.go b/internal/photoprism/search.go index 07f66394f..9710f9ec5 100644 --- a/internal/photoprism/search.go +++ b/internal/photoprism/search.go @@ -19,23 +19,22 @@ type SearchCount struct { type PhotoSearchResult struct { // Photo - ID uint - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - TakenAt time.Time - PhotoTitle string - PhotoDescription string - PhotoArtist string - PhotoKeywords string - PhotoColors string - PhotoVibrantColor string - PhotoMutedColor string - PhotoCanonicalName string - PhotoPerceptualHash string - PhotoLat float64 - PhotoLong float64 - PhotoFavorite bool + ID uint + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + TakenAt time.Time + PhotoTitle string + PhotoDescription string + PhotoArtist string + PhotoKeywords string + PhotoColors string + PhotoVibrantColor string + PhotoMutedColor string + PhotoCanonicalName string + PhotoLat float64 + PhotoLong float64 + PhotoFavorite bool // Camera CameraID uint @@ -65,15 +64,18 @@ type PhotoSearchResult struct { LocType string // File - FileID uint - FileName string - FileHash string - FileType string - FileMime string - FileWidth int - FileHeight int - FileOrientation int - FileAspectRatio float64 + FileID uint + FilePrimary bool + FileMissing bool + FileName string + FileHash string + FilePerceptualHash string + FileType string + FileMime string + FileWidth int + FileHeight int + FileOrientation int + FileAspectRatio float64 // Tags Tags string @@ -92,7 +94,7 @@ func (s *Search) Photos(form PhotoSearchForm) ([]PhotoSearchResult, error) { q := s.db.NewScope(nil).DB() q = q.Table("photos"). Select(`SQL_CALC_FOUND_ROWS photos.*, - files.id AS file_id, files.file_name, files.file_hash, files.file_type, files.file_mime, files.file_width, files.file_height, files.file_aspect_ratio, files.file_orientation, + files.id AS file_id, files.file_primary, files.file_missing, files.file_name, files.file_hash, files.file_perceptual_hash, files.file_type, files.file_mime, files.file_width, files.file_height, files.file_aspect_ratio, files.file_orientation, cameras.camera_make, cameras.camera_model, lenses.lens_make, lenses.lens_model, countries.country_name, diff --git a/internal/server/server.go b/internal/server/server.go index 5bdcdb4ab..f416a7afb 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -21,4 +21,4 @@ func Start(conf *photoprism.Config) { registerRoutes(app, conf) app.Run(fmt.Sprintf("%s:%d", conf.ServerIP, conf.ServerPort)) -} \ No newline at end of file +}