-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathepub.go
168 lines (147 loc) · 3.58 KB
/
epub.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
// Copyright 2012 Ruben Pollan <[email protected]>
// Use of this source code is governed by a LGPL licence
// version 3 or later that can be found in the LICENSE file.
package epubgo
import (
"archive/zip"
"errors"
"io"
"os"
)
// Epub holds all the data of the ebook
type Epub struct {
file *os.File
zip *zip.Reader
rootPath string
metadata mdata
opf *xmlOPF
ncx *xmlNCX
}
type mdata map[string][]mdataElement
type mdataElement struct {
content string
attr map[string]string
}
// Open opens an existing epub
func Open(path string) (e *Epub, err error) {
e = new(Epub)
e.file, err = os.Open(path)
if err != nil {
return
}
fileInfo, err := e.file.Stat()
if err != nil {
return
}
err = e.load(e.file, fileInfo.Size())
return
}
// Load loads an epub from an io.ReaderAt
func Load(r io.ReaderAt, size int64) (e *Epub, err error) {
e = new(Epub)
e.file = nil
err = e.load(r, size)
return
}
func (e *Epub) load(r io.ReaderAt, size int64) (err error) {
e.zip, err = zip.NewReader(r, size)
if err != nil {
return
}
e.rootPath, err = getRootPath(e.zip)
if err != nil {
return
}
return e.parseFiles()
}
func (e *Epub) parseFiles() (err error) {
opfFile, err := openOPF(e.zip)
if err != nil {
return
}
defer opfFile.Close()
e.opf, err = parseOPF(opfFile)
if err != nil {
return
}
e.metadata = e.opf.toMData()
ncxPath := e.opf.ncxPath()
if ncxPath != "" {
ncx, err := e.OpenFile(ncxPath)
if err != nil {
return errors.New("Can't open the NCX file")
}
defer ncx.Close()
e.ncx, err = parseNCX(ncx)
}
return
}
// Close closes the epub file
func (e Epub) Close() {
if e.file != nil {
e.file.Close()
}
}
// OpenFile opens a file inside the epub
func (e Epub) OpenFile(name string) (io.ReadCloser, error) {
return openFile(e.zip, e.rootPath+name)
}
// OpenFileId opens a file from it's id
//
// The id of the files often appears on metadata fields
func (e Epub) OpenFileId(id string) (io.ReadCloser, error) {
path := e.opf.filePath(id)
return openFile(e.zip, e.rootPath+path)
}
// Navigation returns a navigation iterator
func (e Epub) Navigation() (*NavigationIterator, error) {
if e.ncx == nil {
return nil, errors.New("There is no NCX file on the epub")
}
return newNavigationIterator(e.ncx.navMap())
}
// Spine returns a spine iterator
func (e Epub) Spine() (*SpineIterator, error) {
return newSpineIterator(&e)
}
// Metadata returns the values of a metadata field
//
// The valid field names are:
// title, language, identifier, creator, subject, description, publisher,
// contributor, date, type, format, source, relation, coverage, rights, meta
func (e Epub) Metadata(field string) ([]string, error) {
elem, ok := e.metadata[field]
if ok {
cont := make([]string, len(elem))
for i, e := range elem {
cont[i] = e.content
}
return cont, nil
}
return nil, errors.New("Field " + field + " don't exists")
}
// MetadataFields retunrs the list of metadata fields pressent on the current epub
func (e Epub) MetadataFields() []string {
fields := make([]string, len(e.metadata))
i := 0
for k, _ := range e.metadata {
fields[i] = k
i++
}
return fields
}
// MetadataAttr returns the metadata attributes
//
// Returns: an array of maps of each attribute and it's value.
// The array has the fields on the same order than the Metadata method.
func (e Epub) MetadataAttr(field string) ([]map[string]string, error) {
elem, ok := e.metadata[field]
if ok {
attr := make([]map[string]string, len(elem))
for i, e := range elem {
attr[i] = e.attr
}
return attr, nil
}
return nil, errors.New("Field " + field + " don't exists")
}