mirror of
https://github.com/sajari/docconv.git
synced 2026-10-17 05:30:56 +08:00
Added in support for converting ODT documents #9
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Convert ODT to text
|
||||
func ConvertOdt(input io.Reader) (string, map[string]string) {
|
||||
meta := make(map[string]string)
|
||||
var textBody string
|
||||
|
||||
inputBytes, err := ioutil.ReadAll(input)
|
||||
if err != nil {
|
||||
log.Println("ioutil.ReadAll:", err)
|
||||
return "", nil
|
||||
}
|
||||
r, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes)))
|
||||
if err != nil {
|
||||
log.Println("zip.NewReader:", err)
|
||||
return "", nil
|
||||
}
|
||||
|
||||
for _, f := range r.File {
|
||||
if f.Name == "meta.xml" {
|
||||
rc, _ := f.Open()
|
||||
defer rc.Close()
|
||||
info := Xml2Map(rc)
|
||||
if tmp, ok := info["creator"]; ok {
|
||||
meta["Author"] = tmp
|
||||
}
|
||||
if tmp, ok := info["date"]; ok {
|
||||
if t, err := time.Parse("2006-01-02T15:04:05", tmp); err == nil {
|
||||
meta["ModifiedDate"] = fmt.Sprintf("%d", t.Unix())
|
||||
}
|
||||
}
|
||||
if tmp, ok := info["creation-date"]; ok {
|
||||
if t, err := time.Parse("2006-01-02T15:04:05", tmp); err == nil {
|
||||
meta["CreatedDate"] = fmt.Sprintf("%d", t.Unix())
|
||||
}
|
||||
}
|
||||
} else if f.Name == "content.xml" {
|
||||
rc, _ := f.Open()
|
||||
defer rc.Close()
|
||||
textBody = OdtXml2Text(rc)
|
||||
}
|
||||
}
|
||||
|
||||
return textBody, meta
|
||||
}
|
||||
|
||||
func OdtXml2Text(input io.Reader) string {
|
||||
return Xml2Text(input, []string{"br", "p", "tab"}, []string{}, true)
|
||||
}
|
||||
@@ -42,6 +42,8 @@ func mimeTypeByExtension(filename string) string {
|
||||
return "application/msword"
|
||||
case "docx":
|
||||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
case "odt":
|
||||
return "application/vnd.oasis.opendocument.text"
|
||||
case "pdf":
|
||||
return "application/pdf"
|
||||
case "rtf":
|
||||
@@ -69,6 +71,9 @@ func convert(input io.Reader, mimeType string, readability bool) *Response {
|
||||
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
response.Body, response.Meta = ConvertDocx(input)
|
||||
|
||||
case "application/vnd.oasis.opendocument.text":
|
||||
response.Body, response.Meta = ConvertOdt(input)
|
||||
|
||||
case "application/pdf":
|
||||
response.Body, response.Meta = ConvertPdf(input)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user