Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

drive: Fix multiple files of same name being created

Nick Craig-Wood86b77f3ca8a6
diff --git a/drive/drive.go b/drive/drive.go
index 28406a10e..08bd0a758 100644
--- a/drive/drive.go
+++ b/drive/drive.go
@@ -663,45 +663,47 @@ func (f *FsDrive) Put(in io.Reader, remote string, modTime time.Time, size int64
return nil, fmt.Errorf("Couldn't find or make directory: %s", err)
}
+ // See if the file already exists
+ var info *drive.File
+ found, err := f.listAll(directoryId, leaf, false, true, func(item *drive.File) bool {
+ info = item
+ return true
+ })
+ if err != nil {
+ return nil, fmt.Errorf("Error finding file: %s", leaf, err)
+ }
+
// Guess the mime type
mimeType := mime.TypeByExtension(path.Ext(remote))
if mimeType == "" {
mimeType = "application/octet-stream"
}
+ modifiedDate := modTime.Format(time.RFC3339Nano)
- // Define the metadata for the file we are going to create.
- info := &drive.File{
- Title: leaf,
- Description: leaf,
- Parents: []*drive.ParentReference{{Id: directoryId}},
- MimeType: mimeType,
- }
-
- // FIXME can't set modified date on initial upload as no
- // .SetModifiedDate(). This agrees with the API docs, but not
- // with the comment on
- // https://developers.google.com/drive/v2/reference/files/insert
- //
- // modifiedDate datetime Last time this file was modified by
- // anyone (formatted RFC 3339 timestamp). This is only mutable
- // on update when the setModifiedDate parameter is set.
- // writable
- //
- // There is no setModifiedDate parameter though
-
- // Make the API request to upload infodata and file data.
- info, err = f.svc.Files.Insert(info).Media(in).Do()
+ if found {
+ // Modify metadata
+ info.ModifiedDate = modifiedDate
+ info.MimeType = mimeType
+
+ // Make the API request to upload metadata and file data.
+ info, err = f.svc.Files.Update(info.Id, info).SetModifiedDate(true).Media(in).Do()
+ } else {
+ // Define the metadata for the file we are going to create.
+ info = &drive.File{
+ Title: leaf,
+ Description: leaf,
+ Parents: []*drive.ParentReference{{Id: directoryId}},
+ MimeType: mimeType,
+ ModifiedDate: modifiedDate,
+ }
+
+ // Make the API request to upload metadata and file data.
+ info, err = f.svc.Files.Insert(info).Media(in).Do()
+ }
if err != nil {
return nil, fmt.Errorf("Upload failed: %s", err)
}
fs.setMetaData(info)
-
- // Set modified date
- info.ModifiedDate = modTime.Format(time.RFC3339Nano)
- _, err = f.svc.Files.Update(info.Id, info).SetModifiedDate(true).Do()
- if err != nil {
- return fs, fmt.Errorf("Failed to set mtime: %s", err)
- }
return fs, nil
}
diff --git a/notes.txt b/notes.txt
index 2c4334de5..21c3824fe 100644
--- a/notes.txt
+++ b/notes.txt
@@ -31,6 +31,7 @@ Ideas
* Google cloud storage: https://developers.google.com/storage/
* rsync over ssh
* dropbox: https://github.com/nickoneill/go-dropbox (no MD5s)
+ * control times sync (which is slow) with -a --archive flag?
Need to make directory objects otherwise can't upload an empty directory
* Or could upload empty directories only?
@@ -42,6 +43,10 @@ s3
* Otherwise can set metadata
* Returns etag and last modified in bucket list
+Drive
+ * Should keep a note of files we list then call a new method
+ Object.Update() which would be more efficient than haveing to look
+ the id up for each file
+
Bugs
-
-Non verbose - not sure number transferred got counted up? CHECK
+ * Non verbose - not sure number transferred got counted up? CHECK