"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/revision_download.go" (28 May 2021, 1797 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "io"
    6     "io/ioutil"
    7     "path/filepath"
    8     "time"
    9 )
   10 
   11 type DownloadRevisionArgs struct {
   12     Out        io.Writer
   13     Progress   io.Writer
   14     FileId     string
   15     RevisionId string
   16     Path       string
   17     Force      bool
   18     Stdout     bool
   19     Timeout    time.Duration
   20 }
   21 
   22 func (self *Drive) DownloadRevision(args DownloadRevisionArgs) (err error) {
   23     getRev := self.service.Revisions.Get(args.FileId, args.RevisionId)
   24 
   25     rev, err := getRev.Fields("originalFilename").Do()
   26     if err != nil {
   27         return fmt.Errorf("Failed to get file: %s", err)
   28     }
   29 
   30     if rev.OriginalFilename == "" {
   31         return fmt.Errorf("Download is not supported for this file type")
   32     }
   33 
   34     // Get timeout reader wrapper and context
   35     timeoutReaderWrapper, ctx := getTimeoutReaderWrapperContext(args.Timeout)
   36 
   37     res, err := getRev.Context(ctx).Download()
   38     if err != nil {
   39         if isTimeoutError(err) {
   40             return fmt.Errorf("Failed to download file: timeout, no data was transferred for %v", args.Timeout)
   41         }
   42         return fmt.Errorf("Failed to download file: %s", err)
   43     }
   44 
   45     // Close body on function exit
   46     defer res.Body.Close()
   47 
   48     // Discard other output if file is written to stdout
   49     out := args.Out
   50     if args.Stdout {
   51         out = ioutil.Discard
   52     }
   53 
   54     // Path to file
   55     fpath := filepath.Join(args.Path, rev.OriginalFilename)
   56 
   57     fmt.Fprintf(out, "Downloading %s -> %s\n", rev.OriginalFilename, fpath)
   58 
   59     bytes, rate, err := self.saveFile(saveFileArgs{
   60         out:           args.Out,
   61         body:          timeoutReaderWrapper(res.Body),
   62         contentLength: res.ContentLength,
   63         fpath:         fpath,
   64         force:         args.Force,
   65         stdout:        args.Stdout,
   66         progress:      args.Progress,
   67     })
   68 
   69     if err != nil {
   70         return err
   71     }
   72 
   73     fmt.Fprintf(out, "Download complete, rate: %s/s, total size: %s\n", formatSize(rate, false), formatSize(bytes, false))
   74     return nil
   75 }