Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit ba0e00e

Browse files
author
Emmanuel Odeke
committed
doc export: regex support
Allows for command like this: `drive pull -export doc` Equal to `drive pull -export docx` and any other similar formats bunched together
1 parent 0c4fac7 commit ba0e00e

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,27 @@ Background sync is not just hard, it's stupid. My technical and philosophical ra
5959
exported to different forms e.g docx, xlsx, csv etc.
6060
When doing a pull remember to include option `-export ext1,ext2,ext3`
6161
where ext1, ext2, ... could be:
62-
* docx
63-
* jpeg
62+
* doc, docx
63+
* jpeg, jpg
64+
* gif
6465
* html
6566
* odt
6667
* rtf
6768
* pdf
6869
* png
69-
* pptx
70+
* ppt, pptx
7071
* svg
71-
* txt
72-
* xlsx
72+
* txt, text
73+
* xls, xlsx
74+
75+
The exported files will be placed in a directory in the same path
76+
as the source Doc but affixed with '\_exports' e.g
77+
drive pull -export gif,jpg,svg logo
78+
if successful will create a directory logo\_exports which will look like:
79+
|- logo\_exports
80+
|- logo.gif
81+
|- logo.png
82+
|- logo.svg
7383

7484
## Known issues
7585
* Probably, it doesn't work on Windows.

pull.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ func (g *Commands) export(f *File, destAbsPath string, exports []string) (manife
153153

154154
waitables := map[string]string{}
155155
for _, ext := range exports {
156-
mimeType, ok = docExportsMap[ext]
157-
if !ok {
158-
continue
159-
}
156+
mimeType = mimeTypeFromExt(ext)
160157
exportURL, ok = f.ExportLinks[mimeType]
161158
if !ok {
162159
continue

remote.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"io"
2121
"net/http"
22+
"regexp"
2223
"strings"
2324
"time"
2425

@@ -46,25 +47,46 @@ var (
4647
ErrPathNotExists = errors.New("remote path doesn't exist")
4748
)
4849

49-
var docExportsMap = map[string]string{
50-
"csv": "text/csv",
51-
"html": "text/html",
52-
"txt": "text/plain",
50+
var regExtStrMap = map[string]string{
51+
"csv": "text/csv",
52+
"html?": "text/html",
53+
"te?xt": "text/plain",
5354

54-
"gif": "image/gif",
55-
"png": "image/png",
56-
"svg": "image/svg+xml",
57-
"jpeg": "image/jpeg",
55+
"gif": "image/gif",
56+
"png": "image/png",
57+
"svg": "image/svg+xml",
58+
"jpe?g": "image/jpeg",
5859

5960
"odt": "application/vnd.oasis.opendocument.text",
6061
"rtf": "application/rtf",
6162
"pdf": "application/pdf",
6263

63-
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
64-
"pptx": "application/vnd.openxmlformats-officedocument.wordprocessingml.presentation",
64+
"docx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
65+
"pptx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.presentation",
66+
"xlsx?": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
67+
}
68+
69+
func compileRegExtMap() *map[*regexp.Regexp]string {
70+
regExpMap := make(map[*regexp.Regexp]string)
71+
for regStr, mimeType := range regExtStrMap {
72+
regExComp, err := regexp.Compile(regStr)
73+
if err == nil {
74+
regExpMap[regExComp] = mimeType
75+
}
76+
}
77+
return &regExpMap
78+
}
79+
80+
var regExtMap = *compileRegExtMap()
6581

66-
"xls": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
67-
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
82+
func mimeTypeFromExt(ext string) string {
83+
bExt := []byte(ext)
84+
for regEx, mimeType := range regExtMap {
85+
if regEx != nil && regEx.Match(bExt) {
86+
return mimeType
87+
}
88+
}
89+
return ""
6890
}
6991

7092
type Remote struct {

types.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,30 @@ const (
3232
)
3333

3434
type File struct {
35+
BlobAt string
36+
ExportLinks map[string]string
3537
Id string
36-
Name string
3738
IsDir bool
39+
Md5Checksum string
40+
MimeType string
3841
ModTime time.Time
42+
Name string
3943
Size int64
40-
BlobAt string
41-
MimeType string
42-
Md5Checksum string
43-
ExportLinks map[string]string
4444
}
4545

4646
func NewRemoteFile(f *drive.File) *File {
4747
mtime, _ := time.Parse("2006-01-02T15:04:05.000Z", f.ModifiedDate)
4848
mtime = mtime.Round(time.Second)
4949
return &File{
50+
BlobAt: f.DownloadUrl,
51+
ExportLinks: f.ExportLinks,
5052
Id: f.Id,
51-
Name: f.Title,
5253
IsDir: f.MimeType == "application/vnd.google-apps.folder",
54+
Md5Checksum: f.Md5Checksum,
55+
MimeType: f.MimeType,
5356
ModTime: mtime,
57+
Name: f.Title,
5458
Size: f.FileSize,
55-
MimeType: f.MimeType,
56-
BlobAt: f.DownloadUrl,
57-
Md5Checksum: f.Md5Checksum,
58-
ExportLinks: f.ExportLinks,
5959
}
6060
}
6161

0 commit comments

Comments
 (0)