From 3e14ba54b8093e2ee9afc5298333309903a7d48b Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 8 Nov 2024 14:01:51 +0000 Subject: [PATCH] gofile: fix server side copying over existing object This was creating a duplicate. --- backend/gofile/gofile.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/backend/gofile/gofile.go b/backend/gofile/gofile.go index 8f7b98b89..ca4091046 100644 --- a/backend/gofile/gofile.go +++ b/backend/gofile/gofile.go @@ -1214,7 +1214,7 @@ func (f *Fs) copyTo(ctx context.Context, srcID, srcLeaf, dstLeaf, dstDirectoryID // Will only be called if src.Fs().Name() == f.Name() // // If it isn't possible then return fs.ErrorCantCopy -func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, error) { +func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Object, err error) { srcObj, ok := src.(*Object) if !ok { fs.Debugf(src, "Can't copy - not same remote type") @@ -1228,6 +1228,19 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, return nil, fmt.Errorf("can't copy %q -> %q as are same name", srcPath, dstPath) } + // Find existing object + existingObj, err := f.NewObject(ctx, remote) + if err == nil { + defer func() { + // Don't remove existing object if returning an error + if err != nil { + return + } + fs.Debugf(existingObj, "Server side copy: removing existing object after successful copy") + err = existingObj.Remove(ctx) + }() + } + // Create temporary object dstObj, dstLeaf, dstDirectoryID, err := f.createObject(ctx, remote, srcObj.modTime, srcObj.size) if err != nil {