-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpamphlet.go
69 lines (57 loc) · 1.41 KB
/
pamphlet.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
package pamphlet
import (
"archive/zip"
"bytes"
"github.com/timsims/pamphlet/epub"
"io"
"os"
)
// NewParser
// Deprecated: use Open instead
func NewParser(filePath string) (*Parser, error) {
return Open(filePath)
}
// Open open epub by file path
func Open(path string) (*Parser, error) {
result, err := zip.OpenReader(path)
if err != nil {
return nil, ErrOpenEpub
}
parser := &Parser{
zipReader: &ZipReadCloser{result},
toc: make(map[string]epub.NavPoint),
manifestFiles: make(map[string]ManifestFile),
}
err = parser.parse()
if err != nil {
return nil, err
}
return parser, nil
}
// OpenFile open epub by file pointer
// use this function if you have the epub file in memory, eg: from http request
func OpenFile(file *os.File) (*Parser, error) {
fileBytes, err := io.ReadAll(file)
if err != nil {
return nil, ErrOpenEpub
}
return OpenBytes(fileBytes)
}
// OpenBytes open epub by byte slice
// use this function if you have the epub file in memory and you don't have file pointer
func OpenBytes(data []byte) (*Parser, error) {
result, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, ErrOpenEpub
}
parser := &Parser{
zipReader: &ZipReader{result},
toc: make(map[string]epub.NavPoint),
manifestFiles: make(map[string]ManifestFile),
}
err = parser.parse()
if err != nil {
return nil, err
}
return parser, nil
}