aboutsummaryrefslogtreecommitdiffstats
path: root/drive/import.go
diff options
context:
space:
mode:
authorPetter Rasmussen2016-01-25 22:34:58 +0100
committerPetter Rasmussen2016-01-25 22:34:58 +0100
commit35bbe302d53346426541f63f134cb02a11c50e78 (patch)
tree61d27cc6c6d211d1c09c8fb1ebe4d60a52539ef6 /drive/import.go
parent3a669be42beacbde675088f4be12a7a9b078acd7 (diff)
downloadgdrive-35bbe302d53346426541f63f134cb02a11c50e78.tar.bz2
Implement import
Diffstat (limited to 'drive/import.go')
-rw-r--r--drive/import.go57
1 files changed, 57 insertions, 0 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]
+}