Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interactive speed being lost when file is moved #4799

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions pkg/file/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,28 +895,35 @@ func (s *scanJob) handleRename(ctx context.Context, f models.File, fp []models.F
// assume does not exist, update existing file
// it's possible that there may be multiple missing files.
// just use the first one to rename.
// #4775 - using the new file instance means that any changes made to the existing
// file will be lost. Update the existing file instead.
other := missing[0]
otherBase := other.Base()
updated := other.Clone()
updatedBase := updated.Base()

fBase := f.Base()
fBaseCopy := *(f.Base())

logger.Infof("%s moved to %s. Updating path...", otherBase.Path, fBase.Path)
fBase.ID = otherBase.ID
fBase.CreatedAt = otherBase.CreatedAt
fBase.Fingerprints = otherBase.Fingerprints
oldPath := updatedBase.Path
newPath := fBaseCopy.Path

logger.Infof("%s moved to %s. Updating path...", oldPath, newPath)
fBaseCopy.ID = updatedBase.ID
fBaseCopy.CreatedAt = updatedBase.CreatedAt
fBaseCopy.Fingerprints = updatedBase.Fingerprints
*updatedBase = fBaseCopy

if err := s.withTxn(ctx, func(ctx context.Context) error {
if err := s.Repository.File.Update(ctx, f); err != nil {
return fmt.Errorf("updating file for rename %q: %w", fBase.Path, err)
if err := s.Repository.File.Update(ctx, updated); err != nil {
return fmt.Errorf("updating file for rename %q: %w", newPath, err)
}

if s.isZipFile(fBase.Basename) {
if err := transferZipHierarchy(ctx, s.Repository.Folder, s.Repository.File, fBase.ID, otherBase.Path, fBase.Path); err != nil {
return fmt.Errorf("moving zip hierarchy for renamed zip file %q: %w", fBase.Path, err)
if s.isZipFile(updatedBase.Basename) {
if err := transferZipHierarchy(ctx, s.Repository.Folder, s.Repository.File, updatedBase.ID, oldPath, newPath); err != nil {
return fmt.Errorf("moving zip hierarchy for renamed zip file %q: %w", newPath, err)
}
}

if err := s.fireHandlers(ctx, f, other); err != nil {
if err := s.fireHandlers(ctx, updated, other); err != nil {
return err
}

Expand All @@ -925,7 +932,7 @@ func (s *scanJob) handleRename(ctx context.Context, f models.File, fp []models.F
return nil, err
}

return f, nil
return updated, nil
}

func (s *scanJob) isHandlerRequired(ctx context.Context, f models.File) bool {
Expand Down
21 changes: 21 additions & 0 deletions pkg/models/model_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type File interface {
Base() *BaseFile
SetFingerprints(fp Fingerprints)
Open(fs FS) (io.ReadCloser, error)
Clone() File
}

// BaseFile represents a file in the file system.
Expand Down Expand Up @@ -173,6 +174,12 @@ func (f *BaseFile) Open(fs FS) (io.ReadCloser, error) {
return fs.Open(f.Path)
}

func (f *BaseFile) Clone() (ret File) {
clone := *f
ret = &clone
return
}

func (f *BaseFile) Info(fs FS) (fs.FileInfo, error) {
return f.info(fs, f.Path)
}
Expand Down Expand Up @@ -249,6 +256,13 @@ func (f ImageFile) GetFormat() string {
return f.Format
}

func (f ImageFile) Clone() (ret File) {
clone := f
clone.BaseFile = f.BaseFile.Clone().(*BaseFile)
ret = &clone
return
}

// VideoFile is an extension of BaseFile to represent video files.
type VideoFile struct {
*BaseFile
Expand Down Expand Up @@ -277,6 +291,13 @@ func (f VideoFile) GetFormat() string {
return f.Format
}

func (f VideoFile) Clone() (ret File) {
clone := f
clone.BaseFile = f.BaseFile.Clone().(*BaseFile)
ret = &clone
return
}

// #1572 - Inf and NaN values cause the JSON marshaller to fail
// Replace these values with 0 rather than erroring

Expand Down
Loading