Skip to content

Commit

Permalink
drive: Fix multiple files of same name being created
Browse files Browse the repository at this point in the history
ModifiedDate seems to be set on Insert if set, so do that
  • Loading branch information
ncw committed Apr 17, 2014
1 parent bd62eb1 commit 86b77f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
60 changes: 31 additions & 29 deletions drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 7 additions & 2 deletions notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

0 comments on commit 86b77f3

Please sign in to comment.