Update ConvertDoc and ConvertDocx to return errors.

This commit is contained in:
David Howden
2015-08-10 09:48:20 +10:00
parent d5a2aa9a5c
commit cfd468ca2c
3 changed files with 56 additions and 24 deletions
+9 -4
View File
@@ -13,11 +13,10 @@ import (
)
// Convert MS Word DOC
func ConvertDoc(r io.Reader) (string, map[string]string) {
func ConvertDoc(r io.Reader) (string, map[string]string, error) {
f, err := NewLocalFile(r, "/tmp", "sajari-convert-")
if err != nil {
log.Println("error creating local file:", err)
return "", nil
return "", nil, fmt.Errorf("error creating local file: %v", err)
}
defer f.Done()
@@ -27,6 +26,7 @@ func ConvertDoc(r io.Reader) (string, map[string]string) {
meta := make(map[string]string)
metaStr, err := exec.Command("wvSummary", f.Name()).Output()
if err != nil {
// TODO: Remove this.
log.Println("wvSummary:", err)
}
@@ -60,6 +60,7 @@ func ConvertDoc(r io.Reader) (string, map[string]string) {
// Save output to a file
outputFile, err := ioutil.TempFile("/tmp", "sajari-convert-")
if err != nil {
// TODO: Remove this.
log.Println("TempFile Out:", err)
return
}
@@ -67,24 +68,28 @@ func ConvertDoc(r io.Reader) (string, map[string]string) {
err = exec.Command("wvText", f.Name(), outputFile.Name()).Run()
if err != nil {
// TODO: Remove this.
log.Println("wvText:", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(outputFile)
if err != nil {
// TODO: Remove this.
log.Println("wvText:", err)
}
bc <- buf.String()
}()
// TODO: Should errors in either of the above Goroutines stop things from progressing?
body := <-bc
meta := <-mc
// TODO: Check for errors instead of len(body) == 0?
if len(body) == 0 {
f.Seek(0, 0)
return ConvertDocx(f)
}
return body, meta
return body, meta, nil
}
+2 -2
View File
@@ -67,10 +67,10 @@ func convert(r io.Reader, mimeType string, readability bool) *Response {
var err error
switch mimeType {
case "application/msword", "application/vnd.ms-word":
body, meta = ConvertDoc(r)
body, meta, err = ConvertDoc(r)
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
body, meta = ConvertDocx(r)
body, meta, err = ConvertDocx(r)
case "application/vnd.oasis.opendocument.text":
body, meta = ConvertODT(r)
+45 -18
View File
@@ -6,25 +6,22 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"regexp"
"time"
)
// Convert DOCX to text
func ConvertDocx(r io.Reader) (string, map[string]string) {
func ConvertDocx(r io.Reader) (string, map[string]string, error) {
meta := make(map[string]string)
var textHeader, textBody, textFooter string
b, err := ioutil.ReadAll(r)
if err != nil {
log.Println("ioutil.ReadAll:", err)
return "", nil
return "", nil, 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)
}
// Regular expression for XML files to include in the text parsing
@@ -33,9 +30,17 @@ func ConvertDocx(r io.Reader) (string, map[string]string) {
for _, f := range zr.File {
if f.Name == "docProps/core.xml" {
rc, _ := f.Open()
rc, err := f.Open()
if err != nil {
return "", nil, fmt.Errorf("error opening '%v' from archive: %v", f.Name, err)
}
defer rc.Close()
info, _ := XMLToMap(rc)
info, err := XMLToMap(rc)
if err != nil {
return "", nil, fmt.Errorf("error parsing '%v': %v", f.Name, err)
}
if tmp, ok := info["modified"]; ok {
if t, err := time.Parse(time.RFC3339, tmp); err == nil {
meta["ModifiedDate"] = fmt.Sprintf("%d", t.Unix())
@@ -47,24 +52,46 @@ func ConvertDocx(r io.Reader) (string, map[string]string) {
}
}
} else if f.Name == "word/document.xml" {
rc, _ := f.Open()
rc, err := f.Open()
if err != nil {
return "", nil, fmt.Errorf("error opening '%v' from archive: %v", f.Name, err)
}
defer rc.Close()
textBody = DocxXMLToText(rc)
textBody, err = DocxXMLToText(rc)
if err != nil {
return "", nil, fmt.Errorf("error parsing '%v': %v", f.Name, err)
}
} else if reHeaderFile.MatchString(f.Name) {
rc, _ := f.Open()
rc, err := f.Open()
if err != nil {
return "", nil, fmt.Errorf("error opening '%v' from archive: %v", f.Name, err)
}
defer rc.Close()
textHeader += DocxXMLToText(rc) + "\n"
header, err := DocxXMLToText(rc)
if err != nil {
return "", nil, fmt.Errorf("error parsing header file '%v': %v", f.Name, err)
}
textHeader += header + "\n"
} else if reFooterFile.MatchString(f.Name) {
rc, _ := f.Open()
rc, err := f.Open()
if err != nil {
return "", nil, fmt.Errorf("error opening '%v' from archive: %v", f.Name, err)
}
defer rc.Close()
textFooter += DocxXMLToText(rc) + "\n"
footer, err := DocxXMLToText(rc)
if err != nil {
return "", nil, fmt.Errorf("error parsing footer file '%v': %v", f.Name, err)
}
textFooter += footer + "\n"
}
}
return textHeader + "\n" + textBody + "\n" + textFooter, meta
return textHeader + "\n" + textBody + "\n" + textFooter, meta, nil
}
func DocxXMLToText(r io.Reader) string {
result, _ := XMLToText(r, []string{"br", "p", "tab"}, []string{"instrText", "script"}, true)
return result
func DocxXMLToText(r io.Reader) (string, error) {
return XMLToText(r, []string{"br", "p", "tab"}, []string{"instrText", "script"}, true)
}