forked from kapmahc/epub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbook.go
97 lines (80 loc) · 1.77 KB
/
book.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
package epub
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
)
// Book epub book
type Book struct {
Ncx Ncx `json:"ncx"`
Opf Opf `json:"opf"`
Container Container `json:"-"`
Mimetype string `json:"-"`
Encryption Encryption `json:"-"`
fd *zip.ReadCloser
directory string
}
//Open open resource file
func (p *Book) Open(n string) (io.ReadCloser, error) {
return p.open(p.filename(n))
}
//Files list resource files
func (p *Book) Files() []string {
var fns []string
for _, f := range p.fd.File {
fns = append(fns, f.Name)
}
return fns
}
//Close close file reader
func (p *Book) Close() {
p.fd.Close()
}
//-----------------------------------------------------------------------------
func (p *Book) filename(n string) string {
return path.Join(path.Dir(p.Container.Rootfile.Path), n)
}
func (p *Book) readXML(n string, v interface{}) error {
fd, err := p.open(n)
if err != nil {
return nil
}
defer fd.Close()
dec := xml.NewDecoder(fd)
return dec.Decode(v)
}
func (p *Book) readBytes(n string) ([]byte, error) {
fd, err := p.open(n)
if err != nil {
return nil, nil
}
defer fd.Close()
return ioutil.ReadAll(fd)
}
func (p *Book) open(n string) (io.ReadCloser, error) {
if p.directory != "" {
filename := path.Join(path.Dir(p.directory+string(filepath.Separator)), n)
return os.Open(filename)
}
for _, f := range p.fd.File {
if f.Name == n {
return f.Open()
}
}
return nil, fmt.Errorf("file %s not exist", n)
}
// ZipReader return the internal file descriptor
func (p *Book) ZipReader() *zip.ReadCloser {
return p.fd
}
// GetSMIL parse and return SMIL structure
func (p *Book) GetSMIL(ressouce string) SMIL {
var smil SMIL
p.readXML(ressouce, &smil)
return smil
}