forked from gonfva/docxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.go
117 lines (107 loc) · 2.79 KB
/
unpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package docx
// This contains internal functions needed to unpack (read) a zip file
import (
"archive/zip"
"encoding/xml"
"io/ioutil"
"github.com/sirupsen/logrus"
)
// This receives a zip file (word documents are a zip with multiple xml inside)
// and parses the files that are relevant for us:
// 1.-Document
// 2.-Relationships
func unpack(zipReader *zip.Reader) (docx *DocxFile, err error) {
var doc *Document
var relations *Relationships
for _, f := range zipReader.File {
if f.Name == "word/_rels/document.xml.rels" {
relations, err = processRelations(f)
if err != nil {
return nil, err
}
}
if f.Name == "word/document.xml" {
doc, err = processDoc(f)
if err != nil {
return nil, err
}
}
if f.Name == "word/styles.xml" {
styles, err := processStyles(f)
if err != nil {
return nil, err
}
doc.Styles = styles
}
}
docx = &DocxFile{
Document: *doc,
DocRelation: *relations,
}
for _, para := range doc.Body.Paragraphs {
para.file = docx
}
return docx, nil
}
func processStyles(file *zip.File) (*DocumentStyle, error) {
filebytes, err := readZipFile(file)
if err != nil {
logrus.Errorln("Error reading from internal zip file")
return nil, err
}
doc := DocumentStyle{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "styles"}}
err = xml.Unmarshal(filebytes, &doc)
if err != nil {
logrus.Errorln("Error unmarshalling doc style", string(filebytes))
return nil, err
}
return &doc, nil
}
// Processes one of the relevant files, the one with the actual document
func processDoc(file *zip.File) (*Document, error) {
filebytes, err := readZipFile(file)
if err != nil {
logrus.Errorln("Error reading from internal zip file")
return nil, err
}
logrus.Errorln("Doc:", string(filebytes))
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
err = xml.Unmarshal(filebytes, &doc)
if err != nil {
logrus.Errorln("Error unmarshalling doc", string(filebytes))
return nil, err
}
logrus.Debugln("Paragraph", doc.Body.Paragraphs)
return &doc, nil
}
// Processes one of the relevant files, the one with the relationships
func processRelations(file *zip.File) (*Relationships, error) {
filebytes, err := readZipFile(file)
if err != nil {
logrus.Errorln("Error reading from internal zip file")
return nil, err
}
logrus.Errorln("Relations:", string(filebytes))
rels := Relationships{Xmlns: XMLNS_R}
err = xml.Unmarshal(filebytes, &rels)
if err != nil {
logrus.Errorln("Error unmarshalling relationships")
return nil, err
}
return &rels, nil
}
// From a zip file structure, we return a byte array
func readZipFile(zf *zip.File) ([]byte, error) {
f, err := zf.Open()
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}