-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.go
96 lines (75 loc) · 1.88 KB
/
convert.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
package pico
import (
"github.com/pkg/errors"
)
// Convert converts single PDF to images. This function is solely a options parser
// and command builder
func Convert(pdf string, options ...CallOption) (*SingleTask, error) {
p := defaultConvertCallOption()
if err := p.apply(options...); err != nil {
return nil, errors.WithStack(err)
}
// 1. page calculation
pages, err := GetPagesCount(pdf, options...)
if err != nil {
return nil, errors.WithStack(err)
}
totalPage := int32(pages)
if p.singleFile {
p.firstPage = 1
p.lastPage = 1
}
if p.lastPage < 0 || p.lastPage > totalPage {
p.lastPage = totalPage
}
if p.firstPage > p.lastPage {
return nil, errors.WithStack(newWrongPageRangeError(p.firstPage, p.lastPage))
}
// 2. worker number calculation
p.pageCount = p.lastPage - p.firstPage + 1
// workerCount is not set, we could infer for one
if p.job <= 0 {
switch {
case p.pageCount > 50:
p.job = 3
case p.pageCount > 20:
p.job = 2
default:
p.job = 1
}
}
if p.job > p.pageCount {
p.job = p.pageCount
}
p.minPagesPerWorker = p.pageCount / p.job
task := newSingleTask(p)
return task, task.Start(pdf)
}
// ConvertFiles converts multiple PDF files to images
//
// files could be type `[]string`, `chan string`, or `PdfProvider`
func ConvertFiles(files interface{}, options ...CallOption) (*BatchTask, error) {
p := defaultConvertFilesCallOption()
if err := p.apply(options...); err != nil {
return nil, errors.WithStack(err)
}
provider := FromInterface(files)
// automatically determine worker count, perfer using 4 worker
p.job = determineWorkerCount(p.job, int32(provider.Count()))
task := newBatchTask(p)
return task, task.Start(provider)
}
func determineWorkerCount(set, need int32) int32 {
switch {
case set > 0:
return set
case need >= 20:
return 4
case need >= 10:
return 2
case need > 0:
return 1
default:
return 4
}
}