2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2020-10-12 23:12:46 +00:00
|
|
|
"database/sql"
|
2023-06-15 02:46:09 +00:00
|
|
|
"errors"
|
2020-10-12 23:12:46 +00:00
|
|
|
"fmt"
|
2022-09-01 07:54:34 +00:00
|
|
|
"path/filepath"
|
2024-10-29 00:26:23 +00:00
|
|
|
"slices"
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2023-11-01 21:58:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil"
|
2022-07-13 06:30:54 +00:00
|
|
|
"gopkg.in/guregu/null.v4"
|
|
|
|
"gopkg.in/guregu/null.v4/zero"
|
|
|
|
|
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
|
|
"github.com/doug-martin/goqu/v9/exp"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
2024-02-22 00:28:18 +00:00
|
|
|
const imageTable = "images"
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
const (
|
|
|
|
imageIDColumn = "image_id"
|
|
|
|
performersImagesTable = "performers_images"
|
|
|
|
imagesTagsTable = "images_tags"
|
|
|
|
imagesFilesTable = "images_files"
|
2023-09-12 03:31:53 +00:00
|
|
|
imagesURLsTable = "image_urls"
|
|
|
|
imageURLColumn = "url"
|
2022-07-13 06:30:54 +00:00
|
|
|
)
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
type imageRow struct {
|
2022-11-15 22:31:44 +00:00
|
|
|
ID int `db:"id" goqu:"skipinsert"`
|
|
|
|
Title zero.String `db:"title"`
|
2023-11-28 04:45:07 +00:00
|
|
|
Code zero.String `db:"code"`
|
2022-11-15 22:31:44 +00:00
|
|
|
// expressed as 1-100
|
2023-11-28 04:45:07 +00:00
|
|
|
Rating null.Int `db:"rating"`
|
|
|
|
Date NullDate `db:"date"`
|
|
|
|
Details zero.String `db:"details"`
|
|
|
|
Photographer zero.String `db:"photographer"`
|
|
|
|
Organized bool `db:"organized"`
|
|
|
|
OCounter int `db:"o_counter"`
|
|
|
|
StudioID null.Int `db:"studio_id,omitempty"`
|
|
|
|
CreatedAt Timestamp `db:"created_at"`
|
|
|
|
UpdatedAt Timestamp `db:"updated_at"`
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (r *imageRow) fromImage(i models.Image) {
|
|
|
|
r.ID = i.ID
|
|
|
|
r.Title = zero.StringFrom(i.Title)
|
2023-11-28 04:45:07 +00:00
|
|
|
r.Code = zero.StringFrom(i.Code)
|
2022-07-13 06:30:54 +00:00
|
|
|
r.Rating = intFromPtr(i.Rating)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.Date = NullDateFromDatePtr(i.Date)
|
2023-11-28 04:45:07 +00:00
|
|
|
r.Details = zero.StringFrom(i.Details)
|
|
|
|
r.Photographer = zero.StringFrom(i.Photographer)
|
2022-07-13 06:30:54 +00:00
|
|
|
r.Organized = i.Organized
|
|
|
|
r.OCounter = i.OCounter
|
|
|
|
r.StudioID = intFromPtr(i.StudioID)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.CreatedAt = Timestamp{Timestamp: i.CreatedAt}
|
|
|
|
r.UpdatedAt = Timestamp{Timestamp: i.UpdatedAt}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
type imageQueryRow struct {
|
|
|
|
imageRow
|
|
|
|
PrimaryFileID null.Int `db:"primary_file_id"`
|
|
|
|
PrimaryFileFolderPath zero.String `db:"primary_file_folder_path"`
|
|
|
|
PrimaryFileBasename zero.String `db:"primary_file_basename"`
|
|
|
|
PrimaryFileChecksum zero.String `db:"primary_file_checksum"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageQueryRow) resolve() *models.Image {
|
|
|
|
ret := &models.Image{
|
2023-11-28 04:45:07 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Title: r.Title.String,
|
|
|
|
Code: r.Code.String,
|
|
|
|
Rating: nullIntPtr(r.Rating),
|
|
|
|
Date: r.Date.DatePtr(),
|
|
|
|
Details: r.Details.String,
|
|
|
|
Photographer: r.Photographer.String,
|
|
|
|
Organized: r.Organized,
|
|
|
|
OCounter: r.OCounter,
|
|
|
|
StudioID: nullIntPtr(r.StudioID),
|
2022-09-01 07:54:34 +00:00
|
|
|
|
|
|
|
PrimaryFileID: nullIntFileIDPtr(r.PrimaryFileID),
|
|
|
|
Checksum: r.PrimaryFileChecksum.String,
|
|
|
|
|
2022-09-19 04:53:46 +00:00
|
|
|
CreatedAt: r.CreatedAt.Timestamp,
|
|
|
|
UpdatedAt: r.UpdatedAt.Timestamp,
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2022-09-01 07:54:34 +00:00
|
|
|
|
|
|
|
if r.PrimaryFileFolderPath.Valid && r.PrimaryFileBasename.Valid {
|
|
|
|
ret.Path = filepath.Join(r.PrimaryFileFolderPath.String, r.PrimaryFileBasename.String)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
type imageRowRecord struct {
|
|
|
|
updateRecord
|
2020-11-04 23:26:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
func (r *imageRowRecord) fromPartial(i models.ImagePartial) {
|
|
|
|
r.setNullString("title", i.Title)
|
2023-11-28 04:45:07 +00:00
|
|
|
r.setNullString("code", i.Code)
|
2022-08-11 06:14:57 +00:00
|
|
|
r.setNullInt("rating", i.Rating)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.setNullDate("date", i.Date)
|
2023-11-28 04:45:07 +00:00
|
|
|
r.setNullString("details", i.Details)
|
|
|
|
r.setNullString("photographer", i.Photographer)
|
2022-08-11 06:14:57 +00:00
|
|
|
r.setBool("organized", i.Organized)
|
|
|
|
r.setInt("o_counter", i.OCounter)
|
|
|
|
r.setNullInt("studio_id", i.StudioID)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.setTimestamp("created_at", i.CreatedAt)
|
|
|
|
r.setTimestamp("updated_at", i.UpdatedAt)
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
type imageRepositoryType struct {
|
2022-07-13 06:30:54 +00:00
|
|
|
repository
|
2024-06-11 01:34:38 +00:00
|
|
|
performers joinRepository
|
|
|
|
galleries joinRepository
|
|
|
|
tags joinRepository
|
|
|
|
files filesRepository
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
func (r *imageRepositoryType) addImagesFilesTable(f *filterBuilder) {
|
|
|
|
f.addLeftJoin(imagesFilesTable, "", "images_files.image_id = images.id")
|
|
|
|
}
|
2022-08-11 06:14:57 +00:00
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
func (r *imageRepositoryType) addFilesTable(f *filterBuilder) {
|
|
|
|
r.addImagesFilesTable(f)
|
|
|
|
f.addLeftJoin(fileTable, "", "images_files.file_id = files.id")
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
func (r *imageRepositoryType) addFoldersTable(f *filterBuilder) {
|
|
|
|
r.addFilesTable(f)
|
|
|
|
f.addLeftJoin(folderTable, "", "files.parent_folder_id = folders.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageRepositoryType) addImageFilesTable(f *filterBuilder) {
|
|
|
|
r.addImagesFilesTable(f)
|
|
|
|
f.addLeftJoin(imageFileTable, "", "image_files.file_id = images_files.file_id")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
imageRepository = imageRepositoryType{
|
2022-07-13 06:30:54 +00:00
|
|
|
repository: repository{
|
|
|
|
tableName: imageTable,
|
|
|
|
idColumn: idColumn,
|
|
|
|
},
|
2024-06-11 01:34:38 +00:00
|
|
|
|
|
|
|
performers: joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tableName: performersImagesTable,
|
|
|
|
idColumn: imageIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: performerIDColumn,
|
|
|
|
},
|
|
|
|
|
|
|
|
galleries: joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tableName: galleriesImagesTable,
|
|
|
|
idColumn: imageIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: galleryIDColumn,
|
|
|
|
},
|
|
|
|
|
|
|
|
files: filesRepository{
|
|
|
|
repository: repository{
|
|
|
|
tableName: imagesFilesTable,
|
|
|
|
idColumn: imageIDColumn,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
tags: joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tableName: imagesTagsTable,
|
|
|
|
idColumn: imageIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: tagIDColumn,
|
|
|
|
foreignTable: tagTable,
|
|
|
|
orderBy: "tags.name ASC",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImageStore struct {
|
|
|
|
tableMgr *table
|
|
|
|
oCounterManager
|
|
|
|
|
|
|
|
repo *storeRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewImageStore(r *storeRepository) *ImageStore {
|
|
|
|
return &ImageStore{
|
2022-07-13 06:30:54 +00:00
|
|
|
tableMgr: imageTableMgr,
|
|
|
|
oCounterManager: oCounterManager{imageTableMgr},
|
2024-06-11 01:34:38 +00:00
|
|
|
repo: r,
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) table() exp.IdentifierExpression {
|
|
|
|
return qb.tableMgr.table
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
func (qb *ImageStore) selectDataset() *goqu.SelectDataset {
|
|
|
|
table := qb.table()
|
|
|
|
files := fileTableMgr.table
|
|
|
|
folders := folderTableMgr.table
|
|
|
|
checksum := fingerprintTableMgr.table
|
|
|
|
|
|
|
|
return dialect.From(table).LeftJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(
|
|
|
|
imagesFilesJoinTable.Col(imageIDColumn).Eq(table.Col(idColumn)),
|
|
|
|
imagesFilesJoinTable.Col("primary").Eq(1),
|
|
|
|
),
|
|
|
|
).LeftJoin(
|
|
|
|
files,
|
|
|
|
goqu.On(files.Col(idColumn).Eq(imagesFilesJoinTable.Col(fileIDColumn))),
|
|
|
|
).LeftJoin(
|
|
|
|
folders,
|
|
|
|
goqu.On(folders.Col(idColumn).Eq(files.Col("parent_folder_id"))),
|
|
|
|
).LeftJoin(
|
|
|
|
checksum,
|
|
|
|
goqu.On(
|
|
|
|
checksum.Col(fileIDColumn).Eq(imagesFilesJoinTable.Col(fileIDColumn)),
|
2023-09-01 00:39:29 +00:00
|
|
|
checksum.Col("type").Eq(models.FingerprintTypeMD5),
|
2023-06-15 02:46:09 +00:00
|
|
|
),
|
|
|
|
).Select(
|
|
|
|
qb.table().All(),
|
|
|
|
imagesFilesJoinTable.Col(fileIDColumn).As("primary_file_id"),
|
|
|
|
folders.Col("path").As("primary_file_folder_path"),
|
|
|
|
files.Col("basename").As("primary_file_basename"),
|
|
|
|
checksum.Col("fingerprint").As("primary_file_checksum"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-11 02:24:15 +00:00
|
|
|
func (qb *ImageStore) Create(ctx context.Context, newObject *models.Image, fileIDs []models.FileID) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
var r imageRow
|
2023-09-11 02:24:15 +00:00
|
|
|
r.fromImage(*newObject)
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
id, err := qb.tableMgr.insertID(ctx, r)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return err
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 02:24:15 +00:00
|
|
|
if len(fileIDs) > 0 {
|
2022-07-13 06:30:54 +00:00
|
|
|
const firstPrimary = true
|
2023-09-11 02:24:15 +00:00
|
|
|
if err := imagesFilesTableMgr.insertJoins(ctx, id, firstPrimary, fileIDs); err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 03:31:53 +00:00
|
|
|
if newObject.URLs.Loaded() {
|
|
|
|
const startPos = 0
|
|
|
|
if err := imagesURLsTableMgr.insertJoins(ctx, id, startPos, newObject.URLs.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
if newObject.PerformerIDs.Loaded() {
|
|
|
|
if err := imagesPerformersTableMgr.insertJoins(ctx, id, newObject.PerformerIDs.List()); err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
if newObject.TagIDs.Loaded() {
|
|
|
|
if err := imagesTagsTableMgr.insertJoins(ctx, id, newObject.TagIDs.List()); err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
|
|
|
|
if newObject.GalleryIDs.Loaded() {
|
|
|
|
if err := imageGalleriesTableMgr.insertJoins(ctx, id, newObject.GalleryIDs.List()); err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
updated, err := qb.find(ctx, id)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
return fmt.Errorf("finding after create: %w", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 02:24:15 +00:00
|
|
|
*newObject = *updated
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) UpdatePartial(ctx context.Context, id int, partial models.ImagePartial) (*models.Image, error) {
|
|
|
|
r := imageRowRecord{
|
|
|
|
updateRecord{
|
|
|
|
Record: make(exp.Record),
|
|
|
|
},
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
r.fromPartial(partial)
|
|
|
|
|
|
|
|
if len(r.Record) > 0 {
|
|
|
|
if err := qb.tableMgr.updateByID(ctx, id, r.Record); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
if partial.GalleryIDs != nil {
|
|
|
|
if err := imageGalleriesTableMgr.modifyJoins(ctx, id, partial.GalleryIDs.IDs, partial.GalleryIDs.Mode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 03:31:53 +00:00
|
|
|
|
|
|
|
if partial.URLs != nil {
|
|
|
|
if err := imagesURLsTableMgr.modifyJoins(ctx, id, partial.URLs.Values, partial.URLs.Mode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
if partial.PerformerIDs != nil {
|
|
|
|
if err := imagesPerformersTableMgr.modifyJoins(ctx, id, partial.PerformerIDs.IDs, partial.PerformerIDs.Mode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if partial.TagIDs != nil {
|
|
|
|
if err := imagesTagsTableMgr.modifyJoins(ctx, id, partial.TagIDs.IDs, partial.TagIDs.Mode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 01:46:18 +00:00
|
|
|
if partial.PrimaryFileID != nil {
|
|
|
|
if err := imagesFilesTableMgr.setPrimary(ctx, id, *partial.PrimaryFileID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return qb.find(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *ImageStore) Update(ctx context.Context, updatedObject *models.Image) error {
|
|
|
|
var r imageRow
|
|
|
|
r.fromImage(*updatedObject)
|
|
|
|
|
|
|
|
if err := qb.tableMgr.updateByID(ctx, updatedObject.ID, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-12 03:31:53 +00:00
|
|
|
if updatedObject.URLs.Loaded() {
|
|
|
|
if err := imagesURLsTableMgr.replaceJoins(ctx, updatedObject.ID, updatedObject.URLs.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
if updatedObject.PerformerIDs.Loaded() {
|
|
|
|
if err := imagesPerformersTableMgr.replaceJoins(ctx, updatedObject.ID, updatedObject.PerformerIDs.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
|
|
|
|
if updatedObject.TagIDs.Loaded() {
|
|
|
|
if err := imagesTagsTableMgr.replaceJoins(ctx, updatedObject.ID, updatedObject.TagIDs.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
|
|
|
|
if updatedObject.GalleryIDs.Loaded() {
|
|
|
|
if err := imageGalleriesTableMgr.replaceJoins(ctx, updatedObject.ID, updatedObject.GalleryIDs.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
if updatedObject.Files.Loaded() {
|
2023-09-01 00:39:29 +00:00
|
|
|
fileIDs := make([]models.FileID, len(updatedObject.Files.List()))
|
2022-09-01 07:54:34 +00:00
|
|
|
for i, f := range updatedObject.Files.List() {
|
2023-05-16 23:30:51 +00:00
|
|
|
fileIDs[i] = f.Base().ID
|
2022-09-01 07:54:34 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
if err := imagesFilesTableMgr.replaceJoins(ctx, updatedObject.ID, fileIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) Destroy(ctx context.Context, id int) error {
|
|
|
|
return qb.tableMgr.destroyExisting(ctx, []int{id})
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
// returns nil, nil if not found
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) Find(ctx context.Context, id int) (*models.Image, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
ret, err := qb.find(ctx, id)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return ret, err
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) FindMany(ctx context.Context, ids []int) ([]*models.Image, error) {
|
2022-08-11 06:14:57 +00:00
|
|
|
images := make([]*models.Image, len(ids))
|
|
|
|
|
2023-03-15 22:08:21 +00:00
|
|
|
if err := batchExec(ids, defaultBatchSize, func(batch []int) error {
|
|
|
|
q := qb.selectDataset().Prepared(true).Where(qb.table().Col(idColumn).In(batch))
|
|
|
|
unsorted, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range unsorted {
|
2024-10-29 00:26:23 +00:00
|
|
|
i := slices.Index(ids, s.ID)
|
2023-03-15 22:08:21 +00:00
|
|
|
images[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2022-08-11 06:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range images {
|
|
|
|
if images[i] == nil {
|
|
|
|
return nil, fmt.Errorf("image with id %d not found", ids[i])
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
// returns nil, sql.ErrNoRows if not found
|
|
|
|
func (qb *ImageStore) find(ctx context.Context, id int) (*models.Image, error) {
|
|
|
|
q := qb.selectDataset().Where(qb.tableMgr.byID(id))
|
|
|
|
|
|
|
|
ret, err := qb.get(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *ImageStore) findBySubquery(ctx context.Context, sq *goqu.SelectDataset) ([]*models.Image, error) {
|
2022-09-01 07:54:34 +00:00
|
|
|
table := qb.table()
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
q := qb.selectDataset().Prepared(true).Where(
|
|
|
|
table.Col(idColumn).Eq(
|
|
|
|
sq,
|
2022-09-01 07:54:34 +00:00
|
|
|
),
|
|
|
|
)
|
2023-06-15 02:46:09 +00:00
|
|
|
|
|
|
|
return qb.getMany(ctx, q)
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
// returns nil, sql.ErrNoRows if not found
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) get(ctx context.Context, q *goqu.SelectDataset) (*models.Image, error) {
|
|
|
|
ret, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) == 0 {
|
|
|
|
return nil, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *ImageStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Image, error) {
|
|
|
|
const single = false
|
2022-08-11 06:14:57 +00:00
|
|
|
var ret []*models.Image
|
2022-09-02 01:18:37 +00:00
|
|
|
var lastID int
|
2022-07-13 06:30:54 +00:00
|
|
|
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
2022-09-01 07:54:34 +00:00
|
|
|
var f imageQueryRow
|
2022-07-13 06:30:54 +00:00
|
|
|
if err := r.StructScan(&f); err != nil {
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
i := f.resolve()
|
|
|
|
|
2022-09-02 01:18:37 +00:00
|
|
|
if i.ID == lastID {
|
|
|
|
return fmt.Errorf("internal error: multiple rows returned for single image id %d", i.ID)
|
|
|
|
}
|
|
|
|
lastID = i.ID
|
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
ret = append(ret, i)
|
2022-07-13 06:30:54 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2024-08-29 01:24:52 +00:00
|
|
|
// Returns the custom cover for the gallery, if one has been set.
|
|
|
|
func (qb *ImageStore) CoverByGalleryID(ctx context.Context, galleryID int) (*models.Image, error) {
|
|
|
|
table := qb.table()
|
|
|
|
|
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
galleriesImagesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(galleriesImagesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).
|
|
|
|
Where(goqu.And(
|
|
|
|
galleriesImagesJoinTable.Col("gallery_id").Eq(galleryID),
|
|
|
|
galleriesImagesJoinTable.Col("cover").Eq(true),
|
|
|
|
))
|
|
|
|
|
|
|
|
q := qb.selectDataset().Prepared(true).Where(
|
|
|
|
table.Col(idColumn).Eq(
|
|
|
|
sq,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
ret, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting cover for gallery %d: %w", galleryID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(ret) > 1:
|
|
|
|
return nil, fmt.Errorf("internal error: multiple covers returned for gallery %d", galleryID)
|
|
|
|
case len(ret) == 1:
|
|
|
|
return ret[0], nil
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) GetFiles(ctx context.Context, id int) ([]models.File, error) {
|
2024-06-11 01:34:38 +00:00
|
|
|
fileIDs, err := imageRepository.files.get(ctx, id)
|
2022-08-11 06:14:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use fileStore to load files
|
2024-06-11 01:34:38 +00:00
|
|
|
files, err := qb.repo.File.Find(ctx, fileIDs...)
|
2022-08-11 06:14:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-02 02:19:13 +00:00
|
|
|
ret := make([]models.File, len(files))
|
|
|
|
copy(ret, files)
|
|
|
|
|
|
|
|
return ret, nil
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) GetManyFileIDs(ctx context.Context, ids []int) ([][]models.FileID, error) {
|
2022-09-01 07:54:34 +00:00
|
|
|
const primaryOnly = false
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.files.getMany(ctx, ids, primaryOnly)
|
2022-09-01 07:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) FindByFileID(ctx context.Context, fileID models.FileID) ([]*models.Image, error) {
|
2022-08-01 01:01:29 +00:00
|
|
|
table := qb.table()
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-08-01 01:01:29 +00:00
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(imagesFilesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).Where(imagesFilesJoinTable.Col(fileIDColumn).Eq(fileID))
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
ret, err := qb.findBySubquery(ctx, sq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting image by file id %d: %w", fileID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) CountByFileID(ctx context.Context, fileID models.FileID) (int, error) {
|
2022-07-18 00:51:59 +00:00
|
|
|
joinTable := imagesFilesJoinTable
|
|
|
|
|
|
|
|
q := dialect.Select(goqu.COUNT("*")).From(joinTable).Where(joinTable.Col(fileIDColumn).Eq(fileID))
|
|
|
|
return count(ctx, q)
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) FindByFingerprints(ctx context.Context, fp []models.Fingerprint) ([]*models.Image, error) {
|
2022-08-01 01:01:29 +00:00
|
|
|
table := qb.table()
|
|
|
|
fingerprintTable := fingerprintTableMgr.table
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
var ex []exp.Expression
|
|
|
|
|
|
|
|
for _, v := range fp {
|
|
|
|
ex = append(ex, goqu.And(
|
2022-08-01 01:01:29 +00:00
|
|
|
fingerprintTable.Col("type").Eq(v.Type),
|
|
|
|
fingerprintTable.Col("fingerprint").Eq(v.Fingerprint),
|
2022-07-13 06:30:54 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:01:29 +00:00
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(imagesFilesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
InnerJoin(
|
|
|
|
fingerprintTable,
|
|
|
|
goqu.On(fingerprintTable.Col(fileIDColumn).Eq(imagesFilesJoinTable.Col(fileIDColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).Where(goqu.Or(ex...))
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
ret, err := qb.findBySubquery(ctx, sq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting image by fingerprints: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *ImageStore) FindByChecksum(ctx context.Context, checksum string) ([]*models.Image, error) {
|
2023-09-01 00:39:29 +00:00
|
|
|
return qb.FindByFingerprints(ctx, []models.Fingerprint{
|
2022-08-01 01:01:29 +00:00
|
|
|
{
|
2023-09-01 00:39:29 +00:00
|
|
|
Type: models.FingerprintTypeMD5,
|
2022-08-01 01:01:29 +00:00
|
|
|
Fingerprint: checksum,
|
|
|
|
},
|
|
|
|
})
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:59:20 +00:00
|
|
|
var defaultGalleryOrder = []exp.OrderedExpression{
|
|
|
|
goqu.L("COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI").Asc(),
|
|
|
|
goqu.L("COALESCE(images.title, images.id) COLLATE NATURAL_CI").Asc(),
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) FindByGalleryID(ctx context.Context, galleryID int) ([]*models.Image, error) {
|
2022-08-01 01:01:29 +00:00
|
|
|
table := qb.table()
|
|
|
|
|
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
galleriesImagesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(galleriesImagesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).Where(
|
|
|
|
galleriesImagesJoinTable.Col("gallery_id").Eq(galleryID),
|
|
|
|
)
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
q := qb.selectDataset().Prepared(true).Where(
|
2022-08-11 06:14:57 +00:00
|
|
|
table.Col(idColumn).Eq(
|
2022-08-01 01:01:29 +00:00
|
|
|
sq,
|
|
|
|
),
|
2024-09-05 02:59:20 +00:00
|
|
|
).Order(defaultGalleryOrder...)
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
ret, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting images for gallery %d: %w", galleryID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 22:59:41 +00:00
|
|
|
func (qb *ImageStore) FindByGalleryIDIndex(ctx context.Context, galleryID int, index uint) (*models.Image, error) {
|
|
|
|
table := qb.table()
|
|
|
|
|
|
|
|
q := qb.selectDataset().
|
|
|
|
InnerJoin(
|
|
|
|
galleriesImagesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(galleriesImagesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
Where(galleriesImagesJoinTable.Col(galleryIDColumn).Eq(galleryID)).
|
|
|
|
Prepared(true).
|
2024-09-05 02:59:20 +00:00
|
|
|
Order(defaultGalleryOrder...).
|
2024-08-27 22:59:41 +00:00
|
|
|
Limit(1).Offset(index)
|
|
|
|
|
|
|
|
ret, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting images for gallery %d: %w", galleryID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret[0], nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) CountByGalleryID(ctx context.Context, galleryID int) (int, error) {
|
|
|
|
joinTable := goqu.T(galleriesImagesTable)
|
|
|
|
|
|
|
|
q := dialect.Select(goqu.COUNT("*")).From(joinTable).Where(joinTable.Col("gallery_id").Eq(galleryID))
|
|
|
|
return count(ctx, q)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 21:01:41 +00:00
|
|
|
func (qb *ImageStore) OCountByPerformerID(ctx context.Context, performerID int) (int, error) {
|
|
|
|
table := qb.table()
|
|
|
|
joinTable := performersImagesJoinTable
|
|
|
|
q := dialect.Select(goqu.COALESCE(goqu.SUM("o_counter"), 0)).From(table).InnerJoin(joinTable, goqu.On(table.Col(idColumn).Eq(joinTable.Col(imageIDColumn)))).Where(joinTable.Col(performerIDColumn).Eq(performerID))
|
|
|
|
|
|
|
|
var ret int
|
|
|
|
if err := querySimple(ctx, q, &ret); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 05:19:41 +00:00
|
|
|
func (qb *ImageStore) OCount(ctx context.Context) (int, error) {
|
|
|
|
table := qb.table()
|
|
|
|
|
|
|
|
q := dialect.Select(goqu.COALESCE(goqu.SUM("o_counter"), 0)).From(table)
|
|
|
|
var ret int
|
|
|
|
if err := querySimple(ctx, q, &ret); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) FindByFolderID(ctx context.Context, folderID models.FolderID) ([]*models.Image, error) {
|
2022-08-01 01:01:29 +00:00
|
|
|
table := qb.table()
|
|
|
|
fileTable := goqu.T(fileTable)
|
|
|
|
|
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(imagesFilesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
InnerJoin(
|
|
|
|
fileTable,
|
|
|
|
goqu.On(imagesFilesJoinTable.Col(fileIDColumn).Eq(fileTable.Col(idColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).Where(
|
|
|
|
fileTable.Col("parent_folder_id").Eq(folderID),
|
|
|
|
)
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
ret, err := qb.findBySubquery(ctx, sq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting image by folder: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) FindByZipFileID(ctx context.Context, zipFileID models.FileID) ([]*models.Image, error) {
|
2022-08-01 01:01:29 +00:00
|
|
|
table := qb.table()
|
|
|
|
fileTable := goqu.T(fileTable)
|
|
|
|
|
|
|
|
sq := dialect.From(table).
|
|
|
|
InnerJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(imagesFilesJoinTable.Col(imageIDColumn))),
|
|
|
|
).
|
|
|
|
InnerJoin(
|
|
|
|
fileTable,
|
|
|
|
goqu.On(imagesFilesJoinTable.Col(fileIDColumn).Eq(fileTable.Col(idColumn))),
|
|
|
|
).
|
|
|
|
Select(table.Col(idColumn)).Where(
|
|
|
|
fileTable.Col("zip_file_id").Eq(zipFileID),
|
|
|
|
)
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
ret, err := qb.findBySubquery(ctx, sq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting image by zip file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) Count(ctx context.Context) (int, error) {
|
|
|
|
q := dialect.Select(goqu.COUNT("*")).From(qb.table())
|
|
|
|
return count(ctx, q)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) Size(ctx context.Context) (float64, error) {
|
|
|
|
table := qb.table()
|
|
|
|
fileTable := fileTableMgr.table
|
|
|
|
q := dialect.Select(
|
2023-12-22 10:40:05 +00:00
|
|
|
goqu.COALESCE(goqu.SUM(fileTableMgr.table.Col("size")), 0),
|
2022-07-13 06:30:54 +00:00
|
|
|
).From(table).InnerJoin(
|
|
|
|
imagesFilesJoinTable,
|
|
|
|
goqu.On(table.Col(idColumn).Eq(imagesFilesJoinTable.Col(imageIDColumn))),
|
|
|
|
).InnerJoin(
|
|
|
|
fileTable,
|
|
|
|
goqu.On(imagesFilesJoinTable.Col(fileIDColumn).Eq(fileTable.Col(idColumn))),
|
|
|
|
)
|
|
|
|
var ret float64
|
|
|
|
if err := querySimple(ctx, q, &ret); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) All(ctx context.Context) ([]*models.Image, error) {
|
|
|
|
return qb.getMany(ctx, qb.selectDataset())
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) makeQuery(ctx context.Context, imageFilter *models.ImageFilterType, findFilter *models.FindFilterType) (*queryBuilder, error) {
|
2021-05-03 03:09:46 +00:00
|
|
|
if imageFilter == nil {
|
|
|
|
imageFilter = &models.ImageFilterType{}
|
|
|
|
}
|
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &models.FindFilterType{}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
query := imageRepository.newQuery()
|
2021-10-25 00:40:13 +00:00
|
|
|
distinctIDs(&query, imageTable)
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2022-08-01 01:01:29 +00:00
|
|
|
query.addJoins(
|
|
|
|
join{
|
|
|
|
table: imagesFilesTable,
|
|
|
|
onClause: "images_files.image_id = images.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: fileTable,
|
|
|
|
onClause: "images_files.file_id = files.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: folderTable,
|
|
|
|
onClause: "files.parent_folder_id = folders.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: fingerprintTable,
|
|
|
|
onClause: "files_fingerprints.file_id = images_files.file_id",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-10-26 07:48:13 +00:00
|
|
|
filepathColumn := "folders.path || '" + string(filepath.Separator) + "' || files.basename"
|
|
|
|
searchColumns := []string{"images.title", filepathColumn, "files_fingerprints.fingerprint"}
|
2021-11-22 03:59:22 +00:00
|
|
|
query.parseQueryString(searchColumns, *q)
|
2021-04-09 08:46:00 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
filter := filterBuilderFromHandler(ctx, &imageFilterHandler{
|
|
|
|
imageFilter: imageFilter,
|
|
|
|
})
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2023-02-13 01:14:41 +00:00
|
|
|
if err := query.addFilter(filter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2024-05-22 04:59:08 +00:00
|
|
|
if err := qb.setImageSortAndPagination(&query, findFilter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return &query, nil
|
2021-04-15 00:46:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) Query(ctx context.Context, options models.ImageQueryOptions) (*models.ImageQueryResult, error) {
|
2022-05-19 07:49:32 +00:00
|
|
|
query, err := qb.makeQuery(ctx, options.ImageFilter, options.FindFilter)
|
2021-05-03 03:09:46 +00:00
|
|
|
if err != nil {
|
2021-10-25 00:40:13 +00:00
|
|
|
return nil, err
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
result, err := qb.queryGroupedFields(ctx, options, *query)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
2021-10-25 00:40:13 +00:00
|
|
|
return nil, fmt.Errorf("error querying aggregate fields: %w", err)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
idsResult, err := query.findIDs(ctx)
|
2021-10-25 00:40:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error finding IDs: %w", err)
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
result.IDs = idsResult
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.ImageQueryOptions, query queryBuilder) (*models.ImageQueryResult, error) {
|
2021-10-25 00:40:13 +00:00
|
|
|
if !options.Count && !options.Megapixels && !options.TotalSize {
|
|
|
|
// nothing to do - return empty result
|
|
|
|
return models.NewImageQueryResult(qb), nil
|
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
aggregateQuery := imageRepository.newQuery()
|
2021-10-25 00:40:13 +00:00
|
|
|
|
|
|
|
if options.Count {
|
2022-11-14 05:35:09 +00:00
|
|
|
aggregateQuery.addColumn("COUNT(DISTINCT temp.id) as total")
|
2021-10-25 00:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 06:16:40 +00:00
|
|
|
if options.Megapixels {
|
|
|
|
query.addJoins(
|
|
|
|
join{
|
|
|
|
table: imagesFilesTable,
|
|
|
|
onClause: "images_files.image_id = images.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: imageFileTable,
|
|
|
|
onClause: "images_files.file_id = image_files.file_id",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
query.addColumn("COALESCE(image_files.width, 0) * COALESCE(image_files.height, 0) as megapixels")
|
|
|
|
aggregateQuery.addColumn("COALESCE(SUM(temp.megapixels), 0) / 1000000 as megapixels")
|
|
|
|
}
|
2021-10-25 00:40:13 +00:00
|
|
|
|
2023-10-16 06:16:40 +00:00
|
|
|
if options.TotalSize {
|
|
|
|
query.addJoins(
|
|
|
|
join{
|
|
|
|
table: imagesFilesTable,
|
|
|
|
onClause: "images_files.image_id = images.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: fileTable,
|
|
|
|
onClause: "images_files.file_id = files.id",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
query.addColumn("COALESCE(files.size, 0) as size")
|
|
|
|
aggregateQuery.addColumn("SUM(temp.size) as size")
|
|
|
|
}
|
2021-10-25 00:40:13 +00:00
|
|
|
|
|
|
|
const includeSortPagination = false
|
|
|
|
aggregateQuery.from = fmt.Sprintf("(%s) as temp", query.toSQL(includeSortPagination))
|
|
|
|
|
|
|
|
out := struct {
|
|
|
|
Total int
|
2023-10-16 06:16:40 +00:00
|
|
|
Megapixels null.Float
|
|
|
|
Size null.Float
|
2021-10-25 00:40:13 +00:00
|
|
|
}{}
|
2024-06-11 01:34:38 +00:00
|
|
|
if err := imageRepository.queryStruct(ctx, aggregateQuery.toSQL(includeSortPagination), query.args, &out); err != nil {
|
2021-10-25 00:40:13 +00:00
|
|
|
return nil, err
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
ret := models.NewImageQueryResult(qb)
|
|
|
|
ret.Count = out.Total
|
2023-10-16 06:16:40 +00:00
|
|
|
ret.Megapixels = out.Megapixels.Float64
|
|
|
|
ret.TotalSize = out.Size.Float64
|
2021-10-25 00:40:13 +00:00
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) QueryCount(ctx context.Context, imageFilter *models.ImageFilterType, findFilter *models.FindFilterType) (int, error) {
|
2022-05-19 07:49:32 +00:00
|
|
|
query, err := qb.makeQuery(ctx, imageFilter, findFilter)
|
2021-05-03 03:09:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return query.executeCount(ctx)
|
2021-04-15 00:46:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 04:59:08 +00:00
|
|
|
var imageSortOptions = sortOptions{
|
|
|
|
"created_at",
|
|
|
|
"date",
|
|
|
|
"file_count",
|
|
|
|
"file_mod_time",
|
|
|
|
"filesize",
|
|
|
|
"id",
|
|
|
|
"o_counter",
|
|
|
|
"path",
|
|
|
|
"performer_count",
|
|
|
|
"random",
|
|
|
|
"rating",
|
|
|
|
"tag_count",
|
|
|
|
"title",
|
|
|
|
"updated_at",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *models.FindFilterType) error {
|
2022-08-01 01:01:29 +00:00
|
|
|
sortClause := ""
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2022-08-01 01:01:29 +00:00
|
|
|
if findFilter != nil && findFilter.Sort != nil && *findFilter.Sort != "" {
|
|
|
|
sort := findFilter.GetSort("title")
|
|
|
|
direction := findFilter.GetDirection()
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2024-05-22 04:59:08 +00:00
|
|
|
// CVE-2024-32231 - ensure sort is in the list of allowed sorts
|
|
|
|
if err := imageSortOptions.validateSort(sort); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:01:29 +00:00
|
|
|
// translate sort field
|
|
|
|
if sort == "file_mod_time" {
|
|
|
|
sort = "mod_time"
|
|
|
|
}
|
|
|
|
|
|
|
|
addFilesJoin := func() {
|
|
|
|
q.addJoins(
|
|
|
|
join{
|
|
|
|
table: imagesFilesTable,
|
|
|
|
onClause: "images_files.image_id = images.id",
|
|
|
|
},
|
|
|
|
join{
|
|
|
|
table: fileTable,
|
|
|
|
onClause: "images_files.file_id = files.id",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-25 02:07:55 +00:00
|
|
|
addFolderJoin := func() {
|
2022-08-01 01:01:29 +00:00
|
|
|
q.addJoins(join{
|
|
|
|
table: folderTable,
|
|
|
|
onClause: "files.parent_folder_id = folders.id",
|
|
|
|
})
|
2022-09-25 02:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch sort {
|
|
|
|
case "path":
|
|
|
|
addFilesJoin()
|
|
|
|
addFolderJoin()
|
2023-04-17 05:21:13 +00:00
|
|
|
sortClause = " ORDER BY COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI " + direction
|
2022-08-01 01:01:29 +00:00
|
|
|
case "file_count":
|
|
|
|
sortClause = getCountSort(imageTable, imagesFilesTable, imageIDColumn, direction)
|
|
|
|
case "tag_count":
|
|
|
|
sortClause = getCountSort(imageTable, imagesTagsTable, imageIDColumn, direction)
|
|
|
|
case "performer_count":
|
|
|
|
sortClause = getCountSort(imageTable, performersImagesTable, imageIDColumn, direction)
|
2022-08-11 06:14:57 +00:00
|
|
|
case "mod_time", "filesize":
|
2022-08-01 01:01:29 +00:00
|
|
|
addFilesJoin()
|
|
|
|
sortClause = getSort(sort, direction, "files")
|
2022-09-25 02:07:55 +00:00
|
|
|
case "title":
|
|
|
|
addFilesJoin()
|
|
|
|
addFolderJoin()
|
2023-04-17 05:21:13 +00:00
|
|
|
sortClause = " ORDER BY COALESCE(images.title, files.basename) COLLATE NATURAL_CI " + direction + ", folders.path COLLATE NATURAL_CI " + direction
|
2022-08-01 01:01:29 +00:00
|
|
|
default:
|
|
|
|
sortClause = getSort(sort, direction, "images")
|
|
|
|
}
|
2023-04-17 05:21:13 +00:00
|
|
|
|
|
|
|
// Whatever the sorting, always use title/id as a final sort
|
|
|
|
sortClause += ", COALESCE(images.title, images.id) COLLATE NATURAL_CI ASC"
|
2021-04-09 08:46:00 +00:00
|
|
|
}
|
2022-08-01 01:01:29 +00:00
|
|
|
|
|
|
|
q.sortAndPagination = sortClause + getPagination(findFilter)
|
2024-05-22 04:59:08 +00:00
|
|
|
|
|
|
|
return nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *ImageStore) AddFileID(ctx context.Context, id int, fileID models.FileID) error {
|
2022-08-12 02:21:46 +00:00
|
|
|
const firstPrimary = false
|
2023-09-01 00:39:29 +00:00
|
|
|
return imagesFilesTableMgr.insertJoins(ctx, id, firstPrimary, []models.FileID{fileID})
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 01:27:31 +00:00
|
|
|
// RemoveFileID removes the file ID from the image.
|
|
|
|
// If the file ID is the primary file, then the next file in the list is set as the primary file.
|
|
|
|
func (qb *ImageStore) RemoveFileID(ctx context.Context, id int, fileID models.FileID) error {
|
|
|
|
fileIDs, err := imagesFilesTableMgr.get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting file IDs for image %d: %w", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileIDs = sliceutil.Filter(fileIDs, func(f models.FileID) bool {
|
|
|
|
return f != fileID
|
|
|
|
})
|
|
|
|
|
|
|
|
return imagesFilesTableMgr.replaceJoins(ctx, id, fileIDs)
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
func (qb *ImageStore) GetGalleryIDs(ctx context.Context, imageID int) ([]int, error) {
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.galleries.getIDs(ctx, imageID)
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
// func (qb *imageQueryBuilder) UpdateGalleries(ctx context.Context, imageID int, galleryIDs []int) error {
|
|
|
|
// // Delete the existing joins and then create new ones
|
|
|
|
// return qb.galleriesRepository().replace(ctx, imageID, galleryIDs)
|
|
|
|
// }
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) GetPerformerIDs(ctx context.Context, imageID int) ([]int, error) {
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.performers.getIDs(ctx, imageID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) UpdatePerformers(ctx context.Context, imageID int, performerIDs []int) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
// Delete the existing joins and then create new ones
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.performers.replace(ctx, imageID, performerIDs)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) GetTagIDs(ctx context.Context, imageID int) ([]int, error) {
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.tags.getIDs(ctx, imageID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *ImageStore) UpdateTags(ctx context.Context, imageID int, tagIDs []int) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
// Delete the existing joins and then create new ones
|
2024-06-11 01:34:38 +00:00
|
|
|
return imageRepository.tags.replace(ctx, imageID, tagIDs)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2023-09-12 03:31:53 +00:00
|
|
|
|
|
|
|
func (qb *ImageStore) GetURLs(ctx context.Context, imageID int) ([]string, error) {
|
|
|
|
return imagesURLsTableMgr.get(ctx, imageID)
|
|
|
|
}
|