mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
* Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
207 lines
5.6 KiB
Go
207 lines
5.6 KiB
Go
package scene
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
|
|
"github.com/stashapp/stash/pkg/file"
|
|
"github.com/stashapp/stash/pkg/file/video"
|
|
"github.com/stashapp/stash/pkg/fsutil"
|
|
"github.com/stashapp/stash/pkg/models"
|
|
"github.com/stashapp/stash/pkg/models/paths"
|
|
)
|
|
|
|
// FileDeleter is an extension of file.Deleter that handles deletion of scene files.
|
|
type FileDeleter struct {
|
|
*file.Deleter
|
|
|
|
FileNamingAlgo models.HashAlgorithm
|
|
Paths *paths.Paths
|
|
}
|
|
|
|
// MarkGeneratedFiles marks for deletion the generated files for the provided scene.
|
|
func (d *FileDeleter) MarkGeneratedFiles(scene *models.Scene) error {
|
|
sceneHash := scene.GetHash(d.FileNamingAlgo)
|
|
|
|
if sceneHash == "" {
|
|
return nil
|
|
}
|
|
|
|
markersFolder := filepath.Join(d.Paths.Generated.Markers, sceneHash)
|
|
|
|
exists, _ := fsutil.FileExists(markersFolder)
|
|
if exists {
|
|
if err := d.Dirs([]string{markersFolder}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var files []string
|
|
|
|
thumbPath := d.Paths.Scene.GetThumbnailScreenshotPath(sceneHash)
|
|
exists, _ = fsutil.FileExists(thumbPath)
|
|
if exists {
|
|
files = append(files, thumbPath)
|
|
}
|
|
|
|
normalPath := d.Paths.Scene.GetScreenshotPath(sceneHash)
|
|
exists, _ = fsutil.FileExists(normalPath)
|
|
if exists {
|
|
files = append(files, normalPath)
|
|
}
|
|
|
|
streamPreviewPath := d.Paths.Scene.GetVideoPreviewPath(sceneHash)
|
|
exists, _ = fsutil.FileExists(streamPreviewPath)
|
|
if exists {
|
|
files = append(files, streamPreviewPath)
|
|
}
|
|
|
|
streamPreviewImagePath := d.Paths.Scene.GetWebpPreviewPath(sceneHash)
|
|
exists, _ = fsutil.FileExists(streamPreviewImagePath)
|
|
if exists {
|
|
files = append(files, streamPreviewImagePath)
|
|
}
|
|
|
|
transcodePath := d.Paths.Scene.GetTranscodePath(sceneHash)
|
|
exists, _ = fsutil.FileExists(transcodePath)
|
|
if exists {
|
|
files = append(files, transcodePath)
|
|
}
|
|
|
|
spritePath := d.Paths.Scene.GetSpriteImageFilePath(sceneHash)
|
|
exists, _ = fsutil.FileExists(spritePath)
|
|
if exists {
|
|
files = append(files, spritePath)
|
|
}
|
|
|
|
vttPath := d.Paths.Scene.GetSpriteVttFilePath(sceneHash)
|
|
exists, _ = fsutil.FileExists(vttPath)
|
|
if exists {
|
|
files = append(files, vttPath)
|
|
}
|
|
|
|
heatmapPath := d.Paths.Scene.GetInteractiveHeatmapPath(sceneHash)
|
|
exists, _ = fsutil.FileExists(heatmapPath)
|
|
if exists {
|
|
files = append(files, heatmapPath)
|
|
}
|
|
|
|
return d.Files(files)
|
|
}
|
|
|
|
// MarkMarkerFiles deletes generated files for a scene marker with the
|
|
// provided scene and timestamp.
|
|
func (d *FileDeleter) MarkMarkerFiles(scene *models.Scene, seconds int) error {
|
|
videoPath := d.Paths.SceneMarkers.GetVideoPreviewPath(scene.GetHash(d.FileNamingAlgo), seconds)
|
|
imagePath := d.Paths.SceneMarkers.GetWebpPreviewPath(scene.GetHash(d.FileNamingAlgo), seconds)
|
|
screenshotPath := d.Paths.SceneMarkers.GetScreenshotPath(scene.GetHash(d.FileNamingAlgo), seconds)
|
|
|
|
var files []string
|
|
|
|
exists, _ := fsutil.FileExists(videoPath)
|
|
if exists {
|
|
files = append(files, videoPath)
|
|
}
|
|
|
|
exists, _ = fsutil.FileExists(imagePath)
|
|
if exists {
|
|
files = append(files, imagePath)
|
|
}
|
|
|
|
exists, _ = fsutil.FileExists(screenshotPath)
|
|
if exists {
|
|
files = append(files, screenshotPath)
|
|
}
|
|
|
|
return d.Files(files)
|
|
}
|
|
|
|
type Destroyer interface {
|
|
Destroy(ctx context.Context, id int) error
|
|
}
|
|
|
|
type MarkerDestroyer interface {
|
|
FindBySceneID(ctx context.Context, sceneID int) ([]*models.SceneMarker, error)
|
|
Destroy(ctx context.Context, id int) error
|
|
}
|
|
|
|
// Destroy deletes a scene and its associated relationships from the
|
|
// database.
|
|
func (s *Service) Destroy(ctx context.Context, scene *models.Scene, fileDeleter *FileDeleter, deleteGenerated, deleteFile bool) error {
|
|
mqb := s.MarkerDestroyer
|
|
markers, err := mqb.FindBySceneID(ctx, scene.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, m := range markers {
|
|
if err := DestroyMarker(ctx, scene, m, mqb, fileDeleter); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// TODO - we currently destroy associated files so that they will be rescanned.
|
|
// A better way would be to keep the file entries in the database, and recreate
|
|
// associated objects during the scan process if there are none already.
|
|
|
|
if err := s.destroyFiles(ctx, scene, fileDeleter, deleteFile); err != nil {
|
|
return err
|
|
}
|
|
|
|
if deleteGenerated {
|
|
if err := fileDeleter.MarkGeneratedFiles(scene); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := s.Repository.Destroy(ctx, scene.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) destroyFiles(ctx context.Context, scene *models.Scene, fileDeleter *FileDeleter, deleteFile bool) error {
|
|
for _, f := range scene.Files {
|
|
// only delete files where there is no other associated scene
|
|
otherScenes, err := s.Repository.FindByFileID(ctx, f.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(otherScenes) > 1 {
|
|
// other scenes associated, don't remove
|
|
continue
|
|
}
|
|
|
|
if err := file.Destroy(ctx, s.File, f, fileDeleter.Deleter, deleteFile); err != nil {
|
|
return err
|
|
}
|
|
|
|
// don't delete files in zip archives
|
|
if deleteFile && f.ZipFileID == nil {
|
|
funscriptPath := video.GetFunscriptPath(f.Path)
|
|
funscriptExists, _ := fsutil.FileExists(funscriptPath)
|
|
if funscriptExists {
|
|
if err := fileDeleter.Files([]string{funscriptPath}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DestroyMarker deletes the scene marker from the database and returns a
|
|
// function that removes the generated files, to be executed after the
|
|
// transaction is successfully committed.
|
|
func DestroyMarker(ctx context.Context, scene *models.Scene, sceneMarker *models.SceneMarker, qb MarkerDestroyer, fileDeleter *FileDeleter) error {
|
|
if err := qb.Destroy(ctx, sceneMarker.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
// delete the preview for the marker
|
|
seconds := int(sceneMarker.Seconds)
|
|
return fileDeleter.MarkMarkerFiles(scene, seconds)
|
|
}
|