From 967a25f64a33237a14f93dac3089c01fdc7757cc Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 27 Feb 2023 10:28:00 +1100 Subject: [PATCH] Include database cover in draft submission if filesystem version not present (#3465) * Fix styling of stash-box dropdown * Use cover blob in draft submission if present --- internal/api/resolver_mutation_stash_box.go | 11 +++++++++-- internal/manager/repository.go | 2 ++ pkg/scene/screenshot.go | 15 +++++++++++++++ pkg/scraper/stashbox/stash_box.go | 14 +++----------- ui/v2.5/src/components/Dialogs/SubmitDraft.tsx | 2 +- ui/v2.5/src/docs/en/Changelog/v0200.md | 1 + 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/internal/api/resolver_mutation_stash_box.go b/internal/api/resolver_mutation_stash_box.go index d1a7e2de2..c01625688 100644 --- a/internal/api/resolver_mutation_stash_box.go +++ b/internal/api/resolver_mutation_stash_box.go @@ -58,9 +58,16 @@ func (r *mutationResolver) SubmitStashBoxSceneDraft(ctx context.Context, input S return err } - filepath := manager.GetInstance().Paths.Scene.GetScreenshotPath(scene.GetHash(config.GetInstance().GetVideoFileNamingAlgorithm())) + if scene == nil { + return fmt.Errorf("scene with id %d not found", id) + } - res, err = client.SubmitSceneDraft(ctx, scene, boxes[input.StashBoxIndex].Endpoint, filepath) + cover, err := r.sceneService.GetCover(ctx, scene) + if err != nil { + return fmt.Errorf("getting scene cover: %w", err) + } + + res, err = client.SubmitSceneDraft(ctx, scene, boxes[input.StashBoxIndex].Endpoint, cover) return err }) diff --git a/internal/manager/repository.go b/internal/manager/repository.go index 713e017b4..766914fd8 100644 --- a/internal/manager/repository.go +++ b/internal/manager/repository.go @@ -100,6 +100,8 @@ type SceneService interface { AssignFile(ctx context.Context, sceneID int, fileID file.ID) error Merge(ctx context.Context, sourceIDs []int, destinationID int, values models.ScenePartial) error Destroy(ctx context.Context, scene *models.Scene, fileDeleter *scene.FileDeleter, deleteGenerated, deleteFile bool) error + + GetCover(ctx context.Context, scene *models.Scene) ([]byte, error) } type ImageService interface { diff --git a/pkg/scene/screenshot.go b/pkg/scene/screenshot.go index 8335c53d6..c33203327 100644 --- a/pkg/scene/screenshot.go +++ b/pkg/scene/screenshot.go @@ -8,6 +8,7 @@ import ( "os" "github.com/stashapp/stash/pkg/file" + "github.com/stashapp/stash/pkg/fsutil" "github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models/paths" @@ -86,3 +87,17 @@ func SetScreenshot(paths *paths.Paths, checksum string, imageData []byte) error return err } + +func (s *Service) GetCover(ctx context.Context, scene *models.Scene) ([]byte, error) { + if scene.Path != "" { + filepath := s.Paths.Scene.GetScreenshotPath(scene.GetHash(s.Config.GetVideoFileNamingAlgorithm())) + + // fall back to the scene image blob if the file isn't present + screenshotExists, _ := fsutil.FileExists(filepath) + if screenshotExists { + return os.ReadFile(filepath) + } + } + + return s.Repository.GetCover(ctx, scene.ID) +} diff --git a/pkg/scraper/stashbox/stash_box.go b/pkg/scraper/stashbox/stash_box.go index 69b200614..8265d8050 100644 --- a/pkg/scraper/stashbox/stash_box.go +++ b/pkg/scraper/stashbox/stash_box.go @@ -10,7 +10,6 @@ import ( "io" "mime/multipart" "net/http" - "os" "strconv" "strings" @@ -20,7 +19,6 @@ import ( "github.com/Yamashou/gqlgenc/graphqljson" "github.com/stashapp/stash/pkg/file" - "github.com/stashapp/stash/pkg/fsutil" "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/match" "github.com/stashapp/stash/pkg/models" @@ -804,7 +802,7 @@ func appendFingerprintUnique(v []*graphql.FingerprintInput, toAdd *graphql.Finge return append(v, toAdd) } -func (c Client) SubmitSceneDraft(ctx context.Context, scene *models.Scene, endpoint string, imagePath string) (*string, error) { +func (c Client) SubmitSceneDraft(ctx context.Context, scene *models.Scene, endpoint string, cover []byte) (*string, error) { draft := graphql.SceneDraftInput{} var image io.Reader r := c.repository @@ -934,14 +932,8 @@ func (c Client) SubmitSceneDraft(ctx context.Context, scene *models.Scene, endpo } draft.Tags = tags - if imagePath != "" { - exists, _ := fsutil.FileExists(imagePath) - if exists { - file, err := os.Open(imagePath) - if err == nil { - image = file - } - } + if cover != nil { + image = bytes.NewReader(cover) } if err := scene.LoadStashIDs(ctx, r.Scene); err != nil { diff --git a/ui/v2.5/src/components/Dialogs/SubmitDraft.tsx b/ui/v2.5/src/components/Dialogs/SubmitDraft.tsx index ac0e99937..f00e4b213 100644 --- a/ui/v2.5/src/components/Dialogs/SubmitDraft.tsx +++ b/ui/v2.5/src/components/Dialogs/SubmitDraft.tsx @@ -96,7 +96,7 @@ export const SubmitStashBoxDraft: React.FC = ({ {boxes.map((box, i) => (