diff options
| -rw-r--r-- | drive/update.go | 71 | ||||
| -rw-r--r-- | gdrive.go | 43 | ||||
| -rw-r--r-- | handlers_drive.go | 16 | 
3 files changed, 130 insertions, 0 deletions
| diff --git a/drive/update.go b/drive/update.go new file mode 100644 index 0000000..4fa33ed --- /dev/null +++ b/drive/update.go @@ -0,0 +1,71 @@ +package drive + +import ( +    "fmt" +    "mime" +    "os" +    "io" +    "path/filepath" +    "google.golang.org/api/drive/v3" +    "golang.org/x/net/context" +) + +type UpdateArgs struct { +    Out io.Writer +    Id string +    Path string +    Name string +    Parents []string +    Mime string +    Recursive bool +    Stdin bool +    Share bool +    NoProgress bool +} + +func (self *Drive) Update(args UpdateArgs) (err error) { +    //if args.Stdin { +    //    self.uploadStdin() +    //} + +    srcFile, err := os.Open(args.Path) +    if err != nil { +        return fmt.Errorf("Failed to open file: %s", err) +    } + +    srcFileInfo, err := srcFile.Stat() +    if err != nil { +        return fmt.Errorf("Failed to read file metadata: %s", err) +    } + +    // Instantiate empty drive file +    dstFile := &drive.File{} + +    // Use provided file name or use filename +    if args.Name == "" { +        dstFile.Name = filepath.Base(srcFileInfo.Name()) +    } else { +        dstFile.Name = args.Name +    } + +    // Set provided mime type or get type based on file extension +    if args.Mime == "" { +        dstFile.MimeType = mime.TypeByExtension(filepath.Ext(dstFile.Name)) +    } else { +        dstFile.MimeType = args.Mime +    } + +    // Set parent folders +    dstFile.Parents = args.Parents + +    f, err := self.service.Files.Update(args.Id, dstFile).ResumableMedia(context.Background(), srcFile, srcFileInfo.Size(), dstFile.MimeType).Do() +    if err != nil { +        return fmt.Errorf("Failed to upload file: %s", err) +    } + +    fmt.Fprintf(args.Out, "Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size) +    //if args.Share { +    //    self.Share(TODO) +    //} +    return +} @@ -202,6 +202,49 @@ func main() {              },          },          &cli.Handler{ +            Pattern: "[global options] update [options] <id> <path>", +            Description: "Update file, this creates a new revision of the file", +            Callback: updateHandler, +            Flags: cli.Flags{ +                "global options": globalFlags, +                "options": []cli.Flag{ +                    cli.StringSliceFlag{ +                        Name: "parent", +                        Patterns: []string{"-p", "--parent"}, +                        Description: "Parent id, used to upload file to a specific directory, can be specified multiple times to give many parents", +                    }, +                    cli.StringFlag{ +                        Name: "name", +                        Patterns: []string{"--name"}, +                        Description: "Filename", +                    }, +                    cli.BoolFlag{ +                        Name: "noProgress", +                        Patterns: []string{"--no-progress"}, +                        Description: "Hide progress", +                        OmitValue: true, +                    }, +                    cli.BoolFlag{ +                        Name: "stdin", +                        Patterns: []string{"--stdin"}, +                        Description: "Use stdin as file content", +                        OmitValue: true, +                    }, +                    cli.StringFlag{ +                        Name: "mime", +                        Patterns: []string{"--mime"}, +                        Description: "Force mime type", +                    }, +                    cli.BoolFlag{ +                        Name: "share", +                        Patterns: []string{"--share"}, +                        Description: "Share file", +                        OmitValue: true, +                    }, +                }, +            }, +        }, +        &cli.Handler{              Pattern: "[global options] info [options] <id>",              Description: "Show file info",              Callback: infoHandler, diff --git a/handlers_drive.go b/handlers_drive.go index 1b01996..7491996 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -67,6 +67,22 @@ func uploadHandler(ctx cli.Context) {      checkErr(err)  } +func updateHandler(ctx cli.Context) { +    args := ctx.Args() +    err := newDrive(args).Update(drive.UpdateArgs{ +        Out: os.Stdout, +        Id: args.String("id"), +        Path: args.String("path"), +        Name: args.String("name"), +        Parents: args.StringSlice("parent"), +        Mime: args.String("mime"), +        Stdin: args.Bool("stdin"), +        Share: args.Bool("share"), +        NoProgress: args.Bool("noProgress"), +    }) +    checkErr(err) +} +  func infoHandler(ctx cli.Context) {      args := ctx.Args()      err := newDrive(args).Info(drive.FileInfoArgs{ | 
