photoprism/internal/query/photo.go
Theresa Gresch ad9167360d
Feature/246 (#345)
* Import: Implement "add to album" in backend #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Import: Implement "add to album" in frontend #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Add OriginalName to photo search result

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Add json tags to PhotoName and PhotoPath

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Photo: Use EstimateCountry() in UpdateLocation()

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Photo: Set OriginalName earlier while indexing

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Ignore whitespace when stripping sequence from filename #335

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Fix labels count for SQLite

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Import: Show name of new albums #246

Signed-off-by: Michael Mayer <michael@liquidbytes.net>

* Frontend: Add acceptance test files

Co-authored-by: Michael Mayer <michael@liquidbytes.net>
2020-06-01 09:45:24 +02:00

115 lines
3.4 KiB
Go

package query
import (
"time"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/entity"
)
// PhotoByID returns a Photo based on the ID.
func PhotoByID(photoID uint64) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("id = ?", photoID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Links").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Location").
Preload("Location.Place").
First(&photo).Error; err != nil {
return photo, err
}
return photo, nil
}
// PhotoByUID returns a Photo based on the UID.
func PhotoByUID(photoUID string) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("photo_uid = ?", photoUID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Links").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Location").
Preload("Location.Place").
First(&photo).Error; err != nil {
return photo, err
}
return photo, nil
}
// PhotoPreloadByUID returns a Photo based on the UID with all dependencies preloaded.
func PhotoPreloadByUID(photoUID string) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("photo_uid = ?", photoUID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Links").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Location").
Preload("Location.Place").
First(&photo).Error; err != nil {
return photo, err
}
photo.PreloadMany()
return photo, nil
}
// PhotosMissing returns photo entities without existing files.
func PhotosMissing(limit int, offset int) (entities entity.Photos, err error) {
err = Db().
Select("photos.*").
Joins("JOIN files a ON photos.id = a.photo_id ").
Joins("LEFT JOIN files b ON a.photo_id = b.photo_id AND a.id != b.id AND b.file_missing = 0").
Where("a.file_missing = 1 AND b.id IS NULL").
Where("photos.photo_type <> ?", entity.TypeText).
Group("photos.id").
Limit(limit).Offset(offset).Find(&entities).Error
return entities, err
}
// ResetPhotoQuality resets the quality of photos without primary file to -1.
func ResetPhotoQuality() error {
return Db().Table("photos").
Where("id IN (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id)").
Update("photo_quality", -1).Error
}
// PhotosMaintenance returns photos selected for maintenance.
func PhotosMaintenance(limit int, offset int) (entities entity.Photos, err error) {
err = Db().
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Links").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Location").
Preload("Location.Place").
Where("maintained_at IS NULL OR maintained_at < ?", time.Now().Add(-1*time.Hour*24*3)).
Where("updated_at < ?", time.Now().Add(-1*time.Minute*10)).
Limit(limit).Offset(offset).Find(&entities).Error
return entities, err
}