forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.go
57 lines (49 loc) · 1.14 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package drive
import (
"fmt"
"io"
"io/ioutil"
"mime"
"path/filepath"
"strings"
)
type ImportArgs struct {
Out io.Writer
Mime string
Progress io.Writer
Path string
Parents []string
}
func (self *Drive) Import(args ImportArgs) error {
fromMime := args.Mime
if fromMime == "" {
fromMime = getMimeType(args.Path)
}
if fromMime == "" {
return fmt.Errorf("Could not determine mime type of file, use --mime")
}
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],
})
if err != nil {
return err
}
fmt.Fprintf(args.Out, "Imported %s with mime type: '%s'\n", f.Id, toMimes[0])
return nil
}
func getMimeType(path string) string {
t := mime.TypeByExtension(filepath.Ext(path))
return strings.Split(t, ";")[0]
}