aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetter Rasmussen2016-01-25 22:34:58 +0100
committerPetter Rasmussen2016-01-25 22:34:58 +0100
commit35bbe302d53346426541f63f134cb02a11c50e78 (patch)
tree61d27cc6c6d211d1c09c8fb1ebe4d60a52539ef6
parent3a669be42beacbde675088f4be12a7a9b078acd7 (diff)
downloadgdrive-35bbe302d53346426541f63f134cb02a11c50e78.tar.bz2
Implement import
-rw-r--r--drive/import.go57
-rw-r--r--drive/upload.go11
-rw-r--r--gdrive.go27
-rw-r--r--handlers_drive.go12
4 files changed, 102 insertions, 5 deletions
diff --git a/drive/import.go b/drive/import.go
new file mode 100644
index 0000000..cb82508
--- /dev/null
+++ b/drive/import.go
@@ -0,0 +1,57 @@
+package drive
+
+import (
+ "io"
+ "io/ioutil"
+ "fmt"
+ "strings"
+ "mime"
+ "path/filepath"
+)
+
+type ImportArgs struct {
+ Out io.Writer
+ Progress io.Writer
+ Path string
+ Share bool
+ Parents []string
+}
+
+func (self *Drive) Import(args ImportArgs) error {
+ fromMime := getMimeType(args.Path)
+ if fromMime == "" {
+ return fmt.Errorf("Could not determine mime type of file")
+ }
+
+ about, err := self.service.About.Get().Fields("importFormats").Do()
+ if err != nil {
+ return fmt.Errorf("Failed to get about: %s", err)
+ }
+
+ toMimes, ok := about.ImportFormats[fromMime]
+ if !ok || len(toMimes) == 0 {
+ return fmt.Errorf("Mime type '%s' is not supported for import", fromMime)
+ }
+
+ f, err := self.uploadFile(UploadArgs{
+ Out: ioutil.Discard,
+ Progress: args.Progress,
+ Path: args.Path,
+ Parents: args.Parents,
+ Mime: toMimes[0],
+ Share: args.Share,
+ })
+ if err != nil {
+ return err
+ }
+
+ fmt.Fprintf(args.Out, "[document] id: %s, name: %s\n", f.Id, f.Name)
+ fmt.Fprintf(args.Out, "Imported %s with mime type: '%s'\n", args.Path, toMimes[0])
+
+ return nil
+}
+
+func getMimeType(path string) string {
+ t := mime.TypeByExtension(filepath.Ext(path))
+ return strings.Split(t, ";")[0]
+}
diff --git a/drive/upload.go b/drive/upload.go
index 5e70dd8..1e145f7 100644
--- a/drive/upload.go
+++ b/drive/upload.go
@@ -48,7 +48,8 @@ func (self *Drive) upload(args UploadArgs) error {
args.Name = ""
return self.uploadDirectory(args)
} else {
- return self.uploadFile(args)
+ _, err := self.uploadFile(args)
+ return err
}
}
@@ -91,10 +92,10 @@ func (self *Drive) uploadDirectory(args UploadArgs) error {
return nil
}
-func (self *Drive) uploadFile(args UploadArgs) error {
+func (self *Drive) uploadFile(args UploadArgs) (*drive.File, error) {
srcFile, srcFileInfo, err := openFile(args.Path)
if err != nil {
- return err
+ return nil, err
}
// Instantiate empty drive file
@@ -128,7 +129,7 @@ func (self *Drive) uploadFile(args UploadArgs) error {
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "md5Checksum").Media(srcReader, chunkSize).Do()
if err != nil {
- return fmt.Errorf("Failed to upload file: %s", err)
+ return nil, fmt.Errorf("Failed to upload file: %s", err)
}
// Calculate average upload rate
@@ -136,7 +137,7 @@ func (self *Drive) uploadFile(args UploadArgs) error {
fmt.Fprintf(args.Out, "[file] id: %s, md5: %s, name: %s\n", f.Id, f.Md5Checksum, f.Name)
fmt.Fprintf(args.Out, "Uploaded '%s' at %s/s, total %s\n", f.Name, formatSize(rate, false), formatSize(f.Size, false))
- return nil
+ return f, nil
}
type UploadStreamArgs struct {
diff --git a/gdrive.go b/gdrive.go
index 7d73dce..754de48 100644
--- a/gdrive.go
+++ b/gdrive.go
@@ -311,6 +311,33 @@ func main() {
},
},
&cli.Handler{
+ Pattern: "[global] import [options] <path>",
+ Description: "Upload and convert file to a google document, see 'about import' for available conversions",
+ Callback: importHandler,
+ Flags: cli.Flags{
+ "global": 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.BoolFlag{
+ Name: "noProgress",
+ Patterns: []string{"--no-progress"},
+ Description: "Hide progress",
+ OmitValue: true,
+ },
+ cli.BoolFlag{
+ Name: "share",
+ Patterns: []string{"--share"},
+ Description: "Share file",
+ OmitValue: true,
+ },
+ },
+ },
+ },
+ &cli.Handler{
Pattern: "[global] export [options] <id>",
Description: "Export a google document",
Callback: exportHandler,
diff --git a/handlers_drive.go b/handlers_drive.go
index 38c1e1c..a8303ed 100644
--- a/handlers_drive.go
+++ b/handlers_drive.go
@@ -112,6 +112,18 @@ func infoHandler(ctx cli.Context) {
checkErr(err)
}
+func importHandler(ctx cli.Context) {
+ args := ctx.Args()
+ err := newDrive(args).Import(drive.ImportArgs{
+ Out: os.Stdout,
+ Path: args.String("path"),
+ Parents: args.StringSlice("parent"),
+ Share: args.Bool("share"),
+ Progress: progressWriter(args.Bool("noProgress")),
+ })
+ checkErr(err)
+}
+
func exportHandler(ctx cli.Context) {
args := ctx.Args()
err := newDrive(args).Export(drive.ExportArgs{