"Fossies" - the Fresh Open Source Software Archive 
Member "gdrive-2.1.1/drive/export.go" (28 May 2021, 2783 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 package drive
2
3 import (
4 "fmt"
5 "io"
6 "mime"
7 "os"
8 )
9
10 var DefaultExportMime = map[string]string{
11 "application/vnd.google-apps.form": "application/zip",
12 "application/vnd.google-apps.document": "application/pdf",
13 "application/vnd.google-apps.drawing": "image/svg+xml",
14 "application/vnd.google-apps.spreadsheet": "text/csv",
15 "application/vnd.google-apps.script": "application/vnd.google-apps.script+json",
16 "application/vnd.google-apps.presentation": "application/pdf",
17 }
18
19 type ExportArgs struct {
20 Out io.Writer
21 Id string
22 PrintMimes bool
23 Mime string
24 Force bool
25 }
26
27 func (self *Drive) Export(args ExportArgs) error {
28 f, err := self.service.Files.Get(args.Id).Fields("name", "mimeType").Do()
29 if err != nil {
30 return fmt.Errorf("Failed to get file: %s", err)
31 }
32
33 if args.PrintMimes {
34 return self.printMimes(args.Out, f.MimeType)
35 }
36
37 exportMime, err := getExportMime(args.Mime, f.MimeType)
38 if err != nil {
39 return err
40 }
41
42 filename := getExportFilename(f.Name, exportMime)
43
44 res, err := self.service.Files.Export(args.Id, exportMime).Download()
45 if err != nil {
46 return fmt.Errorf("Failed to download file: %s", err)
47 }
48
49 // Close body on function exit
50 defer res.Body.Close()
51
52 // Check if file exists
53 if !args.Force && fileExists(filename) {
54 return fmt.Errorf("File '%s' already exists, use --force to overwrite", filename)
55 }
56
57 // Create new file
58 outFile, err := os.Create(filename)
59 if err != nil {
60 return fmt.Errorf("Unable to create new file '%s': %s", filename, err)
61 }
62
63 // Close file on function exit
64 defer outFile.Close()
65
66 // Save file to disk
67 _, err = io.Copy(outFile, res.Body)
68 if err != nil {
69 return fmt.Errorf("Failed saving file: %s", err)
70 }
71
72 fmt.Fprintf(args.Out, "Exported '%s' with mime type: '%s'\n", filename, exportMime)
73 return nil
74 }
75
76 func (self *Drive) printMimes(out io.Writer, mimeType string) error {
77 about, err := self.service.About.Get().Fields("exportFormats").Do()
78 if err != nil {
79 return fmt.Errorf("Failed to get about: %s", err)
80 }
81
82 mimes, ok := about.ExportFormats[mimeType]
83 if !ok {
84 return fmt.Errorf("File with type '%s' cannot be exported", mimeType)
85 }
86
87 fmt.Fprintf(out, "Available mime types: %s\n", formatList(mimes))
88 return nil
89 }
90
91 func getExportMime(userMime, fileMime string) (string, error) {
92 if userMime != "" {
93 return userMime, nil
94 }
95
96 defaultMime, ok := DefaultExportMime[fileMime]
97 if !ok {
98 return "", fmt.Errorf("File with type '%s' does not have a default export mime, and can probably not be exported", fileMime)
99 }
100
101 return defaultMime, nil
102 }
103
104 func getExportFilename(name, mimeType string) string {
105 extensions, err := mime.ExtensionsByType(mimeType)
106 if err != nil || len(extensions) == 0 {
107 return name
108 }
109
110 return name + extensions[0]
111 }