From 4ab7e05e025005461756e67ae418b8fc953cb5c1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 4 Mar 2017 10:10:55 +0000 Subject: [PATCH] Fix MimeType propagation In fs.Copy, don't wrap objects if possible, and if not, then add a MimeType method into the wrapped object. --- fs/operations.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/fs/operations.go b/fs/operations.go index 785ca2b1b..5a8ab8799 100644 --- a/fs/operations.go +++ b/fs/operations.go @@ -211,7 +211,7 @@ func MimeType(o ObjectInfo) (mimeType string) { // Read the MimeType from the optional interface if available if do, ok := o.(MimeTyper); ok { mimeType = do.MimeType() - Debugf(o, "Read MimeType as %q", mimeType) + // Debugf(o, "Read MimeType as %q", mimeType) if mimeType != "" { return mimeType } @@ -246,6 +246,18 @@ func (o *overrideRemoteObject) Remote() string { return o.remote } +// MimeType returns the mime type of the underlying object or "" if it +// can't be worked out +func (o *overrideRemoteObject) MimeType() string { + if do, ok := o.Object.(MimeTyper); ok { + return do.MimeType() + } + return "" +} + +// Check interface is satisfied +var _ MimeTyper = (*overrideRemoteObject)(nil) + // Copy src object to dst or f if nil. If dst is nil then it uses // remote as the name of the new object. func Copy(f Fs, dst Object, remote string, src Object) (err error) { @@ -278,8 +290,11 @@ func Copy(f Fs, dst Object, remote string, src Object) (err error) { err = errors.Wrap(err, "failed to open source object") } else { in := NewAccount(in0, src).WithBuffer() // account and buffer the transfer - - wrappedSrc := &overrideRemoteObject{Object: src, remote: remote} + var wrappedSrc ObjectInfo = src + // We try to pass the original object if possible + if src.Remote() != remote { + wrappedSrc = &overrideRemoteObject{Object: src, remote: remote} + } if doUpdate { actionTaken = "Copied (replaced existing)" err = dst.Update(in, wrappedSrc)