diff options
| -rw-r--r-- | drive/files.go | 56 | ||||
| -rw-r--r-- | drive/types.go | 10 | ||||
| -rw-r--r-- | gdrive.go | 25 | ||||
| -rw-r--r-- | handlers.go | 16 |
4 files changed, 88 insertions, 19 deletions
diff --git a/drive/files.go b/drive/files.go index ed3c349..49cf034 100644 --- a/drive/files.go +++ b/drive/files.go @@ -3,7 +3,11 @@ package drive import ( "fmt" "io" + "mime" "os" + "path/filepath" + "google.golang.org/api/drive/v3" + "golang.org/x/net/context" ) func (self *Drive) List(args ListFilesArgs) { @@ -66,3 +70,55 @@ func (self *Drive) Download(args DownloadFileArgs) { // self.Delete(args.Id) //} } + +func (self *Drive) Upload(args UploadFileArgs) { + //if args.Stdin { + // self.uploadStdin() + //} + + srcFile, err := os.Open(args.Path) + if err != nil { + exitF("Failed to open file: %s", err.Error()) + } + + srcFileInfo, err := srcFile.Stat() + if err != nil { + exitF("Failed to read file metadata: %s", err.Error()) + } + + // 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 folder if provided + if args.Parent != "" { + dstFile.Parents = []string{args.Parent} + } + + f, err := self.service.Files.Create(dstFile).ResumableMedia(context.Background(), srcFile, srcFileInfo.Size(), dstFile.MimeType).Do() + if err != nil { + exitF("Failed to upload file: %s", err.Error()) + } + + fmt.Printf("Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size) + //if args.Share { + // self.Share(TODO) + //} +} + +//func newFile(args UploadFileArgs) *drive.File { +// +//} diff --git a/drive/types.go b/drive/types.go index d70dfb5..6c1bd14 100644 --- a/drive/types.go +++ b/drive/types.go @@ -35,3 +35,13 @@ type DownloadFileArgs struct { NoProgress bool Stdout bool } + +type UploadFileArgs struct { + Path string + Name string + Parent string + Mime string + Recursive bool + Stdin bool + Share bool +} @@ -13,7 +13,6 @@ const ClientId = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleu const ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO" const DefaultMaxFiles = 100 -const DefaultChunkSize = 4194304 var DefaultConfigDir = GetDefaultConfigDir() var DefaultTokenFilePath = GetDefaultTokenFilePath() @@ -50,7 +49,7 @@ func main() { }, cli.BoolFlag{ Name: "skipHeader", - Patterns: []string{"--noheader"}, + Patterns: []string{"--no-header"}, Description: "Dont print the header", OmitValue: true, }, @@ -78,7 +77,7 @@ func main() { }, cli.BoolFlag{ Name: "noProgress", - Patterns: []string{"--noprogress"}, + Patterns: []string{"--no-progress"}, Description: "Hide progress", OmitValue: true, }, @@ -94,7 +93,7 @@ func main() { &cli.Handler{ Pattern: "[global options] upload [options] <path>", Description: "Upload file or directory", - Callback: handler, + Callback: uploadHandler, Flags: cli.Flags{ "global options": globalFlags, "options": []cli.Flag{ @@ -115,9 +114,9 @@ func main() { Description: "Filename", }, cli.BoolFlag{ - Name: "progress", - Patterns: []string{"--progress"}, - Description: "Show progress", + Name: "noProgress", + Patterns: []string{"--no-progress"}, + Description: "Hide progress", OmitValue: true, }, cli.BoolFlag{ @@ -137,18 +136,6 @@ func main() { Description: "Share file", OmitValue: true, }, - cli.BoolFlag{ - Name: "share", - Patterns: []string{"--convert"}, - Description: "Convert file to google docs format", - OmitValue: true, - }, - cli.IntFlag{ - Name: "chunksize", - Patterns: []string{"--chunksize"}, - Description: fmt.Sprintf("Set chunk size in bytes. Minimum is 262144, default is %d", DefaultChunkSize), - DefaultValue: DefaultChunkSize, - }, }, }, }, diff --git a/handlers.go b/handlers.go index a4bb3e2..426f089 100644 --- a/handlers.go +++ b/handlers.go @@ -32,6 +32,21 @@ func downloadHandler(ctx cli.Context) { }) } +func uploadHandler(ctx cli.Context) { + args := ctx.Args() + gdrive := newDrive() + + gdrive.Upload(drive.UploadFileArgs{ + Path: args.String("path"), + Name: args.String("name"), + Parent: args.String("parent"), + Mime: args.String("mime"), + Recursive: args.Bool("recursive"), + Stdin: args.Bool("stdin"), + Share: args.Bool("share"), + }) +} + func deleteHandler(ctx cli.Context) { fmt.Println("Deleting...") } @@ -74,6 +89,7 @@ func printCommandHelp(ctx cli.Context) { } } +// TODO: take app path as arg func newDrive() *drive.Drive { oauth, err := client.NewOauthClient(ClientId, ClientSecret, DefaultTokenFilePath, authCodePrompt) if err != nil { |
