2020-01-05 14:18:40 +01:00
package query
2019-12-11 07:37:39 +01:00
import (
2023-10-15 11:02:15 +02:00
"fmt"
2022-11-22 22:14:34 +01:00
"time"
2021-10-05 18:42:39 +02:00
"github.com/dustin/go-humanize/english"
2020-01-29 16:49:42 +01:00
"github.com/jinzhu/gorm"
2021-10-06 11:50:48 +02:00
2019-12-11 16:55:18 +01:00
"github.com/photoprism/photoprism/internal/entity"
2021-10-06 11:50:48 +02:00
"github.com/photoprism/photoprism/internal/mutex"
2019-12-11 07:37:39 +01:00
)
2020-03-28 15:29:17 +01:00
// PhotoByID returns a Photo based on the ID.
2020-05-08 15:41:01 +02:00
func PhotoByID ( photoID uint64 ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "id = ?" , photoID ) .
2020-05-26 19:27:29 +02: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 22:18:58 +02:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-05-27 13:40:21 +02:00
Preload ( "Place" ) .
2020-07-12 08:27:05 +02:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-04-16 20:57:00 +02:00
First ( & photo ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return photo , err
}
return photo , nil
}
2020-05-23 20:58:58 +02: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 19:27:29 +02: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 22:18:58 +02:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-05-27 13:40:21 +02:00
Preload ( "Place" ) .
2020-07-12 08:27:05 +02:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-04-16 20:57:00 +02:00
First ( & photo ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return photo , err
}
return photo , nil
}
2019-12-11 19:11:44 +01:00
2020-05-26 19:27:29 +02: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 20:58:58 +02:00
if err := UnscopedDb ( ) . Where ( "photo_uid = ?" , photoUID ) .
2020-01-29 16:49:42 +01:00
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
2020-04-19 01:13:55 +02:00
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
2020-01-29 16:49:42 +01:00
} ) .
2020-01-29 15:28:20 +01:00
Preload ( "Labels.Label" ) .
Preload ( "Camera" ) .
Preload ( "Lens" ) .
2020-05-26 19:27:29 +02:00
Preload ( "Details" ) .
2020-05-27 13:40:21 +02:00
Preload ( "Place" ) .
2020-07-12 08:27:05 +02:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2020-01-29 15:28:20 +01:00
First ( & photo ) . Error ; err != nil {
2019-12-11 19:11:44 +01:00
return photo , err
}
2020-04-30 20:07:03 +02:00
photo . PreloadMany ( )
2019-12-11 19:11:44 +01:00
return photo , nil
}
2020-05-07 19:42:04 +02:00
2023-07-23 17:57:48 +02:00
// MissingPhotos returns photo entities without existing files.
func MissingPhotos ( limit int , offset int ) ( entities entity . Photos , err error ) {
2020-05-08 15:41:01 +02:00
err = Db ( ) .
2020-05-07 20:33:11 +02:00
Select ( "photos.*" ) .
2021-02-08 14:09:58 +01:00
Where ( "id NOT IN (SELECT photo_id FROM files WHERE file_missing = 0 AND file_root = '/' AND deleted_at IS NULL)" ) .
2023-07-23 17:57:48 +02:00
Order ( "photos.id" ) .
Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
return entities , err
}
// ArchivedPhotos finds and returns archived photos.
func ArchivedPhotos ( limit int , offset int ) ( entities entity . Photos , err error ) {
err = UnscopedDb ( ) .
Select ( "photos.*" ) .
Where ( "photos.photo_quality > -1" ) .
Where ( "photos.deleted_at IS NOT NULL" ) .
Order ( "photos.id" ) .
2020-05-07 20:33:11 +02:00
Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
2020-05-07 19:42:04 +02:00
return entities , err
}
2020-05-08 12:01:22 +02:00
2021-11-18 02:23:25 +01:00
// PhotosMetadataUpdate returns photos selected for metadata maintenance.
2021-11-20 19:14:00 +01:00
func PhotosMetadataUpdate ( limit , offset int , delay , interval time . Duration ) ( entities entity . Photos , err error ) {
2020-05-26 19:27:29 +02: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 13:40:21 +02:00
Preload ( "Place" ) .
2020-07-12 08:27:05 +02:00
Preload ( "Cell" ) .
Preload ( "Cell.Place" ) .
2021-11-20 19:14:00 +01:00
Where ( "checked_at IS NULL OR checked_at < ?" , time . Now ( ) . Add ( - 1 * interval ) ) .
2020-12-11 22:09:11 +01:00
Where ( "updated_at < ? OR (cell_id = 'zz' AND photo_lat <> 0)" , time . Now ( ) . Add ( - 1 * delay ) ) .
2020-12-09 21:44:04 +01:00
Order ( "photos.ID ASC" ) . Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
2020-05-26 19:27:29 +02:00
return entities , err
2020-12-09 21:49:41 +01:00
}
2021-01-24 17:46:18 +01:00
2021-02-06 16:30:30 +01:00
// OrphanPhotos finds orphan index entries that may be removed.
func OrphanPhotos ( ) ( photos entity . Photos , err error ) {
2021-01-24 17:46:18 +01:00
err = UnscopedDb ( ) .
Raw ( ` SELECT * FROM photos WHERE
deleted_at IS NOT NULL
AND photo_quality = - 1
AND id NOT IN ( SELECT photo_id FROM files WHERE files . deleted_at IS NULL ) ` ) .
Find ( & photos ) . Error
return photos , err
}
2021-01-24 20:40:40 +01:00
// FixPrimaries tries to set a primary file for photos that have none.
func FixPrimaries ( ) error {
2021-12-09 02:33:41 +01:00
mutex . Index . Lock ( )
defer mutex . Index . Unlock ( )
2021-10-06 11:50:48 +02:00
2021-10-05 18:42:39 +02:00
start := time . Now ( )
2021-10-02 14:24:44 +02:00
2021-01-24 20:40:40 +01:00
var photos entity . Photos
2021-10-06 02:59:27 +02:00
// Remove primary file flag from broken or missing files.
if err := UnscopedDb ( ) . Table ( entity . File { } . TableName ( ) ) .
2022-11-21 12:20:28 +01:00
Where ( "(file_error <> '' OR file_missing = 1) AND file_primary <> 0" ) .
2022-04-04 08:54:03 +02:00
UpdateColumn ( "file_primary" , 0 ) . Error ; err != nil {
2021-10-06 02:59:27 +02:00
return err
}
// Find photos without primary file.
2021-01-24 20:40:40 +01:00
if err := UnscopedDb ( ) .
2021-10-02 14:24:44 +02:00
Raw ( ` SELECT * FROM photos
WHERE deleted_at IS NULL
2021-02-08 07:39:29 +01:00
AND id NOT IN ( SELECT photo_id FROM files WHERE file_primary = 1 ) ` ) .
2021-01-24 20:40:40 +01:00
Find ( & photos ) . Error ; err != nil {
return err
}
2021-10-05 18:42:39 +02:00
if len ( photos ) == 0 {
log . Debugf ( "index: found no photos without primary file [%s]" , time . Since ( start ) )
return nil
}
2021-10-06 02:59:27 +02:00
// Try to find matching primary files.
2021-01-24 20:40:40 +01:00
for _ , p := range photos {
2021-10-05 18:42:39 +02:00
log . Debugf ( "index: searching primary file for %s" , p . PhotoUID )
2021-01-24 20:40:40 +01:00
if err := p . SetPrimary ( "" ) ; err != nil {
2022-04-16 13:50:35 +02:00
log . Infof ( "index: %s" , err )
2021-01-24 20:40:40 +01:00
}
}
2021-12-09 07:41:07 +01:00
log . Debugf ( "index: updated primary files [%s]" , time . Since ( start ) )
2021-10-05 18:42:39 +02:00
2021-01-24 20:40:40 +01:00
return nil
}
2021-10-06 11:50:48 +02:00
// FlagHiddenPhotos sets the quality score of photos without valid primary file to -1.
2022-11-21 12:20:28 +01:00
func FlagHiddenPhotos ( ) ( err error ) {
2021-12-09 02:33:41 +01:00
mutex . Index . Lock ( )
defer mutex . Index . Unlock ( )
2021-10-06 11:50:48 +02:00
2022-11-21 12:20:28 +01:00
// Start time for logs.
2021-10-06 11:50:48 +02:00
start := time . Now ( )
2022-11-21 12:20:28 +01:00
// IDs of hidden photos.
var hidden [ ] uint
2021-10-06 11:50:48 +02:00
2023-10-15 11:02:15 +02:00
// Number of updated records.
affected := 0
2023-09-19 23:24:50 +02:00
2022-11-21 12:20:28 +01:00
// Find and flag hidden photos.
if err = Db ( ) . Table ( entity . Photo { } . TableName ( ) ) .
Where ( "id NOT IN (SELECT photo_id FROM files WHERE file_primary = 1 AND file_missing = 0 AND file_error = '' AND deleted_at IS NULL) AND photo_quality > -1" ) .
Pluck ( "id" , & hidden ) . Error ; err != nil {
2023-09-19 10:32:09 +02:00
// Find query failed.
2022-11-21 12:20:28 +01:00
return err
2023-09-19 23:24:50 +02:00
} else if found := len ( hidden ) ; found == 0 {
2023-09-19 10:32:09 +02:00
// Nothing to update.
2021-12-09 07:41:07 +01:00
return nil
2022-11-21 12:20:28 +01:00
} else {
2023-09-19 23:24:50 +02:00
// Update photos in batches to be compatible with SQLite.
2023-10-15 09:46:04 +02:00
batchSize := BatchSize ( )
2023-09-19 23:24:50 +02:00
2023-10-15 09:31:10 +02:00
for i := 0 ; i < len ( hidden ) ; i += batchSize {
j := i + batchSize
2023-09-19 23:24:50 +02:00
if j > len ( hidden ) {
j = len ( hidden )
}
// Next batch.
ids := hidden [ i : j ]
// Set photos.photo_quality = -1.
2023-10-15 11:02:15 +02:00
if result := UnscopedDb ( ) . Table ( entity . Photo { } . TableName ( ) ) .
2023-10-15 09:46:04 +02:00
Where ( "id IN (?) AND photo_quality > -1" , ids ) .
2023-10-15 11:02:15 +02:00
UpdateColumn ( "photo_quality" , - 1 ) ; result . Error != nil {
// Failed to flag all hidden photos.
log . Warnf ( "index: failed to flag %d photos as hidden" , len ( hidden ) - affected )
return fmt . Errorf ( "%s while flagging hidden photos" , result . Error )
} else if result . RowsAffected > 0 {
affected += int ( result . RowsAffected )
2023-09-19 23:24:50 +02:00
} else {
2023-10-15 11:02:15 +02:00
affected += len ( ids )
2023-09-19 23:24:50 +02:00
}
}
}
2023-10-15 11:02:15 +02:00
// Log number of affected rows, if any.
if affected > 0 {
log . Infof ( "index: flagged %s as hidden [%s]" , english . Plural ( affected , "photo" , "photos" ) , time . Since ( start ) )
2021-10-06 11:50:48 +02:00
}
2022-11-21 12:20:28 +01:00
return nil
2021-10-06 11:50:48 +02:00
}