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

Commit

Permalink
doc export: regex support
Browse files Browse the repository at this point in the history
Allows for command like this:
`drive pull -export doc`
Equal to
`drive pull -export docx`
and any other similar formats bunched together
  • Loading branch information
Emmanuel Odeke committed Dec 6, 2014
1 parent 0c4fac7 commit c83bc67
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,27 @@ 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
* gif
* html
* odt
* rtf
* pdf
* png
* pptx
* ppt, pptx
* svg
* txt
* xlsx
* txt, text
* xls, xlsx

The exported files will be placed in a directory in the same path
as the source Doc but affixed with '\_exports' e.g
drive pull -export gif,jpg,svg logo
if successful will create a directory logo\_exports which will look like:
|- logo\_exports
|- logo.gif
|- logo.png
|- logo.svg

## Known issues
* Probably, it doesn't work on Windows.
Expand Down
5 changes: 1 addition & 4 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 34 additions & 12 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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 &regExpMap
}

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 {
Expand Down

0 comments on commit c83bc67

Please sign in to comment.