Skip to content

Commit

Permalink
Download to tmp file and rename on success
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 13, 2016
1 parent 03384c6 commit 6d78f27
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions drive/revision_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,28 @@ func (self *Drive) DownloadRevision(args DownloadRevisionArgs) (err error) {
return fmt.Errorf("File '%s' already exists, use --force to overwrite", rev.OriginalFilename)
}

// Download to tmp file
tmpPath := rev.OriginalFilename + ".incomplete"

// Create new file
outFile, err := os.Create(rev.OriginalFilename)
outFile, err := os.Create(tmpPath)
if err != nil {
return fmt.Errorf("Unable to create new file: %s", err)
}

// Close file on function exit
defer outFile.Close()

// Save file to disk
bytes, err := io.Copy(outFile, srcReader)
if err != nil {
outFile.Close()
os.Remove(tmpPath)
return fmt.Errorf("Failed saving file: %s", err)
}

fmt.Fprintf(args.Out, "Downloaded '%s' at %s, total %d\n", rev.OriginalFilename, "x/s", bytes)

//if deleteSourceFile {
// self.Delete(args.Id)
//}
return
// Close File
outFile.Close()

// Rename tmp file to proper filename
return os.Rename(tmpPath, rev.OriginalFilename)
}

0 comments on commit 6d78f27

Please sign in to comment.