-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovider.go
108 lines (86 loc) · 1.77 KB
/
provider.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
package pico
import (
"os"
"path/filepath"
)
const _dirwalkchansize = 100
type PdfProvider interface {
Source() <-chan string
Count() int
}
type ChanProvider struct {
source chan string
}
type SliceFileProvider struct {
ChanProvider
len int
}
func (p *ChanProvider) Source() <-chan string {
return p.source
}
func (p *ChanProvider) Count() int {
return -1
}
func FromSlice(files []string) PdfProvider {
source := make(chan string, len(files))
defer close(source)
for _, file := range files {
source <- file
}
return &SliceFileProvider{
ChanProvider{source},
len(files),
}
}
func (p *SliceFileProvider) Count() int {
return p.len
}
func FromGlob(pattern string) PdfProvider {
files, _ := filepath.Glob(pattern)
return FromSlice(files)
}
func FromChan(ch chan string) PdfProvider {
return &ChanProvider{source: ch}
}
func FromMultiSource(patterns []string) PdfProvider {
files := []string{}
for _, pattern := range patterns {
info, _ := os.Stat(pattern)
if info.IsDir() {
pattern = filepath.Join(pattern, "/*.pdf")
}
batch, _ := filepath.Glob(pattern)
files = append(files, batch...)
}
return FromSlice(files)
}
func FromMultiSourceAsync(patterns []string) PdfProvider {
ch := make(chan string, _dirwalkchansize)
go func() {
defer close(ch)
for _, pattern := range patterns {
info, _ := os.Stat(pattern)
if info.IsDir() {
pattern = filepath.Join(pattern, "/*")
}
batch, _ := filepath.Glob(pattern)
for _, file := range batch {
ch <- file
}
}
}()
return FromChan(ch)
}
func FromInterface(i interface{}) PdfProvider {
switch i := i.(type) {
case PdfProvider:
return i
case string:
return FromGlob(i)
case []string:
return FromSlice(i)
case chan string:
return FromChan(i)
}
panic("unsupported type")
}