From 1eb50fab3379171deb0f6bc8e043e379fb8ec373 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Thu, 24 Oct 2019 18:18:53 +1100 Subject: [PATCH] Unify common code between delete and clean --- pkg/api/resolver_mutation_scene.go | 124 ++------------------------- pkg/manager/scene.go | 133 +++++++++++++++++++++++++++++ pkg/manager/task_clean.go | 114 +++---------------------- 3 files changed, 149 insertions(+), 222 deletions(-) create mode 100644 pkg/manager/scene.go diff --git a/pkg/api/resolver_mutation_scene.go b/pkg/api/resolver_mutation_scene.go index 2a90cd59e..66ccf4af6 100644 --- a/pkg/api/resolver_mutation_scene.go +++ b/pkg/api/resolver_mutation_scene.go @@ -3,16 +3,12 @@ package api import ( "context" "database/sql" - "os" - "path/filepath" "strconv" "time" "github.com/stashapp/stash/pkg/database" - "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/manager" "github.com/stashapp/stash/pkg/models" - "github.com/stashapp/stash/pkg/utils" ) func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUpdateInput) (*models.Scene, error) { @@ -125,144 +121,36 @@ func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUp func (r *mutationResolver) SceneDestroy(ctx context.Context, input models.SceneDestroyInput) (bool, error) { qb := models.NewSceneQueryBuilder() - jqb := models.NewJoinsQueryBuilder() tx := database.DB.MustBeginTx(ctx, nil) sceneID, _ := strconv.Atoi(input.ID) - scene, err := qb.Find(sceneID) + err = manager.DestroyScene(sceneID, tx) + if err != nil { - _ = tx.Rollback() + tx.Rollback() return false, err } - if err := jqb.DestroyScenesTags(sceneID, tx); err != nil { - _ = tx.Rollback() - return false, err - } - - if err := jqb.DestroyPerformersScenes(sceneID, tx); err != nil { - _ = tx.Rollback() - return false, err - } - - if err := jqb.DestroyScenesMarkers(sceneID, tx); err != nil { - _ = tx.Rollback() - return false, err - } - - if err := jqb.DestroyScenesGalleries(sceneID, tx); err != nil { - _ = tx.Rollback() - return false, err - } - - if err := qb.Destroy(input.ID, tx); err != nil { - _ = tx.Rollback() - return false, err - } if err := tx.Commit(); err != nil { return false, err } - + // if delete generated is true, then delete the generated files // for the scene if input.DeleteGenerated != nil && *input.DeleteGenerated { - deleteGeneratedSceneFiles(scene) + manager.DeleteGeneratedSceneFiles(scene) } // if delete file is true, then delete the file as well // if it fails, just log a message if input.DeleteFile != nil && *input.DeleteFile { - // kill any running encoders - manager.KillRunningStreams(scene.Path) - - err = os.Remove(scene.Path) - if err != nil { - logger.Warnf("Could not delete file %s: %s", scene.Path, err.Error()) - } + manager.DeleteSceneFile(scene) } return true, nil } -func deleteGeneratedSceneFiles(scene *models.Scene) { - markersFolder := filepath.Join(manager.GetInstance().Paths.Generated.Markers, scene.Checksum) - - exists, _ := utils.FileExists(markersFolder) - if exists { - err := os.RemoveAll(markersFolder) - if err != nil { - logger.Warnf("Could not delete file %s: %s", scene.Path, err.Error()) - } - } - - thumbPath := manager.GetInstance().Paths.Scene.GetThumbnailScreenshotPath(scene.Checksum) - exists, _ = utils.FileExists(thumbPath) - if exists { - err := os.Remove(thumbPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", thumbPath, err.Error()) - } - } - - normalPath := manager.GetInstance().Paths.Scene.GetScreenshotPath(scene.Checksum) - exists, _ = utils.FileExists(normalPath) - if exists { - err := os.Remove(normalPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", normalPath, err.Error()) - } - } - - streamPreviewPath := manager.GetInstance().Paths.Scene.GetStreamPreviewPath(scene.Checksum) - exists, _ = utils.FileExists(streamPreviewPath) - if exists { - err := os.Remove(streamPreviewPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", streamPreviewPath, err.Error()) - } - } - - streamPreviewImagePath := manager.GetInstance().Paths.Scene.GetStreamPreviewImagePath(scene.Checksum) - exists, _ = utils.FileExists(streamPreviewImagePath) - if exists { - err := os.Remove(streamPreviewImagePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", streamPreviewImagePath, err.Error()) - } - } - - transcodePath := manager.GetInstance().Paths.Scene.GetTranscodePath(scene.Checksum) - exists, _ = utils.FileExists(transcodePath) - if exists { - // kill any running streams - manager.KillRunningStreams(transcodePath) - - err := os.Remove(transcodePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", transcodePath, err.Error()) - } - } - - spritePath := manager.GetInstance().Paths.Scene.GetSpriteImageFilePath(scene.Checksum) - exists, _ = utils.FileExists(spritePath) - if exists { - err := os.Remove(spritePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", spritePath, err.Error()) - } - } - - vttPath := manager.GetInstance().Paths.Scene.GetSpriteVttFilePath(scene.Checksum) - exists, _ = utils.FileExists(vttPath) - if exists { - err := os.Remove(vttPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", vttPath, err.Error()) - } - } -} - func (r *mutationResolver) SceneMarkerCreate(ctx context.Context, input models.SceneMarkerCreateInput) (*models.SceneMarker, error) { primaryTagID, _ := strconv.Atoi(input.PrimaryTagID) sceneID, _ := strconv.Atoi(input.SceneID) diff --git a/pkg/manager/scene.go b/pkg/manager/scene.go new file mode 100644 index 000000000..e222f50c4 --- /dev/null +++ b/pkg/manager/scene.go @@ -0,0 +1,133 @@ +package manager + +import ( + "os" + "path/filepath" + "strconv" + + "github.com/jmoiron/sqlx" + + "github.com/stashapp/stash/pkg/logger" + "github.com/stashapp/stash/pkg/models" + "github.com/stashapp/stash/pkg/utils" +) + +func DestroyScene(sceneID int, tx *sqlx.Tx) (error) { + qb := models.NewSceneQueryBuilder() + jqb := models.NewJoinsQueryBuilder() + + _, err := qb.Find(sceneID) + if err != nil { + return err + } + + if err := jqb.DestroyScenesTags(sceneID, tx); err != nil { + return err + } + + if err := jqb.DestroyPerformersScenes(sceneID, tx); err != nil { + return err + } + + if err := jqb.DestroyScenesMarkers(sceneID, tx); err != nil { + return err + } + + if err := jqb.DestroyScenesGalleries(sceneID, tx); err != nil { + return err + } + + if err := qb.Destroy(strconv.Itoa(sceneID), tx); err != nil { + return err + } + + return nil +} + +func DeleteGeneratedSceneFiles(scene *models.Scene) { + markersFolder := filepath.Join(GetInstance().Paths.Generated.Markers, scene.Checksum) + + exists, _ := utils.FileExists(markersFolder) + if exists { + err := os.RemoveAll(markersFolder) + if err != nil { + logger.Warnf("Could not delete file %s: %s", scene.Path, err.Error()) + } + } + + thumbPath := GetInstance().Paths.Scene.GetThumbnailScreenshotPath(scene.Checksum) + exists, _ = utils.FileExists(thumbPath) + if exists { + err := os.Remove(thumbPath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", thumbPath, err.Error()) + } + } + + normalPath := GetInstance().Paths.Scene.GetScreenshotPath(scene.Checksum) + exists, _ = utils.FileExists(normalPath) + if exists { + err := os.Remove(normalPath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", normalPath, err.Error()) + } + } + + streamPreviewPath := GetInstance().Paths.Scene.GetStreamPreviewPath(scene.Checksum) + exists, _ = utils.FileExists(streamPreviewPath) + if exists { + err := os.Remove(streamPreviewPath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", streamPreviewPath, err.Error()) + } + } + + streamPreviewImagePath := GetInstance().Paths.Scene.GetStreamPreviewImagePath(scene.Checksum) + exists, _ = utils.FileExists(streamPreviewImagePath) + if exists { + err := os.Remove(streamPreviewImagePath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", streamPreviewImagePath, err.Error()) + } + } + + transcodePath := GetInstance().Paths.Scene.GetTranscodePath(scene.Checksum) + exists, _ = utils.FileExists(transcodePath) + if exists { + // kill any running streams + KillRunningStreams(transcodePath) + + err := os.Remove(transcodePath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", transcodePath, err.Error()) + } + } + + spritePath := GetInstance().Paths.Scene.GetSpriteImageFilePath(scene.Checksum) + exists, _ = utils.FileExists(spritePath) + if exists { + err := os.Remove(spritePath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", spritePath, err.Error()) + } + } + + vttPath := GetInstance().Paths.Scene.GetSpriteVttFilePath(scene.Checksum) + exists, _ = utils.FileExists(vttPath) + if exists { + err := os.Remove(vttPath) + if err != nil { + logger.Warnf("Could not delete file %s: %s", vttPath, err.Error()) + } + } +} + +func DeleteSceneFile(scene *models.Scene) { + // kill any running encoders + KillRunningStreams(scene.Path) + + err := os.Remove(scene.Path) + if err != nil { + logger.Warnf("Could not delete file %s: %s", scene.Path, err.Error()) + } +} \ No newline at end of file diff --git a/pkg/manager/task_clean.go b/pkg/manager/task_clean.go index 594f674ac..1f4c0ae9f 100644 --- a/pkg/manager/task_clean.go +++ b/pkg/manager/task_clean.go @@ -5,10 +5,7 @@ import ( "github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/models" - "github.com/stashapp/stash/pkg/utils" "os" - "path/filepath" - "strconv" "sync" ) @@ -30,114 +27,23 @@ func (t *CleanTask) Start(wg *sync.WaitGroup) { func (t *CleanTask) deleteScene(sceneID int) { ctx := context.TODO() qb := models.NewSceneQueryBuilder() - jqb := models.NewJoinsQueryBuilder() tx := database.DB.MustBeginTx(ctx, nil) - strSceneID := strconv.Itoa(sceneID) - defer tx.Commit() - //check and make sure it still exists. scene is also used to delete generated files scene, err := qb.Find(sceneID) + err = DestroyScene(sceneID, tx) + if err != nil { - _ = tx.Rollback() + logger.Infof("Error deleting scene from database: %s", err.Error()) + tx.Rollback() + return } - if err := jqb.DestroyScenesTags(sceneID, tx); err != nil { - _ = tx.Rollback() - } - - if err := jqb.DestroyPerformersScenes(sceneID, tx); err != nil { - _ = tx.Rollback() - } - - if err := jqb.DestroyScenesMarkers(sceneID, tx); err != nil { - _ = tx.Rollback() - } - - if err := jqb.DestroyScenesGalleries(sceneID, tx); err != nil { - _ = tx.Rollback() - } - - if err := qb.Destroy(strSceneID, tx); err != nil { - _ = tx.Rollback() - } - - t.deleteGeneratedSceneFiles(scene) -} - - -func (t *CleanTask) deleteGeneratedSceneFiles(scene *models.Scene) { - markersFolder := filepath.Join(instance.Paths.Generated.Markers, scene.Checksum) - - exists, _ := utils.FileExists(markersFolder) - if exists { - err := os.RemoveAll(markersFolder) - if err != nil { - logger.Warnf("Could not delete file %s: %s", scene.Path, err.Error()) - } - } - - thumbPath := instance.Paths.Scene.GetThumbnailScreenshotPath(scene.Checksum) - exists, _ = utils.FileExists(thumbPath) - if exists { - err := os.Remove(thumbPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", thumbPath, err.Error()) - } - } - - screenshotPath := instance.Paths.Scene.GetScreenshotPath(scene.Checksum) - exists, _ = utils.FileExists(screenshotPath) - if exists { - err := os.Remove(screenshotPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", screenshotPath, err.Error()) - } - } - - streamPreviewPath := instance.Paths.Scene.GetStreamPreviewPath(scene.Checksum) - exists, _ = utils.FileExists(streamPreviewPath) - if exists { - err := os.Remove(streamPreviewPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", streamPreviewPath, err.Error()) - } - } - - streamPreviewImagePath := instance.Paths.Scene.GetStreamPreviewImagePath(scene.Checksum) - exists, _ = utils.FileExists(streamPreviewImagePath) - if exists { - err := os.Remove(streamPreviewImagePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", streamPreviewImagePath, err.Error()) - } - } - - transcodePath := instance.Paths.Scene.GetTranscodePath(scene.Checksum) - exists, _ = utils.FileExists(transcodePath) - if exists { - err := os.Remove(transcodePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", transcodePath, err.Error()) - } - } - - spritePath := instance.Paths.Scene.GetSpriteImageFilePath(scene.Checksum) - exists, _ = utils.FileExists(spritePath) - if exists { - err := os.Remove(spritePath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", spritePath, err.Error()) - } - } - - vttPath := instance.Paths.Scene.GetSpriteVttFilePath(scene.Checksum) - exists, _ = utils.FileExists(vttPath) - if exists { - err := os.Remove(vttPath) - if err != nil { - logger.Warnf("Could not delete file %s: %s", vttPath, err.Error()) - } + if err := tx.Commit(); err != nil { + logger.Infof("Error deleting scene from database: %s", err.Error()) + return } + + DeleteGeneratedSceneFiles(scene) } func (t *CleanTask) fileExists(filename string) bool {