-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopf.go
229 lines (190 loc) · 5.48 KB
/
opf.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
package gopub
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"path"
"strings"
)
type Identifier struct {
ID string `xml:"id,attr"`
Value string `xml:",chardata"`
}
type Title struct {
ID string `xml:"id,attr"`
Value string `xml:",chardata"`
}
type Creator struct {
ID string `xml:"id,attr"`
Value string `xml:",chardata"`
}
type Metadata struct {
Identifiers []Identifier `xml:"identifier"`
Titles []Title `xml:"title"`
Languages []string `xml:"language"`
Contributers []string `xml:contributor`
Coverages []string `xml:coverage`
Creators []Creator `xml:"creator"`
Dates []string `xml:"date"`
Descriptions []string `xml:"description"`
Formats []string `xml:"format"`
Publishers []string `xml:"publisher"`
Relations []string `xml:"relation"`
Rights []string `xml:"right"`
Sources []string `xml:"source"`
Subjects []string `xml:"subject"`
Types []string `xml:"types"`
}
func (m Metadata) getCreators() []string {
var creators []string
for _, creator := range m.Creators {
creators = append(creators, creator.Value)
}
return creators
}
func (m Metadata) getFirstOrDefaultDescription() string {
if len(m.Descriptions) > 0 {
return m.Descriptions[0]
} else {
return ""
}
}
type ManifestItem struct {
ID string `xml:"id,attr"`
Href string `xml:"href,attr"`
MediaType string `xml:"media-type,attr"`
MediaOverlay string `xml:"media-overlay,attr"`
RequiredNamespace string `xml:"required-namespace,attr"`
RequiredModules string `xml:"required-modules,attr"`
Fallback string `xml:"fallback,attr"`
FallbackStyle string `xml:"fallback-style,attr"`
Properties string `xml:"properties,attr"`
}
type Manifest struct {
Items []ManifestItem `xml:"item"`
}
func (m Manifest) getCoverImage(r *zip.ReadCloser) ([]byte, error) {
for _, item := range m.Items {
if strings.Contains(item.Properties, "cover-image") {
rc, size, err := findFileInZip(r, path.Join("EPUB", item.Href))
if err != nil {
return nil, err
}
defer rc.Close()
image := make([]byte, size)
_, err = io.ReadFull(rc, image)
if err != nil {
if err != io.EOF {
return nil, err
}
}
return image, nil
}
}
return nil, fmt.Errorf("cover image not found")
}
func (m Manifest) getNavigationFilePath() (string, error) {
for _, item := range m.Items {
if strings.Contains(item.Properties, "nav") {
return item.Href, nil
}
}
return "", fmt.Errorf("navigation file not found")
}
type ItemRef struct {
ID string `xml:"id,attr"`
IdRef string `xml:"idref,attr"`
Linear string `xml:"linear,attr"`
Properties string `xml:"properties"`
}
type Spine struct {
ItemRefs []ItemRef `xml:"itemref"`
}
type Reference struct {
Type string `xml:"type,attr"`
Title string `xml:"title,attr"`
Href string `xml:"href,attr"`
}
type Guide struct {
References []Reference `xml:"reference"`
}
type Link struct {
ID string `xml:"id"`
Href string `xml:"href"`
MediaType string `xml:"media-type"`
}
type Collection struct {
ID string `xml:"id,attr"`
Role string `xml:"role,attr"`
Language string `xml:"language,attr"`
Metadata Metadata `xml:"metadata"`
Collections []Collection `xml:"collections"`
Links []Link `xml:"link"`
}
type Package struct {
XMLName xml.Name `xml:"package"`
UniqueIdentifier string `xml:"unique-identifier,attr"`
Version string `xml:"version,attr"`
Metadata Metadata `xml:"metadata"`
Manifest Manifest `xml:"manifest"`
Spine Spine `xml:"spine"`
Guide Guide `xml:"guide"`
Collections []Collection `xml:"collection"`
}
func ReadPackage(r *zip.ReadCloser, rootfilePath string) (Package, error) {
var pkg Package
rc, _, err := findFileInZip(r, rootfilePath)
if err != nil {
return pkg, err
}
defer rc.Close()
if err := xml.NewDecoder(rc).Decode(&pkg); err != nil {
return pkg, err
}
return pkg, nil
}
type schema struct {
pkg Package
navigationDocument NavigationDocument
contentDirectoryPath string
}
func readSchema(er epubReader) (schema, error) {
var schema schema
pkg, err := ReadPackage(er.zipReader, er.options.rootFilePath)
if err != nil {
return schema, err
}
schema.pkg = pkg
schema.contentDirectoryPath = path.Dir(er.options.rootFilePath)
return schema, nil
}
func (s schema) getReadingOrder(er epubReader) ([]LocalTextContentFile, error) {
var readingOrder []LocalTextContentFile
for _, itemRef := range s.pkg.Spine.ItemRefs {
for _, manifestItem := range s.pkg.Manifest.Items {
if manifestItem.ID == itemRef.IdRef {
contentFile := newContentFile(manifestItem, s.contentDirectoryPath, ContentLocationLocal, ContentFileTypeText)
contentFilePath := path.Join(s.contentDirectoryPath, manifestItem.Href)
localTextContentFile, err := newLocalTextContentFile(er, contentFile, contentFilePath)
if err != nil {
return nil, err
}
readingOrder = append(readingOrder, localTextContentFile)
}
}
}
return readingOrder, nil
}
func findFileInZip(r *zip.ReadCloser, filename string) (io.ReadCloser, uint64, error) {
for _, f := range r.File {
if f.Name == filename {
rc, err := f.Open()
if err != nil {
return nil, 0, err
}
return rc, f.UncompressedSize64, nil
}
}
return nil, 0, fmt.Errorf("file not found in EPUB: %s", filename)
}