Update Pages converter to return errors.

This commit is contained in:
David Howden
2015-08-10 09:03:40 +10:00
parent 5dca922378
commit b9debed3c3
2 changed files with 7 additions and 9 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ func convert(r io.Reader, mimeType string, readability bool) *Response {
body, meta = ConvertODT(r)
case "application/vnd.apple.pages", "application/x-iwork-pages-sffpages":
body, meta = ConvertPages(r)
body, meta, err = ConvertPages(r)
case "application/pdf":
body, meta = ConvertPDF(r)
+6 -8
View File
@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"github.com/golang/protobuf/proto"
@@ -18,27 +17,26 @@ import (
)
// Convert PAGES to text
func ConvertPages(r io.Reader) (string, map[string]string) {
func ConvertPages(r io.Reader) (string, map[string]string, error) {
meta := make(map[string]string)
var textBody string
b, err := ioutil.ReadAll(r)
if err != nil {
log.Println("ioutil.ReadAll:", err)
return "", nil
return "", nil, fmt.Errorf("error reading data: %v", err)
}
zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
if err != nil {
log.Println("zip.NewReader:", err)
return "", nil
return "", nil, fmt.Errorf("error unzipping data: %v", err)
}
for _, f := range zr.File {
if strings.HasSuffix(f.Name, "Preview.pdf") {
// There is a preview PDF version we can use
if rc, err := f.Open(); err == nil {
return ConvertPDF(rc)
textBody, meta := ConvertPDF(rc)
return textBody, meta, nil
}
}
if f.Name == "index.xml" {
@@ -59,5 +57,5 @@ func ConvertPages(r io.Reader) (string, map[string]string) {
}
}
return textBody, meta
return textBody, meta, nil
}