aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetter Rasmussen2016-01-23 17:09:47 +0100
committerPetter Rasmussen2016-01-23 17:09:47 +0100
commite3aa4296e9fc875866d907df341637db4ac8c815 (patch)
tree9ee2eb275849b419e30c969022a700b63356f892
parent21260bef3f29b97e1b1c2f7867b567066ebac543 (diff)
downloadgdrive-e3aa4296e9fc875866d907df341637db4ac8c815.tar.bz2
Implement download revision
-rw-r--r--drive/revision_download.go70
-rw-r--r--gdrive.go28
-rw-r--r--handlers_drive.go13
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
+}
diff --git a/gdrive.go b/gdrive.go
index f20f9f3..967f6ec 100644
--- a/gdrive.go
+++ b/gdrive.go
@@ -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{