From 6feb3edfaa1c6a6d923a16af950325e0e6112615 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sat, 6 Dec 2014 03:20:39 -0700 Subject: [PATCH] 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 --- README.md | 10 +++++----- pull.go | 5 +---- remote.go | 46 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a905193f..2dc12ad7 100644 --- a/README.md +++ b/README.md @@ -59,17 +59,17 @@ Background sync is not just hard, it's stupid. My technical and philosophical ra exported to different forms e.g docx, xlsx, csv etc. When doing a pull remember to include option `-export ext1,ext2,ext3` where ext1, ext2, ... could be: - * docx - * jpeg + * doc, docx + * jpeg, jpg * html * odt * rtf * pdf * png - * pptx + * ppt, pptx * svg - * txt - * xlsx + * txt, text + * xls, xlsx ## Known issues * Probably, it doesn't work on Windows. diff --git a/pull.go b/pull.go index cb108b5b..31d02207 100644 --- a/pull.go +++ b/pull.go @@ -153,10 +153,7 @@ func (g *Commands) export(f *File, destAbsPath string, exports []string) (manife waitables := map[string]string{} for _, ext := range exports { - mimeType, ok = docExportsMap[ext] - if !ok { - continue - } + mimeType = mimeTypeFromExt(ext) exportURL, ok = f.ExportLinks[mimeType] if !ok { continue diff --git a/remote.go b/remote.go index 416e30bd..1cedfefa 100644 --- a/remote.go +++ b/remote.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "net/http" + "regexp" "strings" "time" @@ -46,25 +47,46 @@ var ( ErrPathNotExists = errors.New("remote path doesn't exist") ) -var docExportsMap = map[string]string{ - "csv": "text/csv", - "html": "text/html", - "txt": "text/plain", +var regExtStrMap = map[string]string{ + "csv": "text/csv", + "html?": "text/html", + "te?xt": "text/plain", - "gif": "image/gif", - "png": "image/png", - "svg": "image/svg+xml", - "jpeg": "image/jpeg", + "gif": "image/gif", + "png": "image/png", + "svg": "image/svg+xml", + "jpe?g": "image/jpeg", "odt": "application/vnd.oasis.opendocument.text", "rtf": "application/rtf", "pdf": "application/pdf", - "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "pptx": "application/vnd.openxmlformats-officedocument.wordprocessingml.presentation", + "docx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "pptx?": "application/vnd.openxmlformats-officedocument.wordprocessingml.presentation", + "xlsx?": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", +} + +func compileRegExtMap() *map[*regexp.Regexp]string { + regExpMap := make(map[*regexp.Regexp]string) + for regStr, mimeType := range regExtStrMap { + regExComp, err := regexp.Compile(regStr) + if err == nil { + regExpMap[regExComp] = mimeType + } + } + return ®ExpMap +} + +var regExtMap = *compileRegExtMap() - "xls": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", +func mimeTypeFromExt(ext string) string { + bExt := []byte(ext) + for regEx, mimeType := range regExtMap { + if regEx != nil && regEx.Match(bExt) { + return mimeType + } + } + return "" } type Remote struct {