Skip to content

Commit

Permalink
Show available formats for given file. Change export arg to --format
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed May 23, 2015
1 parent ed6dc06 commit dd1adb7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 51 deletions.
9 changes: 1 addition & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,7 @@ func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool,
return fmt.Errorf("An error occurred: %v\n", err)
}

downloadUrl := info.DownloadUrl
extension := ""

if downloadUrl == "" && format == "" {
return fmt.Errorf("Document has no download url. Try to specify an export format.")
}

downloadUrl, extension, err = util.ExportFormat(info, format)
downloadUrl, extension, err := util.InternalDownloadUrlAndExtension(info, format)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Options struct {

Download struct {
FileId string `goptions:"-i, --id, mutexgroup='download', obligatory, description='File Id'"`
Export string `goptions:"-e, --export, description='Export file format for google docs'"`
Format string `goptions:"--format, description='Download file in a specified format (needed for google docs)'"`
Stdout bool `goptions:"-s, --stdout, description='Write file content to stdout'"`
Force bool `goptions:"--force, description='Overwrite existing file'"`
Pop bool `goptions:"--pop, mutexgroup='download', description='Download latest file, and remove it from google drive'"`
Expand Down Expand Up @@ -123,9 +123,9 @@ func main() {
case "download":
args := opts.Download
if args.Pop {
err = cli.DownloadLatest(drive, args.Stdout, args.Export, args.Force)
err = cli.DownloadLatest(drive, args.Stdout, args.Format, args.Force)
} else {
err = cli.Download(drive, args.FileId, args.Stdout, false, args.Export, args.Force)
err = cli.Download(drive, args.FileId, args.Stdout, false, args.Format, args.Force)
}

case "delete":
Expand Down
96 changes: 56 additions & 40 deletions util/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,60 @@ func ParentList(parents []*drive.ParentReference) string {
return strings.Join(ids, ", ")
}

func ExportFormat(info *drive.File, format string) (downloadUrl string, extension string, err error) {
// See https://developers.google.com/drive/web/manage-downloads#downloading_google_documents
switch format {
case "docx":
extension = ".docx"
downloadUrl = info.ExportLinks["application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
case "xlsx":
extension = ".xlsx"
downloadUrl = info.ExportLinks["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]
case "pptx":
extension = ".pptx"
downloadUrl = info.ExportLinks["application/application/vnd.openxmlformats-officedocument.presentationml.presentation"]
case "odf":
extension = ".odf"
downloadUrl = info.ExportLinks["application/vnd.oasis.opendocument.text"]
case "ods":
extension = ".ods"
downloadUrl = info.ExportLinks["application/x-vnd.oasis.opendocument.spreadsheet"]
case "pdf":
extension = ".pdf"
downloadUrl = info.ExportLinks["application/pdf"]
case "rtf":
extension = ".rtf"
downloadUrl = info.ExportLinks["application/rtf"]
case "csv":
extension = ".csv"
downloadUrl = info.ExportLinks["text/csv"]
case "html":
extension = ".html"
downloadUrl = info.ExportLinks["text/html"]
case "txt":
extension = ".txt"
downloadUrl = info.ExportLinks["text/plain"]
case "json":
extension = ".json"
downloadUrl = info.ExportLinks["application/vnd.google-apps.script+json"]
default:
err = fmt.Errorf("Unknown export format: %s. Known formats: docx, xlsx, pptx, odf, ods, pdf, rtf, csv, txt, html, json", format)
}
return
func InternalDownloadUrlAndExtension(info *drive.File, format string) (downloadUrl string, extension string, err error) {
// Make a list of available mime types for this file
availableMimeTypes := make([]string, 0)
for mime, _ := range info.ExportLinks {
availableMimeTypes = append(availableMimeTypes, mime)
}

mimeExtensions := map[string]string{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
"application/application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
"application/vnd.oasis.opendocument.text": "odf",
"application/x-vnd.oasis.opendocument.spreadsheet": "ods",
"application/pdf": "pdf",
"application/rtf": "rtf",
"text/csv": "csv",
"text/html": "html",
"text/plain": "txt",
"application/vnd.google-apps.script+json": "json",
}

// Make a list of available formats for this file
availableFormats := make([]string, 0)
for _, mime := range availableMimeTypes {
if ext, ok := mimeExtensions[mime]; ok {
availableFormats = append(availableFormats, ext)
}
}

// Return DownloadUrl if no format is specified
if format == "" {
if info.DownloadUrl == "" {
return "", "", fmt.Errorf("A format needs to be specified to download this file (--format). Available formats: %s", strings.Join(availableFormats, ", "))
}
return info.DownloadUrl, "", nil
}

// Ensure that the specified format is available
if !inArray(format, availableFormats) {
if len(availableFormats) > 0 {
return "", "", fmt.Errorf("Invalid format. Available formats: %s", strings.Join(availableFormats, ", "))
} else {
return "", "", fmt.Errorf("No export formats are available for this file")
}
}

// Grab download url
for mime, f := range mimeExtensions {
if f == format {
downloadUrl = info.ExportLinks[mime]
break
}
}

extension = "." + format
return
}
10 changes: 10 additions & 0 deletions util/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,13 @@ func columnPadder(items []map[string]string, key string, spacing int) func(strin
return column
}
}

func inArray(needle string, haystack []string) bool {
for _, x := range haystack {
if needle == x {
return true
}
}

return false
}

0 comments on commit dd1adb7

Please sign in to comment.