mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
[Files Refactor] Bug fixes (#2868)
* Return error if multiple rows returned for id * Add missing LoadFiles calls * Show id if path is empty
This commit is contained in:
@@ -40,6 +40,10 @@ func (s *Service) Destroy(ctx context.Context, i *models.Gallery, fileDeleter *i
|
||||
}
|
||||
|
||||
func (s *Service) destroyZipFileImages(ctx context.Context, i *models.Gallery, fileDeleter *image.FileDeleter, deleteGenerated, deleteFile bool) ([]*models.Image, error) {
|
||||
if err := i.LoadFiles(ctx, s.Repository); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var imgsDestroyed []*models.Image
|
||||
|
||||
destroyer := &file.ZipDestroyer{
|
||||
|
||||
@@ -97,7 +97,7 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
|
||||
}
|
||||
|
||||
if !found {
|
||||
logger.Infof("Adding %s to gallery %s", f.Base().Path, i.GetTitle())
|
||||
logger.Infof("Adding %s to gallery %s", f.Base().Path, i.DisplayName())
|
||||
|
||||
if err := h.CreatorUpdater.AddFileID(ctx, i.ID, f.Base().ID); err != nil {
|
||||
return fmt.Errorf("adding file to gallery: %w", err)
|
||||
|
||||
@@ -15,6 +15,7 @@ type FinderByFile interface {
|
||||
type Repository interface {
|
||||
FinderByFile
|
||||
Destroy(ctx context.Context, id int) error
|
||||
models.FileLoader
|
||||
}
|
||||
|
||||
type ImageFinder interface {
|
||||
|
||||
@@ -81,6 +81,10 @@ func (s *Service) destroyImage(ctx context.Context, i *models.Image, fileDeleter
|
||||
|
||||
// deleteFiles deletes files for the image from the database and file system, if they are not in use by other images
|
||||
func (s *Service) deleteFiles(ctx context.Context, i *models.Image, fileDeleter *FileDeleter) error {
|
||||
if err := i.LoadFiles(ctx, s.Repository); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range i.Files.List() {
|
||||
// only delete files where there is no other associated image
|
||||
otherImages, err := s.Repository.FindByFileID(ctx, f.ID)
|
||||
|
||||
@@ -159,7 +159,7 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
|
||||
}
|
||||
|
||||
if !found {
|
||||
logger.Infof("Adding %s to image %s", f.Path, i.GetTitle())
|
||||
logger.Infof("Adding %s to image %s", f.Path, i.DisplayName())
|
||||
|
||||
// associate with folder-based gallery if applicable
|
||||
if h.ScanConfig.GetCreateGalleriesFromFolders() {
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/stashapp/stash/pkg/file"
|
||||
@@ -128,6 +129,20 @@ func (g Gallery) GetTitle() string {
|
||||
return g.Path
|
||||
}
|
||||
|
||||
// DisplayName returns a display name for the scene for logging purposes.
|
||||
// It returns the path or title, or otherwise it returns the ID if both of these are empty.
|
||||
func (g Gallery) DisplayName() string {
|
||||
if g.Path != "" {
|
||||
return g.Path
|
||||
}
|
||||
|
||||
if g.Title != "" {
|
||||
return g.Title
|
||||
}
|
||||
|
||||
return strconv.Itoa(g.ID)
|
||||
}
|
||||
|
||||
const DefaultGthumbWidth int = 640
|
||||
|
||||
type Galleries []*Gallery
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/stashapp/stash/pkg/file"
|
||||
@@ -96,6 +97,16 @@ func (i Image) GetTitle() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// DisplayName returns a display name for the scene for logging purposes.
|
||||
// It returns Path if not empty, otherwise it returns the ID.
|
||||
func (i Image) DisplayName() string {
|
||||
if i.Path != "" {
|
||||
return i.Path
|
||||
}
|
||||
|
||||
return strconv.Itoa(i.ID)
|
||||
}
|
||||
|
||||
type ImageCreateInput struct {
|
||||
*Image
|
||||
FileIDs []file.ID
|
||||
|
||||
@@ -222,6 +222,16 @@ func (s Scene) GetTitle() string {
|
||||
return filepath.Base(s.Path)
|
||||
}
|
||||
|
||||
// DisplayName returns a display name for the scene for logging purposes.
|
||||
// It returns Path if not empty, otherwise it returns the ID.
|
||||
func (s Scene) DisplayName() string {
|
||||
if s.Path != "" {
|
||||
return s.Path
|
||||
}
|
||||
|
||||
return strconv.Itoa(s.ID)
|
||||
}
|
||||
|
||||
// GetHash returns the hash of the scene, based on the hash algorithm provided. If
|
||||
// hash algorithm is MD5, then Checksum is returned. Otherwise, OSHash is returned.
|
||||
func (s Scene) GetHash(hashAlgorithm HashAlgorithm) string {
|
||||
|
||||
@@ -161,6 +161,10 @@ func (s *Service) Destroy(ctx context.Context, scene *models.Scene, fileDeleter
|
||||
|
||||
// deleteFiles deletes files from the database and file system
|
||||
func (s *Service) deleteFiles(ctx context.Context, scene *models.Scene, fileDeleter *FileDeleter) error {
|
||||
if err := scene.LoadFiles(ctx, s.Repository); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range scene.Files.List() {
|
||||
// only delete files where there is no other associated scene
|
||||
otherScenes, err := s.Repository.FindByFileID(ctx, f.ID)
|
||||
|
||||
@@ -130,7 +130,7 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
|
||||
}
|
||||
|
||||
if !found {
|
||||
logger.Infof("Adding %s to scene %s", f.Path, s.GetTitle())
|
||||
logger.Infof("Adding %s to scene %s", f.Path, s.DisplayName())
|
||||
|
||||
if err := h.CreatorUpdater.AddFileID(ctx, s.ID, f.ID); err != nil {
|
||||
return fmt.Errorf("adding file to scene: %w", err)
|
||||
|
||||
@@ -14,6 +14,7 @@ type FinderByFile interface {
|
||||
type Repository interface {
|
||||
FinderByFile
|
||||
Destroyer
|
||||
models.VideoFileLoader
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
|
||||
@@ -806,6 +806,10 @@ func (c Client) SubmitSceneDraft(ctx context.Context, scene *models.Scene, endpo
|
||||
fingerprints := []*graphql.FingerprintInput{}
|
||||
|
||||
// submit all file fingerprints
|
||||
if err := scene.LoadFiles(ctx, r.Scene); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, f := range scene.Files.List() {
|
||||
duration := f.Duration
|
||||
|
||||
@@ -888,6 +892,10 @@ func (c Client) SubmitSceneDraft(ctx context.Context, scene *models.Scene, endpo
|
||||
}
|
||||
}
|
||||
|
||||
if err := scene.LoadStashIDs(ctx, r.Scene); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stashIDs := scene.StashIDs.List()
|
||||
var stashID *string
|
||||
for _, v := range stashIDs {
|
||||
|
||||
@@ -299,6 +299,7 @@ func (qb *GalleryStore) get(ctx context.Context, q *goqu.SelectDataset) (*models
|
||||
func (qb *GalleryStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Gallery, error) {
|
||||
const single = false
|
||||
var ret []*models.Gallery
|
||||
var lastID int
|
||||
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
||||
var f galleryQueryRow
|
||||
if err := r.StructScan(&f); err != nil {
|
||||
@@ -307,6 +308,11 @@ func (qb *GalleryStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*
|
||||
|
||||
s := f.resolve()
|
||||
|
||||
if s.ID == lastID {
|
||||
return fmt.Errorf("internal error: multiple rows returned for single gallery id %d", s.ID)
|
||||
}
|
||||
lastID = s.ID
|
||||
|
||||
ret = append(ret, s)
|
||||
return nil
|
||||
}); err != nil {
|
||||
|
||||
@@ -315,6 +315,7 @@ func (qb *ImageStore) get(ctx context.Context, q *goqu.SelectDataset) (*models.I
|
||||
func (qb *ImageStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Image, error) {
|
||||
const single = false
|
||||
var ret []*models.Image
|
||||
var lastID int
|
||||
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
||||
var f imageQueryRow
|
||||
if err := r.StructScan(&f); err != nil {
|
||||
@@ -323,6 +324,11 @@ func (qb *ImageStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*mo
|
||||
|
||||
i := f.resolve()
|
||||
|
||||
if i.ID == lastID {
|
||||
return fmt.Errorf("internal error: multiple rows returned for single image id %d", i.ID)
|
||||
}
|
||||
lastID = i.ID
|
||||
|
||||
ret = append(ret, i)
|
||||
return nil
|
||||
}); err != nil {
|
||||
|
||||
@@ -410,6 +410,7 @@ func (qb *SceneStore) get(ctx context.Context, q *goqu.SelectDataset) (*models.S
|
||||
func (qb *SceneStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Scene, error) {
|
||||
const single = false
|
||||
var ret []*models.Scene
|
||||
var lastID int
|
||||
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
||||
var f sceneQueryRow
|
||||
if err := r.StructScan(&f); err != nil {
|
||||
@@ -417,6 +418,10 @@ func (qb *SceneStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*mo
|
||||
}
|
||||
|
||||
s := f.resolve()
|
||||
if s.ID == lastID {
|
||||
return fmt.Errorf("internal error: multiple rows returned for single scene id %d", s.ID)
|
||||
}
|
||||
lastID = s.ID
|
||||
|
||||
ret = append(ret, s)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user