-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_loader.go
151 lines (117 loc) · 2.79 KB
/
asset_loader.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
package xcassets
import (
"fmt"
"image"
_ "image/jpeg" // support for JPG images
_ "image/png" // support for PNG images
"io"
"io/ioutil"
"log"
"net/http"
"path"
"path/filepath"
_ "golang.org/x/image/bmp" // support for BMP images
"os"
)
type assetLoader struct {
source AssetSource
width, height, maxScaleFactor int
}
func (l *assetLoader) Key(source AssetSource) string {
if path := source.file; path != "" {
return path
}
if url := source.url; url != "" {
return url
}
return ""
}
func (l *assetLoader) Load(source AssetSource) (image.Image, error) {
if path := source.file; path != "" {
img, err := l.loadImageFromFile(path)
return img, err
}
if url := l.source.url; url != "" {
img, err := l.loadImageFromURL(url)
return img, err
}
return nil, fmt.Errorf("No image source specified")
}
func (l *assetLoader) Validate() error {
if path := l.source.file; path != "" {
return l.validateFile(path)
}
if url := l.source.url; url != "" {
return l.validateURL(url)
}
return nil
}
func (l *assetLoader) loadImageFromFile(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// Calling the generic image.Decode() will tell give us the data
// and type of image it is as a string. We expect "png"
imageData, _, err := image.Decode(file)
if err != nil {
return nil, fmt.Errorf("Failed to decode image: %w", err)
}
return imageData, nil
}
func (l *assetLoader) loadImageFromURL(url string) (image.Image, error) {
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
fileName := path.Base(url)
path := fmt.Sprintf("*_%v", fileName)
file, err := ioutil.TempFile("", path)
if err != nil {
return nil, err
}
defer file.Close()
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
return nil, err
}
return l.loadImageFromFile(path)
}
func (l *assetLoader) validateURL(url string) error {
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
fileName := path.Base(url)
file, err := ioutil.TempFile("", fmt.Sprintf("*_%v", fileName))
if err != nil {
return err
}
defer os.Remove(file.Name())
defer file.Close()
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
if err = l.validateFile(file.Name()); err != nil {
return err
}
return nil
}
func (l *assetLoader) validateFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
image, _, err := image.DecodeConfig(file)
if err != nil {
return err
}
return l.source.validateImage(image, filepath.Base(file.Name()))
}