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-07-13 06:30:54 +00:00
|
|
|
"github.com/stashapp/stash/pkg/file/video"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/fsutil"
|
2023-08-07 23:34:05 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
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 {
|
2022-07-13 06:30:54 +00:00
|
|
|
*file.Deleter
|
2021-11-29 03:08:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy deletes a scene and its associated relationships from the
|
|
|
|
// database.
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s *Service) Destroy(ctx context.Context, scene *models.Scene, fileDeleter *FileDeleter, deleteGenerated, deleteFile bool) error {
|
2022-11-14 05:35:09 +00:00
|
|
|
mqb := s.MarkerRepository
|
2022-05-19 07:49:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:51:59 +00:00
|
|
|
if deleteFile {
|
|
|
|
if err := s.deleteFiles(ctx, scene, fileDeleter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-29 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if deleteGenerated {
|
|
|
|
if err := fileDeleter.MarkGeneratedFiles(scene); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
if err := s.Repository.Destroy(ctx, scene.ID); err != nil {
|
2021-11-29 03:08:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:51:59 +00:00
|
|
|
// deleteFiles deletes files from the database and file system
|
|
|
|
func (s *Service) deleteFiles(ctx context.Context, scene *models.Scene, fileDeleter *FileDeleter) error {
|
2022-09-02 01:18:37 +00:00
|
|
|
if err := scene.LoadFiles(ctx, s.Repository); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
for _, f := range scene.Files.List() {
|
2022-07-13 06:30:54 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:51:59 +00:00
|
|
|
const deleteFile = true
|
2023-08-07 23:34:05 +00:00
|
|
|
logger.Info("Deleting scene file: ", f.Path)
|
2022-07-13 06:30:54 +00:00
|
|
|
if err := file.Destroy(ctx, s.File, f, fileDeleter.Deleter, deleteFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't delete files in zip archives
|
2022-07-18 00:51:59 +00:00
|
|
|
if f.ZipFileID == nil {
|
2022-07-13 06:30:54 +00:00
|
|
|
funscriptPath := video.GetFunscriptPath(f.Path)
|
|
|
|
funscriptExists, _ := fsutil.FileExists(funscriptPath)
|
|
|
|
if funscriptExists {
|
|
|
|
if err := fileDeleter.Files([]string{funscriptPath}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 03:08:32 +00:00
|
|
|
// 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.
|
2023-09-01 00:39:29 +00:00
|
|
|
func DestroyMarker(ctx context.Context, scene *models.Scene, sceneMarker *models.SceneMarker, qb models.SceneMarkerDestroyer, fileDeleter *FileDeleter) error {
|
2022-05-19 07:49:32 +00:00
|
|
|
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)
|
|
|
|
}
|