diff options
| -rw-r--r-- | drive/revision_download.go | 70 | ||||
| -rw-r--r-- | gdrive.go | 28 | ||||
| -rw-r--r-- | handlers_drive.go | 13 | 
3 files changed, 111 insertions, 0 deletions
| diff --git a/drive/revision_download.go b/drive/revision_download.go new file mode 100644 index 0000000..c26c3ce --- /dev/null +++ b/drive/revision_download.go @@ -0,0 +1,70 @@ +package drive + +import ( +    "fmt" +    "io" +    "os" +) + +type DownloadRevisionArgs struct { +    Out io.Writer +    FileId string +    RevisionId string +    Force bool +    NoProgress bool +    Stdout bool +} + +func (self *Drive) DownloadRevision(args DownloadRevisionArgs) (err error) { +    getRev := self.service.Revisions.Get(args.FileId, args.RevisionId) + +    rev, err := getRev.Fields("originalFilename").Do() +    if err != nil { +        return fmt.Errorf("Failed to get file: %s", err) +    } + +    if rev.OriginalFilename == "" { +        return fmt.Errorf("Download is not supported for this file type") +    } + +    res, err := getRev.Download() +    if err != nil { +        return fmt.Errorf("Failed to download file: %s", err) +    } + +    // Close body on function exit +    defer res.Body.Close() + +    if args.Stdout { +        // Write file content to stdout +        _, err := io.Copy(os.Stdout, res.Body) +        return err +    } + +    // Check if file exists +    if !args.Force && fileExists(rev.OriginalFilename) { +        return fmt.Errorf("File '%s' already exists, use --force to overwrite", rev.OriginalFilename) +    } + +    // Create new file +    outFile, err := os.Create(rev.OriginalFilename) +    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, res.Body) +    if err != nil { +        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 +} @@ -125,6 +125,34 @@ func main() {              },          },          &cli.Handler{ +            Pattern: "[global options] download revision [options] <fileId> <revisionId>", +            Description: "Download revision", +            Callback: downloadRevisionHandler, +            Flags: cli.Flags{ +                "global options": globalFlags, +                "options": []cli.Flag{ +                    cli.BoolFlag{ +                        Name: "force", +                        Patterns: []string{"-f", "--force"}, +                        Description: "Overwrite existing file", +                        OmitValue: true, +                    }, +                    cli.BoolFlag{ +                        Name: "noProgress", +                        Patterns: []string{"--no-progress"}, +                        Description: "Hide progress", +                        OmitValue: true, +                    }, +                    cli.BoolFlag{ +                        Name: "stdout", +                        Patterns: []string{"--stdout"}, +                        Description: "Write file content to stdout", +                        OmitValue: true, +                    }, +                }, +            }, +        }, +        &cli.Handler{              Pattern: "[global options] upload [options] <path>",              Description: "Upload file or directory",              Callback: uploadHandler, diff --git a/handlers_drive.go b/handlers_drive.go index f208bed..1b01996 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -38,6 +38,19 @@ func downloadHandler(ctx cli.Context) {      checkErr(err)  } +func downloadRevisionHandler(ctx cli.Context) { +    args := ctx.Args() +    err := newDrive(args).DownloadRevision(drive.DownloadRevisionArgs{ +        Out: os.Stdout, +        FileId: args.String("fileId"), +        RevisionId: args.String("revisionId"), +        Force: args.Bool("force"), +        Stdout: args.Bool("stdout"), +        NoProgress: args.Bool("noProgress"), +    }) +    checkErr(err) +} +  func uploadHandler(ctx cli.Context) {      args := ctx.Args()      err := newDrive(args).Upload(drive.UploadFileArgs{ | 
