2020-01-05 13:18:40 +00:00
package query
2019-12-11 06:37:39 +00:00
import (
2020-05-26 17:27:29 +00:00
"time"
2020-01-29 15:49:42 +00:00
"github.com/jinzhu/gorm"
2019-12-11 15:55:18 +00:00
"github.com/photoprism/photoprism/internal/entity"
2019-12-11 06:37:39 +00:00
)
2020-03-28 14:29:17 +00:00
// PhotoByID returns a Photo based on the ID.
2020-05-08 13:41:01 +00:00
func PhotoByID ( photoID uint64 ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "id = ?" , photoID ) .
2020-05-26 17:27:29 +00:00
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
} ) .
Preload ( "Labels.Label" ) .
2020-05-18 20:18:58 +00:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-05-27 11:40:21 +00:00
Preload ( "Place" ) .
2020-07-12 06:27:05 +00:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-04-16 18:57:00 +00:00
First ( & photo ) . Error ; err != nil {
2019-12-11 06:37:39 +00:00
return photo , err
}
return photo , nil
}
2020-05-23 18:58:58 +00:00
// PhotoByUID returns a Photo based on the UID.
func PhotoByUID ( photoUID string ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "photo_uid = ?" , photoUID ) .
2020-05-26 17:27:29 +00:00
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
} ) .
Preload ( "Labels.Label" ) .
2020-05-18 20:18:58 +00:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-05-27 11:40:21 +00:00
Preload ( "Place" ) .
2020-07-12 06:27:05 +00:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-04-16 18:57:00 +00:00
First ( & photo ) . Error ; err != nil {
2019-12-11 06:37:39 +00:00
return photo , err
}
return photo , nil
}
2019-12-11 18:11:44 +00:00
2020-05-26 17:27:29 +00:00
// PhotoPreloadByUID returns a Photo based on the UID with all dependencies preloaded.
func PhotoPreloadByUID ( photoUID string ) ( photo entity . Photo , err error ) {
2020-05-23 18:58:58 +00:00
if err := UnscopedDb ( ) . Where ( "photo_uid = ?" , photoUID ) .
2020-01-29 15:49:42 +00:00
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
2020-04-18 23:13:55 +00:00
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
2020-01-29 15:49:42 +00:00
} ) .
2020-01-29 14:28:20 +00:00
Preload ( "Labels.Label" ) .
Preload ( "Camera" ) .
Preload ( "Lens" ) .
2020-05-26 17:27:29 +00:00
Preload ( "Details" ) .
2020-05-27 11:40:21 +00:00
Preload ( "Place" ) .
2020-07-12 06:27:05 +00:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-01-29 14:28:20 +00:00
First ( & photo ) . Error ; err != nil {
2019-12-11 18:11:44 +00:00
return photo , err
}
2020-04-30 18:07:03 +00:00
photo . PreloadMany ( )
2019-12-11 18:11:44 +00:00
return photo , nil
}
2020-05-07 17:42:04 +00:00
2020-05-26 17:27:29 +00:00
// PhotosMissing returns photo entities without existing files.
2020-06-01 07:45:24 +00:00
func PhotosMissing ( limit int , offset int ) ( entities entity . Photos , err error ) {
2020-05-08 13:41:01 +00:00
err = Db ( ) .
2020-05-07 18:33:11 +00:00
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" ) .
2020-05-21 08:03:56 +00:00
Where ( "photos.photo_type <> ?" , entity . TypeText ) .
2020-05-07 18:33:11 +00:00
Group ( "photos.id" ) .
Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
2020-05-07 17:42:04 +00:00
return entities , err
}
2020-05-08 10:01:22 +00:00
2020-05-26 17:27:29 +00:00
// ResetPhotoQuality resets the quality of photos without primary file to -1.
func ResetPhotoQuality ( ) error {
2020-05-08 13:41:01 +00:00
return Db ( ) . Table ( "photos" ) .
2020-05-08 10:01:22 +00:00
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
}
2020-05-26 17:27:29 +00:00
2020-06-24 06:34:23 +00:00
// PhotosCheck returns photos selected for maintenance.
func PhotosCheck ( limit int , offset int ) ( entities entity . Photos , err error ) {
2020-05-26 17:27:29 +00:00
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 ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-05-27 11:40:21 +00:00
Preload ( "Place" ) .
2020-07-12 06:27:05 +00:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-06-24 06:34:23 +00:00
Where ( "checked_at IS NULL OR checked_at < ?" , time . Now ( ) . Add ( - 1 * time . Hour * 24 * 3 ) ) .
2020-05-28 13:12:18 +00:00
Where ( "updated_at < ?" , time . Now ( ) . Add ( - 1 * time . Minute * 10 ) ) .
2020-05-26 17:27:29 +00:00
Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
return entities , err
}