-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
reader.go
263 lines (239 loc) · 6.51 KB
/
reader.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package opc
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"os"
"path"
"strings"
)
type archiveFile interface {
Open() (io.ReadCloser, error)
Name() string
Size() int
}
type archive interface {
Files() []archiveFile
RegisterDecompressor(method uint16, dcomp func(r io.Reader) io.ReadCloser)
}
// ReadCloser wrapps a Reader than can be closed.
type ReadCloser struct {
f *os.File
*Reader
}
// OpenReader will open the OPC file specified by name and return a ReadCloser.
func OpenReader(name string) (*ReadCloser, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
r, err := NewReader(f, fi.Size())
return &ReadCloser{f: f, Reader: r}, err
}
// Close closes the OPC file, rendering it unusable for I/O.
func (r *ReadCloser) Close() error {
return r.f.Close()
}
// File is used to read a part from the OPC package.
type File struct {
*Part
Size int
a archiveFile
}
// Open returns a ReadCloser that provides access to the File's contents.
// Multiple files may be read concurrently.
func (f *File) Open() (io.ReadCloser, error) {
return f.a.Open()
}
// Reader implements a OPC file reader.
type Reader struct {
Files []*File
Relationships []*Relationship
Properties CoreProperties
p *pkg
r archive
}
// NewReader returns a new Reader reading an OPC file to r.
func NewReader(r io.ReaderAt, size int64) (*Reader, error) {
zr, err := newZipReader(r, size)
if err != nil {
return nil, err
}
return newReader(zr)
}
// newReader returns a new Reader reading an OPC file to r.
func newReader(a archive) (*Reader, error) {
r := &Reader{p: newPackage(), r: a}
if err := r.loadPackage(); err != nil {
return nil, err
}
return r, nil
}
// SetDecompressor sets or overrides a custom decompressor for the DEFLATE.
func (r *Reader) SetDecompressor(dcomp func(r io.Reader) io.ReadCloser) {
r.r.RegisterDecompressor(zip.Deflate, dcomp)
}
func (r *Reader) loadPackage() error {
ct, rels, err := r.loadPartProperties()
if err != nil {
return err
}
files := r.r.Files()
r.Files = make([]*File, 0, len(files)-1) // -1 is for [Content_Types].xml
for _, file := range files {
fileName := "/" + file.Name()
// skip content types part, relationship parts and directories
if strings.EqualFold(fileName, contentTypesName) || isRelationshipURI(fileName) || strings.HasSuffix(fileName, "/") {
continue
}
if strings.EqualFold(fileName, ResolveRelationship("/", r.Properties.PartName)) {
err := r.loadCoreProperties(file)
if err != nil {
return err
}
} else {
cType, err := ct.findType(NormalizePartName(fileName))
if err != nil {
return err
}
part := &Part{Name: fileName, ContentType: cType, Relationships: rels.findRelationship(fileName)}
r.Files = append(r.Files, &File{part, file.Size(), file})
if err = r.p.add(part); err != nil {
return err
}
}
}
r.p.contentTypes = *ct
return nil
}
func (r *Reader) loadPartProperties() (*contentTypes, *relationshipsPart, error) {
var ct *contentTypes
rels := new(relationshipsPart)
for _, file := range r.r.Files() {
var err error
name := "/" + file.Name()
if strings.EqualFold(name, contentTypesName) {
ct, err = r.loadContentType(file)
} else if isRelationshipURI(name) {
if strings.EqualFold(name, packageRelName) {
err = r.loadPackageRelationships(file)
} else {
err = loadRelationships(file, rels)
}
}
if err != nil {
return nil, nil, err
}
}
if ct == nil {
return nil, nil, newError(310, "/")
}
return ct, rels, nil
}
func (r *Reader) loadContentType(file archiveFile) (*contentTypes, error) {
// Process descrived in ISO/IEC 29500-2 §10.1.2.4
reader, err := file.Open()
if err != nil {
return nil, fmt.Errorf("opc: %s: cannot be opened: %v", contentTypesName, err)
}
return decodeContentTypes(reader)
}
func (r *Reader) loadCoreProperties(file archiveFile) error {
reader, err := file.Open()
if err != nil {
return fmt.Errorf("opc: %s: cannot be opened: %v", r.Properties.PartName, err)
}
return decodeCoreProperties(reader, &r.Properties)
}
func loadRelationships(file archiveFile, rels *relationshipsPart) error {
reader, err := file.Open()
if err != nil {
return fmt.Errorf("opc: %s: cannot be opened: %v", file.Name(), err)
}
rls, err := decodeRelationships(reader, file.Name())
if err != nil {
return err
}
// get part name from rels parts
name := path.Dir(path.Dir(file.Name()))
pname := "/" + name + "/" + strings.TrimSuffix(path.Base(file.Name()), path.Ext(file.Name()))
pname = NormalizePartName(pname)
rels.addRelationship(pname, rls)
return nil
}
func (r *Reader) loadPackageRelationships(file archiveFile) error {
reader, err := file.Open()
if err != nil {
return fmt.Errorf("opc: %s: cannot be opened: %v", file.Name(), err)
}
rls, err := decodeRelationships(reader, file.Name())
if err != nil {
return err
}
r.Relationships = rls
for _, rel := range rls {
if strings.EqualFold(rel.Type, corePropsRel) {
r.Properties.PartName = rel.TargetURI
r.Properties.RelationshipID = rel.ID
break
}
}
return nil
}
type contentTypesXMLReader struct {
XMLName xml.Name `xml:"Types"`
XML string `xml:"xmlns,attr"`
Types []mixed `xml:",any"`
}
type mixed struct {
Value interface{}
}
func (m *mixed) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch start.Name.Local {
case "Override":
var e overrideContentTypeXML
if err := d.DecodeElement(&e, &start); err != nil {
return err
}
m.Value = e
case "Default":
var e defaultContentTypeXML
if err := d.DecodeElement(&e, &start); err != nil {
return err
}
m.Value = e
}
return nil
}
func decodeContentTypes(r io.Reader) (*contentTypes, error) {
ctdecode := new(contentTypesXMLReader)
if err := xml.NewDecoder(r).Decode(ctdecode); err != nil {
return nil, fmt.Errorf("opc: %s: cannot be decoded: %v", contentTypesName, err)
}
ct := new(contentTypes)
for _, c := range ctdecode.Types {
if cDefault, ok := c.Value.(defaultContentTypeXML); ok {
ext := strings.ToLower(cDefault.Extension)
if ext == "" {
return nil, newError(206, "/")
}
if _, ok := ct.defaults[ext]; ok {
return nil, newError(205, "/")
}
ct.addDefault(ext, cDefault.ContentType)
} else if cOverride, ok := c.Value.(overrideContentTypeXML); ok {
partName := strings.ToUpper(NormalizePartName(cOverride.PartName))
if _, ok := ct.overrides[partName]; ok {
return nil, newError(205, partName)
}
ct.addOverride(partName, cOverride.ContentType)
}
}
return ct, nil
}