Add move files external interface (#3557)

* Add moveFiles graphql mutation
* Move library resolution code into config
* Implement file moving
* Log if old file not removed in SafeMove
* Ensure extensions are consistent
* Don't allow overwriting existing files
This commit is contained in:
WithoutPants
2023-03-22 07:57:26 +11:00
committed by GitHub
parent f6387c1018
commit 09c724b8d5
12 changed files with 436 additions and 50 deletions

View File

@@ -2,7 +2,9 @@ package file
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"strconv"
"time"
)
@@ -30,9 +32,14 @@ func (f *Folder) Info(fs FS) (fs.FileInfo, error) {
return f.info(fs, f.Path)
}
// FolderPathFinder finds Folders by their path.
type FolderPathFinder interface {
FindByPath(ctx context.Context, path string) (*Folder, error)
}
// FolderGetter provides methods to find Folders.
type FolderGetter interface {
FindByPath(ctx context.Context, path string) (*Folder, error)
FolderPathFinder
FindByZipFileID(ctx context.Context, zipFileID ID) ([]*Folder, error)
FindAllInPaths(ctx context.Context, p []string, limit, offset int) ([]*Folder, error)
FindByParentFolderID(ctx context.Context, parentFolderID FolderID) ([]*Folder, error)
@@ -47,6 +54,11 @@ type FolderCreator interface {
Create(ctx context.Context, f *Folder) error
}
type FolderFinderCreator interface {
FolderPathFinder
FolderCreator
}
// FolderUpdater provides methods to update Folders.
type FolderUpdater interface {
Update(ctx context.Context, f *Folder) error
@@ -69,3 +81,39 @@ type FolderStore interface {
FolderUpdater
FolderDestroyer
}
// GetOrCreateFolderHierarchy gets the folder for the given path, or creates a folder hierarchy for the given path if one if no existing folder is found.
// Does not create any folders in the file system
func GetOrCreateFolderHierarchy(ctx context.Context, fc FolderFinderCreator, path string) (*Folder, error) {
// get or create folder hierarchy
folder, err := fc.FindByPath(ctx, path)
if err != nil {
return nil, err
}
if folder == nil {
parentPath := filepath.Dir(path)
parent, err := GetOrCreateFolderHierarchy(ctx, fc, parentPath)
if err != nil {
return nil, err
}
now := time.Now()
folder = &Folder{
Path: path,
ParentFolderID: &parent.ID,
DirEntry: DirEntry{
// leave mod time empty for now - it will be updated when the folder is scanned
},
CreatedAt: now,
UpdatedAt: now,
}
if err = fc.Create(ctx, folder); err != nil {
return nil, fmt.Errorf("creating folder %s: %w", path, err)
}
}
return folder, nil
}