forked from pgaskin/BookBrowser
/
book.go
167 lines (148 loc) · 3.72 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
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
package main
import (
"archive/zip"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"time"
"strings"
"github.com/beevik/etree"
"github.com/kennygrant/sanitize"
"golang.org/x/tools/godoc/vfs/zipfs"
)
func fix(s string, capitalize, correctOrder bool) string {
if s == "" {
return "Unknown"
}
if capitalize {
s = strings.Title(strings.ToLower(s))
s = strings.Replace(s, "'S", "'s", -1)
}
if correctOrder && strings.Contains(s, ",") {
sParts := strings.Split(s, ",")
if len(sParts) == 2 {
s = strings.TrimSpace(sParts[1]) + " " + strings.TrimSpace(sParts[0])
}
}
return strings.Map(func(in rune) rune {
switch in {
case '“', '‹', '”', '›':
return '"'
case '_':
return ' '
case '‘', '’':
return '\''
}
return in
}, s)
}
// Book represents a book
type Book struct {
ID string `json:"id" storm:"id"`
Title string `json:"title" storm:"index"`
Author string `json:"author" storm:"index"`
Description string `json:"description,omitempty"`
Filepath string `json:"filepath" storm:"index"`
Filename string `json:"filename" storm:"index"`
HasMobi bool `json:"hasmobi"`
LastSeen int64 `storm:"index"`
}
// NewBookFromFile creates a book object from a file
func NewBookFromFile(path string) (bk *Book, err error) {
defer func() {
if r := recover(); r != nil {
bk = nil
err = fmt.Errorf("Unknown error parsing book. Skipping. Error: %s", r)
}
}()
book := new(Book)
book.LastSeen = time.Now().UnixNano()
book.Title = filepath.Base(path)
book.Filename = filepath.Base(path)
book.Filepath = path
mobiPath := strings.Replace(path, "epub", "mobi", -1)
_, err = os.Stat(mobiPath)
book.HasMobi = !os.IsNotExist(err)
zr, err := zip.OpenReader(path)
if err != nil {
return nil, err
}
zfs := zipfs.New(zr, "epub")
rsk, err := zfs.Open("/META-INF/container.xml")
if err != nil {
return nil, err
}
defer rsk.Close()
container := etree.NewDocument()
_, err = container.ReadFrom(rsk)
if err != nil {
return nil, err
}
rootfile := ""
for _, e := range container.FindElements("//rootfiles/rootfile[@full-path]") {
rootfile = e.SelectAttrValue("full-path", "")
}
if rootfile == "" {
return nil, errors.New("Cannot parse container")
}
rootReadSeeker, err := zfs.Open("/" + rootfile)
if err != nil {
return nil, err
}
defer rootReadSeeker.Close()
opf := etree.NewDocument()
_, err = opf.ReadFrom(rootReadSeeker)
if err != nil {
return nil, err
}
book.Title = filepath.Base(path)
for _, e := range opf.FindElements("//title") {
book.Title = e.Text()
break
}
for _, e := range opf.FindElements("//creator") {
book.Author = e.Text()
break
}
for _, e := range opf.FindElements("//description") {
book.Description = e.Text()
break
}
book.Title = fix(book.Title, true, false)
book.Author = fix(book.Author, true, true)
book.Description = fix(book.Description, false, false)
book.Description = sanitize.HTML(book.Description)
id := sha1.New()
io.WriteString(id, book.Author)
io.WriteString(id, book.Title)
book.ID = hex.EncodeToString(id.Sum(nil))[:10]
return book, nil
}
// BookList is a list of books
type BookList []Book
// Sorted returns a copy of the BookList sorted by the function
func (l *BookList) Sorted(sorter func(a, b Book) bool) BookList {
// Make a copy
sorted := make(BookList, len(*l))
copy(sorted, *l)
// Sort the copy
sort.Slice(sorted, func(i, j int) bool {
return sorter(sorted[i], sorted[j])
})
return sorted
}
// Filtered returns a copy of the BookList filtered by the function
func (l *BookList) Filtered(filterer func(a Book) bool) *BookList {
filtered := BookList{}
for _, a := range *l {
if filterer(a) {
filtered = append(filtered, a)
}
}
return &filtered
}