2021-11-29 03:08:32 +00:00
|
|
|
package scene
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-11-29 03:08:32 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/file"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/fsutil"
|
2021-11-29 03:08:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/paths"
|
2021-11-29 03:08:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ := fsutil.FileExists(markersFolder)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
if err := d.Dirs([]string{markersFolder}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
|
|
|
|
thumbPath := d.Paths.Scene.GetThumbnailScreenshotPath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(thumbPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, thumbPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
normalPath := d.Paths.Scene.GetScreenshotPath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(normalPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, normalPath)
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
streamPreviewPath := d.Paths.Scene.GetVideoPreviewPath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(streamPreviewPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, streamPreviewPath)
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
streamPreviewImagePath := d.Paths.Scene.GetWebpPreviewPath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(streamPreviewImagePath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, streamPreviewImagePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
transcodePath := d.Paths.Scene.GetTranscodePath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(transcodePath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, transcodePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
spritePath := d.Paths.Scene.GetSpriteImageFilePath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(spritePath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, spritePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
vttPath := d.Paths.Scene.GetSpriteVttFilePath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(vttPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, vttPath)
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:41:07 +00:00
|
|
|
heatmapPath := d.Paths.Scene.GetInteractiveHeatmapPath(sceneHash)
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(heatmapPath)
|
2021-12-13 02:41:07 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, heatmapPath)
|
|
|
|
}
|
|
|
|
|
2021-11-29 03:08:32 +00:00
|
|
|
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 {
|
2022-04-18 00:50:10 +00:00
|
|
|
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)
|
2021-11-29 03:08:32 +00:00
|
|
|
|
|
|
|
var files []string
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ := fsutil.FileExists(videoPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, videoPath)
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(imagePath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, imagePath)
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ = fsutil.FileExists(screenshotPath)
|
2021-11-29 03:08:32 +00:00
|
|
|
if exists {
|
|
|
|
files = append(files, screenshotPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Files(files)
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-29 03:08:32 +00:00
|
|
|
// Destroy deletes a scene and its associated relationships from the
|
|
|
|
// database.
|
2022-05-19 07:49:32 +00:00
|
|
|
func Destroy(ctx context.Context, scene *models.Scene, qb Destroyer, mqb MarkerDestroyer, fileDeleter *FileDeleter, deleteGenerated, deleteFile bool) error {
|
|
|
|
markers, err := mqb.FindBySceneID(ctx, scene.ID)
|
2021-11-29 03:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range markers {
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := DestroyMarker(ctx, scene, m, mqb, fileDeleter); err != nil {
|
2021-11-29 03:08:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleteFile {
|
|
|
|
if err := fileDeleter.Files([]string{scene.Path}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-03 00:58:48 +00:00
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
funscriptPath := GetFunscriptPath(scene.Path)
|
|
|
|
funscriptExists, _ := fsutil.FileExists(funscriptPath)
|
2022-02-03 00:58:48 +00:00
|
|
|
if funscriptExists {
|
|
|
|
if err := fileDeleter.Files([]string{funscriptPath}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if deleteGenerated {
|
|
|
|
if err := fileDeleter.MarkGeneratedFiles(scene); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.Destroy(ctx, scene.ID); err != nil {
|
2021-11-29 03:08:32 +00:00
|
|
|
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.
|
2022-05-19 07:49:32 +00:00
|
|
|
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 {
|
2021-11-29 03:08:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the preview for the marker
|
|
|
|
seconds := int(sceneMarker.Seconds)
|
|
|
|
return fileDeleter.MarkMarkerFiles(scene, seconds)
|
|
|
|
}
|