From dd71f5d968e36fb2be65bf73ec9cc518cb66ee17 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 6 Aug 2022 16:30:45 +0100 Subject: [PATCH] fs: move operations.NewOverrideRemote to fs.NewOverrideRemote --- backend/combine/combine.go | 2 +- backend/googlephotos/googlephotos_test.go | 5 +- fs/operations/operations.go | 70 +---------------------- fs/override.go | 68 ++++++++++++++++++++++ fs/override_test.go | 4 ++ 5 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 fs/override.go create mode 100644 fs/override_test.go diff --git a/backend/combine/combine.go b/backend/combine/combine.go index 34644a968..63fb4635e 100644 --- a/backend/combine/combine.go +++ b/backend/combine/combine.go @@ -631,7 +631,7 @@ func (f *Fs) put(ctx context.Context, in io.Reader, src fs.ObjectInfo, stream bo if err != nil { return nil, err } - uSrc := operations.NewOverrideRemote(src, uRemote) + uSrc := fs.NewOverrideRemote(src, uRemote) var o fs.Object if stream { o, err = u.f.Features().PutStream(ctx, in, uSrc, options...) diff --git a/backend/googlephotos/googlephotos_test.go b/backend/googlephotos/googlephotos_test.go index 3cc3a2706..cd1e0943f 100644 --- a/backend/googlephotos/googlephotos_test.go +++ b/backend/googlephotos/googlephotos_test.go @@ -12,7 +12,6 @@ import ( _ "github.com/rclone/rclone/backend/local" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/hash" - "github.com/rclone/rclone/fs/operations" "github.com/rclone/rclone/fstest" "github.com/rclone/rclone/lib/random" "github.com/stretchr/testify/assert" @@ -56,7 +55,7 @@ func TestIntegration(t *testing.T) { require.NoError(t, err) in, err := srcObj.Open(ctx) require.NoError(t, err) - dstObj, err := f.Put(ctx, in, operations.NewOverrideRemote(srcObj, remote)) + dstObj, err := f.Put(ctx, in, fs.NewOverrideRemote(srcObj, remote)) require.NoError(t, err) assert.Equal(t, remote, dstObj.Remote()) _ = in.Close() @@ -221,7 +220,7 @@ func TestIntegration(t *testing.T) { require.NoError(t, err) in, err := srcObj.Open(ctx) require.NoError(t, err) - dstObj, err := f.Put(ctx, in, operations.NewOverrideRemote(srcObj, remote)) + dstObj, err := f.Put(ctx, in, fs.NewOverrideRemote(srcObj, remote)) require.NoError(t, err) assert.Equal(t, remote, dstObj.Remote()) _ = in.Close() diff --git a/fs/operations/operations.go b/fs/operations/operations.go index 4b4afd041..351066332 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -297,74 +297,6 @@ func removeFailedCopy(ctx context.Context, dst fs.Object) bool { return true } -// OverrideRemote is a wrapper to override the Remote for an -// ObjectInfo -type OverrideRemote struct { - fs.ObjectInfo - remote string -} - -// NewOverrideRemote returns an OverrideRemoteObject which will -// return the remote specified -func NewOverrideRemote(oi fs.ObjectInfo, remote string) *OverrideRemote { - return &OverrideRemote{ - ObjectInfo: oi, - remote: remote, - } -} - -// Remote returns the overridden remote name -func (o *OverrideRemote) Remote() string { - return o.remote -} - -// MimeType returns the mime type of the underlying object or "" if it -// can't be worked out -func (o *OverrideRemote) MimeType(ctx context.Context) string { - if do, ok := o.ObjectInfo.(fs.MimeTyper); ok { - return do.MimeType(ctx) - } - return "" -} - -// ID returns the ID of the Object if known, or "" if not -func (o *OverrideRemote) ID() string { - if do, ok := o.ObjectInfo.(fs.IDer); ok { - return do.ID() - } - return "" -} - -// UnWrap returns the Object that this Object is wrapping or nil if it -// isn't wrapping anything -func (o *OverrideRemote) UnWrap() fs.Object { - if o, ok := o.ObjectInfo.(fs.Object); ok { - return o - } - return nil -} - -// GetTier returns storage tier or class of the Object -func (o *OverrideRemote) GetTier() string { - if do, ok := o.ObjectInfo.(fs.GetTierer); ok { - return do.GetTier() - } - return "" -} - -// Metadata returns metadata for an object -// -// It should return nil if there is no Metadata -func (o *OverrideRemote) Metadata(ctx context.Context) (fs.Metadata, error) { - if do, ok := o.ObjectInfo.(fs.Metadataer); ok { - return do.Metadata(ctx) - } - return nil, nil -} - -// Check all optional interfaces satisfied -var _ fs.FullObjectInfo = (*OverrideRemote)(nil) - // CommonHash returns a single hash.Type and a HashOption with that // type which is in common between the two fs.Fs. func CommonHash(ctx context.Context, fa, fb fs.Info) (hash.Type, *fs.HashesOption) { @@ -491,7 +423,7 @@ func Copy(ctx context.Context, f fs.Fs, dst fs.Object, remote string, src fs.Obj var wrappedSrc fs.ObjectInfo = src // We try to pass the original object if possible if src.Remote() != remote { - wrappedSrc = NewOverrideRemote(src, remote) + wrappedSrc = fs.NewOverrideRemote(src, remote) } options := []fs.OpenOption{hashOption} for _, option := range ci.UploadHeaders { diff --git a/fs/override.go b/fs/override.go new file mode 100644 index 000000000..014acb8ae --- /dev/null +++ b/fs/override.go @@ -0,0 +1,68 @@ +package fs + +import "context" + +// OverrideRemote is a wrapper to override the Remote for an +// ObjectInfo +type OverrideRemote struct { + ObjectInfo + remote string +} + +// NewOverrideRemote returns an OverrideRemoteObject which will +// return the remote specified +func NewOverrideRemote(oi ObjectInfo, remote string) *OverrideRemote { + return &OverrideRemote{ + ObjectInfo: oi, + remote: remote, + } +} + +// Remote returns the overridden remote name +func (o *OverrideRemote) Remote() string { + return o.remote +} + +// MimeType returns the mime type of the underlying object or "" if it +// can't be worked out +func (o *OverrideRemote) MimeType(ctx context.Context) string { + if do, ok := o.ObjectInfo.(MimeTyper); ok { + return do.MimeType(ctx) + } + return "" +} + +// ID returns the ID of the Object if known, or "" if not +func (o *OverrideRemote) ID() string { + if do, ok := o.ObjectInfo.(IDer); ok { + return do.ID() + } + return "" +} + +// UnWrap returns the Object that this Object is wrapping or nil if it +// isn't wrapping anything +func (o *OverrideRemote) UnWrap() Object { + if o, ok := o.ObjectInfo.(Object); ok { + return o + } + return nil +} + +// GetTier returns storage tier or class of the Object +func (o *OverrideRemote) GetTier() string { + if do, ok := o.ObjectInfo.(GetTierer); ok { + return do.GetTier() + } + return "" +} + +// Metadata returns metadata for an object +// +// It should return nil if there is no Metadata +func (o *OverrideRemote) Metadata(ctx context.Context) (Metadata, error) { + if do, ok := o.ObjectInfo.(Metadataer); ok { + return do.Metadata(ctx) + } + return nil, nil +} diff --git a/fs/override_test.go b/fs/override_test.go new file mode 100644 index 000000000..f60dc7bdb --- /dev/null +++ b/fs/override_test.go @@ -0,0 +1,4 @@ +package fs + +// Check all optional interfaces satisfied +var _ FullObjectInfo = (*OverrideRemote)(nil)