forked from pgaskin/BookBrowser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
indexer.go
170 lines (141 loc) · 3.54 KB
/
indexer.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
package indexer
import (
"fmt"
"image/jpeg"
"log"
"os"
"path/filepath"
"sync"
"github.com/geek1011/BookBrowser/booklist"
"github.com/geek1011/BookBrowser/formats"
zglob "github.com/mattn/go-zglob"
"github.com/nfnt/resize"
"github.com/pkg/errors"
)
type Indexer struct {
Verbose bool
Progress float64
coverpath *string
paths []string
exts []string
booklist booklist.BookList
mu sync.Mutex
indMu sync.Mutex
}
func New(paths []string, coverpath *string, exts []string) (*Indexer, error) {
for i := range paths {
p, err := filepath.Abs(paths[i])
if err != nil {
return nil, errors.Wrap(err, "error resolving path")
}
paths[i] = p
}
cp := (*string)(nil)
if coverpath != nil {
p, err := filepath.Abs(*coverpath)
if err != nil {
return nil, errors.Wrap(err, "error resolving cover path")
}
cp = &p
}
return &Indexer{paths: paths, coverpath: cp, exts: exts}, nil
}
func (i *Indexer) Refresh() ([]error, error) {
i.indMu.Lock()
defer i.indMu.Unlock()
defer func() {
i.Progress = 0
}()
errs := []error{}
if len(i.paths) < 1 {
return errs, errors.New("no paths to index")
}
booklist := booklist.BookList{}
seen := map[string]bool{}
filenames := []string{}
for _, path := range i.paths {
for _, ext := range i.exts {
l, err := zglob.Glob(filepath.Join(path, "**", fmt.Sprintf("*.%s", ext)))
if l != nil {
filenames = append(filenames, l...)
}
if err != nil {
errs = append(errs, errors.Wrapf(err, "error scanning '%s' for type '%s'", path, ext))
if i.Verbose {
log.Printf("Error: %v", errs[len(errs)-1])
}
}
}
}
for fi, filepath := range filenames {
if i.Verbose {
log.Printf("Indexing %s", filepath)
}
book, err := i.getBook(filepath)
if err != nil {
errs = append(errs, errors.Wrapf(err, "error reading book '%s'", filepath))
if i.Verbose {
log.Printf("--> Error: %v", errs[len(errs)-1])
}
continue
}
if !seen[book.ID()] {
booklist = append(booklist, book)
seen[book.ID()] = true
}
i.Progress = float64(fi+1) / float64(len(filenames))
}
i.mu.Lock()
i.booklist = booklist
i.mu.Unlock()
return errs, nil
}
func (i *Indexer) BookList() booklist.BookList {
i.mu.Lock()
defer i.mu.Unlock()
return i.booklist
}
func (i *Indexer) getBook(filename string) (*booklist.Book, error) {
// TODO: caching
bi, err := formats.Load(filename)
if err != nil {
return nil, errors.Wrap(err, "error loading book")
}
b := bi.Book()
b.HasCover = false
if i.coverpath != nil && bi.HasCover() {
coverpath := filepath.Join(*i.coverpath, fmt.Sprintf("%s.jpg", b.ID()))
thumbpath := filepath.Join(*i.coverpath, fmt.Sprintf("%s_thumb.jpg", b.ID()))
_, err := os.Stat(coverpath)
_, errt := os.Stat(thumbpath)
if err != nil || errt != nil {
i, err := bi.GetCover()
if err != nil {
return nil, errors.Wrap(err, "error getting cover")
}
f, err := os.Create(coverpath)
if err != nil {
return nil, errors.Wrap(err, "could not create cover file")
}
defer f.Close()
err = jpeg.Encode(f, i, nil)
if err != nil {
os.Remove(coverpath)
return nil, errors.Wrap(err, "could not write cover file")
}
ti := resize.Thumbnail(400, 400, i, resize.Bicubic)
tf, err := os.Create(thumbpath)
if err != nil {
return nil, errors.Wrap(err, "could not create cover thumbnail file")
}
defer tf.Close()
err = jpeg.Encode(tf, ti, nil)
if err != nil {
os.Remove(coverpath)
return nil, errors.Wrap(err, "could not write cover thumbnail file")
}
}
b.HasCover = true
}
return b, nil
}