-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertor.go
246 lines (202 loc) · 4.91 KB
/
convertor.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package pico
import (
"bufio"
"io"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
type Convertor struct {
Progress
t *Task
id int32
cmd *exec.Cmd
// converrs is a list of errors that occurred during the conversion.
converrs []*ConversionError
done chan interface{}
aborted bool
}
// spwanCmdForPipe spwans an `exec.Cmd` for convererting the pdf from `first` to `last`,
//
func (c *Convertor) spwanCmdForPipe(pdf string, first, last int32) (io.ReadCloser, error) {
p := c.t.params
command := p.buildCommand(pdf, c.id, first, last)
c.cmd = buildCmd(p.ctx, p.popplerPath, command)
// cmd.Wait() will close the pipe
pipe, err := c.cmd.StderrPipe()
if err != nil {
return nil, errors.WithStack(err)
}
if err = c.cmd.Start(); err != nil {
return nil, errors.WithStack(err)
}
return pipe, nil
}
func (c *Convertor) Errors() []*ConversionError {
return c.converrs
}
func (c *Convertor) Error() (err error) {
if len(c.converrs) > 0 {
err = c.converrs[0].err
}
return
}
// receiveError 可能存在跨pdf error 的情况么?
func (c *Convertor) receiveError(err error, page int32) bool {
if err == nil {
return true
}
c.converrs = append(c.converrs, &ConversionError{
pdf: c.pdf,
page: page,
workerId: c.id,
err: err,
})
// if we're in `strict` mode, break further execution by return false
return !c.t.params.strict
}
// receiveEntry is called when an entry is received from the parser. This
// function is usually used to
// 1. update the progress
// 2. send the entry to the task
func (c *Convertor) receiveEntry(entry []string) {
current, _ := strconv.Atoi(entry[0])
c.Incr(1)
c.SetCurrent(int32(current))
c.t.Entries <- append(entry, strconv.Itoa(int(c.id)))
}
// current total outputFileName
var _entryRE = regexp.MustCompile(`(\d+) (\d+) (.+)`)
func (c *Convertor) parseProgress(pipe io.ReadCloser, ch chan<- []string, current int32) {
scanner := bufio.NewScanner(pipe)
defer close(ch)
for scanner.Scan() {
line := scanner.Text()
// should we continue other worker when error happens?
if strings.Contains(line, "Syntax Error") {
err := errors.WithStack(NewPDFSyntaxError(line))
if ok := c.receiveError(err, current); !ok {
return
}
}
// this is a critical error
if strings.HasSuffix(line, "; exiting") {
c.receiveError(errors.New(line), current)
return
}
if entry := _entryRE.FindStringSubmatch(line); len(entry) > 3 {
pg, _ := strconv.Atoi(entry[1])
current = int32(pg)
ch <- entry[1:]
}
}
c.cmd.Wait()
}
// start starts the convertor
//
// errors may occur during spwan Cmd and pipe.
func (c *Convertor) start(pdf string) error {
p := c.t.params
first, last, _ := p.pageRangeForPart(pdf, c.id)
pipe, err := c.spwanCmdForPipe(pdf, first, last)
if err != nil {
return errors.WithStack(err)
}
c.Progress.setInit(pdf, first, last)
// ch is closed by `parseProgress`
ch := make(chan []string, last-first+1)
go c.parseProgress(pipe, ch, first)
go func() {
defer c.onComplete()
for {
select {
case <-p.ctx.Done():
c.receiveError(errors.WithStack(p.ctx.Err()), -1)
c.aborted = true
return
case entry, more := <-ch:
if !more {
return
}
c.receiveEntry(entry)
}
}
}()
return nil
}
func (c *Convertor) startAsWorker(provider PdfProvider) {
var pdf string
var pipe io.ReadCloser
var more bool
var ch chan []string
defer c.onComplete()
p := c.t.params
// set the total number as long as we could get the file count from provider
if cnt := provider.Count(); cnt > 0 {
c.t.setInit("", 1, int32(cnt))
}
for {
if c.cmd == nil {
// accuquire a file for conversion
select {
case <-p.ctx.Done():
c.receiveError(errors.WithStack(p.ctx.Err()), -1)
c.aborted = true
return
case pdf, more = <-provider.Source():
if !more {
return
}
}
// page calculation, spwan cmd and pipe
first, last, err := p.pageRangeForFile(pdf, c.id)
if ok := c.receiveError(err, -1); !ok {
continue
}
pipe, err = c.spwanCmdForPipe(pdf, first, last)
if ok := c.receiveError(err, -1); !ok {
continue
}
// initialize new file conversion progress
c.setInit(pdf, first, last)
if provider.Count() == -1 {
c.t.PushTotal(1)
}
ch = make(chan []string, last-first+1)
go c.parseProgress(pipe, ch, first)
}
select {
case <-p.ctx.Done():
c.receiveError(errors.WithStack(p.ctx.Err()), c.current)
c.aborted = true
return
case entry, more := <-ch:
// no more entry means conversion has finised for that file
if !more {
c.cmd = nil
c.setWaiting()
c.t.Incr(1)
} else {
c.receiveEntry(entry)
}
}
}
}
func (c *Convertor) onComplete() {
close(c.done)
c.t.wg.Done()
}
// Completed reports whether the convertor is in completed state
func (c *Convertor) Completed() bool {
select {
case <-c.done:
return true
default:
return false
}
}
func (c *Convertor) Aborted() bool {
return false
}