diff options
| -rw-r--r-- | drive/upload.go | 47 | ||||
| -rw-r--r-- | drive/util.go | 4 | ||||
| -rw-r--r-- | gdrive.go | 33 | ||||
| -rw-r--r-- | handlers_drive.go | 15 |
4 files changed, 90 insertions, 9 deletions
diff --git a/drive/upload.go b/drive/upload.go index d57e020..5c5e38e 100644 --- a/drive/upload.go +++ b/drive/upload.go @@ -6,6 +6,7 @@ import ( "os" "io" "path/filepath" + "google.golang.org/api/googleapi" "google.golang.org/api/drive/v3" "golang.org/x/net/context" ) @@ -17,16 +18,11 @@ type UploadFileArgs struct { Parents []string Mime string Recursive bool - Stdin bool Share bool NoProgress bool } func (self *Drive) Upload(args UploadFileArgs) (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) @@ -68,3 +64,44 @@ func (self *Drive) Upload(args UploadFileArgs) (err error) { //} return } + +type UploadStreamArgs struct { + Out io.Writer + In io.Reader + Name string + Parents []string + Mime string + Share bool + ChunkSize int64 +} + +func (self *Drive) UploadStream(args UploadStreamArgs) (err error) { + if args.ChunkSize > intMax() - 1 { + return fmt.Errorf("Chunk size is to big, max chunk size for this computer is %d", intMax() - 1) + } + + // Instantiate empty drive file + dstFile := &drive.File{Name: args.Name} + + // Set mime type if provided + if args.Mime != "" { + dstFile.MimeType = args.Mime + } + + // Set parent folders + dstFile.Parents = args.Parents + + // Chunk size option + chunkSize := googleapi.ChunkSize(int(args.ChunkSize)) + + f, err := self.service.Files.Create(dstFile).Media(args.In, chunkSize).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 +} diff --git a/drive/util.go b/drive/util.go index be01c80..af80b20 100644 --- a/drive/util.go +++ b/drive/util.go @@ -105,3 +105,7 @@ func fileExists(path string) bool { } return false } + +func intMax() int64 { + return 1 << (strconv.IntSize - 1) - 1 +} @@ -11,6 +11,7 @@ const Version = "2.0.0" const DefaultMaxFiles = 30 const DefaultNameWidth = 40 +const DefaultUploadChunkSize = 8 * 1024 * 1024 const DefaultQuery = "trashed = false and 'me' in owners" const DefaultShareRole = "reader" const DefaultShareType = "anyone" @@ -181,12 +182,38 @@ func main() { Description: "Hide progress", OmitValue: true, }, + cli.StringFlag{ + Name: "mime", + Patterns: []string{"--mime"}, + Description: "Force mime type", + }, cli.BoolFlag{ - Name: "stdin", - Patterns: []string{"--stdin"}, - Description: "Use stdin as file content", + Name: "share", + Patterns: []string{"--share"}, + Description: "Share file", OmitValue: true, }, + }, + }, + }, + &cli.Handler{ + Pattern: "[global options] upload stdin [options] <name>", + Description: "Upload file from stdin", + Callback: uploadStdinHandler, + 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.IntFlag{ + Name: "chunksize", + Patterns: []string{"--chunksize"}, + Description: fmt.Sprintf("Set chunk size in bytes, default: %d", DefaultUploadChunkSize), + DefaultValue: DefaultUploadChunkSize, + }, cli.StringFlag{ Name: "mime", Patterns: []string{"--mime"}, diff --git a/handlers_drive.go b/handlers_drive.go index 988cc46..5c3b549 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -60,13 +60,26 @@ func uploadHandler(ctx cli.Context) { Parents: args.StringSlice("parent"), Mime: args.String("mime"), Recursive: args.Bool("recursive"), - Stdin: args.Bool("stdin"), Share: args.Bool("share"), NoProgress: args.Bool("noProgress"), }) checkErr(err) } +func uploadStdinHandler(ctx cli.Context) { + args := ctx.Args() + err := newDrive(args).UploadStream(drive.UploadStreamArgs{ + Out: os.Stdout, + In: os.Stdin, + Name: args.String("name"), + Parents: args.StringSlice("parent"), + Mime: args.String("mime"), + Share: args.Bool("share"), + ChunkSize: args.Int64("chunksize"), + }) + checkErr(err) +} + func updateHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Update(drive.UpdateArgs{ |
