Move zip files while moving folders (#4374)

This commit is contained in:
WithoutPants
2024-01-16 14:48:28 +11:00
committed by GitHub
parent 75099b38a8
commit 403f7c54ef
3 changed files with 64 additions and 41 deletions

View File

@@ -7,7 +7,6 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/stashapp/stash/pkg/logger"
@@ -88,44 +87,10 @@ func (m *Mover) Move(ctx context.Context, f models.File, folder *models.Folder,
return fmt.Errorf("file %s already exists", newPath)
}
if err := TransferZipFolderHierarchy(ctx, m.Folders, fBase.ID, oldPath, newPath); err != nil {
if err := transferZipHierarchy(ctx, m.Folders, m.Files, fBase.ID, oldPath, newPath); err != nil {
return fmt.Errorf("moving folder hierarchy for file %s: %w", fBase.Path, err)
}
// move contained files if file is a zip file
zipFiles, err := m.Files.FindByZipFileID(ctx, fBase.ID)
if err != nil {
return fmt.Errorf("finding contained files in file %s: %w", fBase.Path, err)
}
for _, zf := range zipFiles {
zfBase := zf.Base()
oldZfPath := zfBase.Path
oldZfDir := filepath.Dir(oldZfPath)
// sanity check - ignore files which aren't under oldPath
if !strings.HasPrefix(oldZfPath, oldPath) {
continue
}
relZfDir, err := filepath.Rel(oldPath, oldZfDir)
if err != nil {
return fmt.Errorf("moving contained file %s: %w", zfBase.ID, err)
}
newZfDir := filepath.Join(newPath, relZfDir)
// folder should have been created by moveZipFolderHierarchy
newZfFolder, err := GetOrCreateFolderHierarchy(ctx, m.Folders, newZfDir)
if err != nil {
return fmt.Errorf("getting or creating folder hierarchy: %w", err)
}
// update file parent folder
zfBase.ParentFolderID = newZfFolder.ID
if err := m.Files.Update(ctx, zf); err != nil {
return fmt.Errorf("updating file %s: %w", oldZfPath, err)
}
}
fBase.ParentFolderID = folder.ID
fBase.Basename = basename
fBase.UpdatedAt = time.Now()