From 6fdd7149c1b68223c42cf5fe9994a4b78fa3d9bc Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 28 Feb 2020 16:45:35 +0000 Subject: [PATCH] drive: don't overwrite the description on sever side copy See: https://forum.rclone.org/t/is-there-a-way-to-sync-while-keeping-file-description-on-the-destination/14609 --- backend/drive/drive.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index feee84a69..928e089aa 100644 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -642,17 +642,21 @@ func containsString(slice []string, s string) bool { return false } -// getRootID returns the canonical ID for the "root" ID -func (f *Fs) getRootID() (string, error) { - var info *drive.File - var err error +// getFile returns drive.File for the ID passed and fields passed in +func (f *Fs) getFile(ID string, fields googleapi.Field) (info *drive.File, err error) { err = f.pacer.CallNoRetry(func() (bool, error) { - info, err = f.svc.Files.Get("root"). - Fields("id"). + info, err = f.svc.Files.Get(ID). + Fields(fields). SupportsAllDrives(true). Do() return f.shouldRetry(err) }) + return info, err +} + +// getRootID returns the canonical ID for the "root" ID +func (f *Fs) getRootID() (string, error) { + info, err := f.getFile("root", "id") if err != nil { return "", errors.Wrap(err, "couldn't find root directory ID") } @@ -2042,11 +2046,13 @@ func (f *Fs) Precision() time.Duration { func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, error) { var srcObj *baseObject ext := "" + readDescription := false switch src := src.(type) { case *Object: srcObj = &src.baseObject case *documentObject: srcObj, ext = &src.baseObject, src.ext() + readDescription = true case *linkObject: srcObj, ext = &src.baseObject, src.ext() default: @@ -2070,6 +2076,19 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, return nil, err } + if readDescription { + // preserve the description on copy for docs + info, err := f.getFile(srcObj.id, "description") + if err != nil { + return nil, errors.Wrap(err, "failed to read description for Google Doc") + } + createInfo.Description = info.Description + } else { + // don't overwrite the description on copy for files + // this should work for docs but it doesn't - it is probably a bug in Google Drive + createInfo.Description = "" + } + var info *drive.File err = f.pacer.Call(func() (bool, error) { info, err = f.svc.Files.Copy(srcObj.id, createInfo).