2020-01-05 14:18:40 +01:00
package query
2019-12-11 07:37:39 +01:00
2020-04-21 10:23:27 +02:00
import (
2020-05-07 21:46:00 +02:00
"strings"
2020-04-21 10:23:27 +02:00
"github.com/photoprism/photoprism/internal/entity"
)
2019-12-11 07:37:39 +01:00
2020-05-07 21:55:34 +02:00
// ExistingFiles returns not-missing and not-deleted file entities in the range of limit and offset sorted by id.
2020-05-08 15:41:01 +02:00
func ExistingFiles ( limit int , offset int , filePath string ) ( files [ ] entity . File , err error ) {
2020-05-07 21:46:00 +02:00
if strings . HasPrefix ( filePath , "/" ) {
filePath = filePath [ 1 : ]
}
2020-05-08 15:41:01 +02:00
stmt := Db ( ) . Unscoped ( ) . Where ( "file_missing = 0 AND deleted_at IS NULL" )
2020-05-07 21:46:00 +02:00
if filePath != "" {
2020-05-07 21:55:34 +02:00
stmt = stmt . Where ( "file_name LIKE ?" , filePath + "/%" )
2020-05-07 21:46:00 +02:00
}
err = stmt . Order ( "id" ) . Limit ( limit ) . Offset ( offset ) . Find ( & files ) . Error
2019-12-11 07:37:39 +01:00
2020-05-07 19:42:04 +02:00
return files , err
2019-12-11 07:37:39 +01:00
}
2020-03-28 15:29:17 +01:00
// FilesByUUID
2020-05-08 15:41:01 +02:00
func FilesByUUID ( u [ ] string , limit int , offset int ) ( files [ ] entity . File , err error ) {
if err := Db ( ) . Where ( "(photo_uuid IN (?) AND file_primary = 1) OR file_uuid IN (?)" , u , u ) . Preload ( "Photo" ) . Limit ( limit ) . Offset ( offset ) . Find ( & files ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return files , err
}
return files , nil
}
2020-03-28 15:29:17 +01:00
// FileByPhotoUUID
2020-05-08 15:41:01 +02:00
func FileByPhotoUUID ( u string ) ( file entity . File , err error ) {
if err := Db ( ) . Where ( "photo_uuid = ? AND file_primary = 1" , u ) . Preload ( "Links" ) . Preload ( "Photo" ) . First ( & file ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return file , err
}
return file , nil
}
2020-04-08 13:24:06 +02:00
// FileByUUID returns the file entity for a given UUID.
2020-05-08 15:41:01 +02:00
func FileByUUID ( uuid string ) ( file entity . File , err error ) {
if err := Db ( ) . Where ( "file_uuid = ?" , uuid ) . Preload ( "Links" ) . Preload ( "Photo" ) . First ( & file ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return file , err
}
return file , nil
}
2020-02-04 12:25:57 +01:00
// FirstFileByHash finds a file with a given hash string.
2020-05-08 15:41:01 +02:00
func FileByHash ( fileHash string ) ( file entity . File , err error ) {
if err := Db ( ) . Where ( "file_hash = ?" , fileHash ) . Preload ( "Links" ) . Preload ( "Photo" ) . First ( & file ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return file , err
}
return file , nil
}
2020-04-21 10:23:27 +02:00
// SetPhotoPrimary sets a new primary image file for a photo.
2020-05-08 15:41:01 +02:00
func SetPhotoPrimary ( photoUUID , fileUUID string ) error {
Db ( ) . Model ( entity . File { } ) . Where ( "photo_uuid = ? AND file_uuid <> ?" , photoUUID , fileUUID ) . UpdateColumn ( "file_primary" , false )
return Db ( ) . Model ( entity . File { } ) . Where ( "photo_uuid = ? AND file_uuid = ?" , photoUUID , fileUUID ) . UpdateColumn ( "file_primary" , true ) . Error
2020-04-21 10:23:27 +02:00
}