2020-01-05 14:18:40 +01:00
package query
2019-12-11 07:37:39 +01:00
2019-12-11 16:55:18 +01:00
import "github.com/photoprism/photoprism/internal/entity"
2019-12-11 07:37:39 +01:00
2020-03-28 15:29:17 +01:00
// Files finds files returning maximum results defined by limit
2019-12-11 07:37:39 +01:00
// and finding them from an offest defined by offset.
2020-03-28 17:17:41 +01:00
func ( q * Query ) Files ( limit int , offset int ) ( files [ ] entity . File , err error ) {
if err := q . db . Where ( & entity . File { } ) . 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
// FilesByUUID
2020-03-28 17:17:41 +01:00
func ( q * Query ) FilesByUUID ( u [ ] string , limit int , offset int ) ( files [ ] entity . File , err error ) {
if err := q . 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-03-28 17:17:41 +01:00
func ( q * Query ) FileByPhotoUUID ( u string ) ( file entity . File , err error ) {
if err := q . db . Where ( "photo_uuid = ? AND file_primary = 1" , u ) . Preload ( "Photo" ) . First ( & file ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return file , err
}
return file , nil
}
2020-03-28 15:29:17 +01:00
// FileByID returns a MediaFile given a certain ID.
2020-03-28 17:17:41 +01:00
func ( q * Query ) FileByID ( id string ) ( file entity . File , err error ) {
if err := q . db . Where ( "id = ?" , id ) . 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-03-28 17:17:41 +01:00
func ( q * Query ) FileByHash ( fileHash string ) ( file entity . File , err error ) {
if err := q . db . Where ( "file_hash = ?" , fileHash ) . Preload ( "Photo" ) . First ( & file ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return file , err
}
return file , nil
}