diff options
Diffstat (limited to 'drive/revision_download.go')
| -rw-r--r-- | drive/revision_download.go | 51 | 
1 files changed, 21 insertions, 30 deletions
| diff --git a/drive/revision_download.go b/drive/revision_download.go index be64187..9cc9d1d 100644 --- a/drive/revision_download.go +++ b/drive/revision_download.go @@ -2,8 +2,9 @@ package drive  import (      "fmt" +    "path/filepath"      "io" -    "os" +    "io/ioutil"  )  type DownloadRevisionArgs struct { @@ -11,6 +12,7 @@ type DownloadRevisionArgs struct {      Progress io.Writer      FileId string      RevisionId string +    Path string      Force bool      Stdout bool  } @@ -35,42 +37,31 @@ func (self *Drive) DownloadRevision(args DownloadRevisionArgs) (err error) {      // Close body on function exit      defer res.Body.Close() -    // Wrap response body in progress reader -    srcReader := getProgressReader(res.Body, args.Progress, res.ContentLength) - +    // Discard other output if file is written to stdout +    out := args.Out      if args.Stdout { -        // Write file content to stdout -        _, err := io.Copy(args.Out, srcReader) -        return err +        out = ioutil.Discard      } -    // Check if file exists -    if !args.Force && fileExists(rev.OriginalFilename) { -        return fmt.Errorf("File '%s' already exists, use --force to overwrite", rev.OriginalFilename) -    } +    // Path to file +    fpath := filepath.Join(args.Path, rev.OriginalFilename) -    // Download to tmp file -    tmpPath := rev.OriginalFilename + ".incomplete" +    fmt.Fprintf(out, "Downloading %s -> %s\n", rev.OriginalFilename, fpath) -    // Create new file -    outFile, err := os.Create(tmpPath) -    if err != nil { -        return fmt.Errorf("Unable to create new file: %s", err) -    } +    bytes, rate, err := self.saveFile(saveFileArgs{ +        out: args.Out, +        body: res.Body, +        contentLength: res.ContentLength, +        fpath: fpath, +        force: args.Force, +        stdout: args.Stdout, +        progress: args.Progress, +    }) -    // 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) +        return err      } -    fmt.Fprintf(args.Out, "Downloaded '%s' at %s, total %d\n", rev.OriginalFilename, "x/s", bytes) - -    // Close File -    outFile.Close() - -    // Rename tmp file to proper filename -    return os.Rename(tmpPath, rev.OriginalFilename) +    fmt.Fprintf(out, "Download complete, rate: %s/s, total size: %s\n", formatSize(rate, false), formatSize(bytes, false)) +    return nil  } | 
