-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
55 lines (50 loc) · 978 Bytes
/
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
package captcha
import (
"image"
"io/fs"
"math/rand"
"os"
"path"
"path/filepath"
)
// Provider 图片提供接口
type Provider interface {
Get() (image.Image, error)
}
type fsProvider struct {
dir string
items []fs.DirEntry
}
// LoadDirImage 加载目录中的图片作为 Provider
func LoadDirImage(dirPath string) (Provider, error) {
dirPath, err := filepath.Abs(dirPath)
if err != nil {
return nil, err
}
dir, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
var p = fsProvider{
dir: dirPath,
items: make([]fs.DirEntry, 0, len(dir)),
}
for _, file := range dir {
if file.IsDir() {
continue
}
p.items = append(p.items, file)
}
return &p, err
}
func (f *fsProvider) Get() (image.Image, error) {
index := rand.Intn(len(f.items))
item := f.items[index]
file, err := os.Open(path.Join(f.dir, item.Name()))
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
return img, err
}