drive: Fix multiple files of same name being created

ModifiedDate seems to be set on Insert if set, so do that
This commit is contained in:
Nick Craig-Wood 2014-04-17 22:27:33 +01:00
parent bd62eb17e3
commit 86b77f3ca8
2 changed files with 37 additions and 30 deletions

View File

@ -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) 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 // Guess the mime type
mimeType := mime.TypeByExtension(path.Ext(remote)) mimeType := mime.TypeByExtension(path.Ext(remote))
if mimeType == "" { if mimeType == "" {
mimeType = "application/octet-stream" mimeType = "application/octet-stream"
} }
modifiedDate := modTime.Format(time.RFC3339Nano)
// Define the metadata for the file we are going to create. if found {
info := &drive.File{ // Modify metadata
Title: leaf, info.ModifiedDate = modifiedDate
Description: leaf, info.MimeType = mimeType
Parents: []*drive.ParentReference{{Id: directoryId}},
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()
} }
// 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 err != nil { if err != nil {
return nil, fmt.Errorf("Upload failed: %s", err) return nil, fmt.Errorf("Upload failed: %s", err)
} }
fs.setMetaData(info) 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 return fs, nil
} }

View File

@ -31,6 +31,7 @@ Ideas
* Google cloud storage: https://developers.google.com/storage/ * Google cloud storage: https://developers.google.com/storage/
* rsync over ssh * rsync over ssh
* dropbox: https://github.com/nickoneill/go-dropbox (no MD5s) * 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 Need to make directory objects otherwise can't upload an empty directory
* Or could upload empty directories only? * Or could upload empty directories only?
@ -42,6 +43,10 @@ s3
* Otherwise can set metadata * Otherwise can set metadata
* Returns etag and last modified in bucket list * 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 Bugs
* Non verbose - not sure number transferred got counted up? CHECK
Non verbose - not sure number transferred got counted up? CHECK