-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
lorem_flickr.go
66 lines (53 loc) · 1.23 KB
/
lorem_flickr.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
package faker
import (
"io"
"log"
"os"
"strconv"
)
const loremFlickrBaseURL = "https://loremflickr.com"
// LoremFlickr is a faker struct for LoremFlickr
type LoremFlickr struct {
faker *Faker
HTTPClient HTTPClient
TempFileCreator TempFileCreator
}
// Image generates a *os.File with a random image using the loremflickr.com service
func (lf LoremFlickr) Image(width, height int, categories []string, prefix string, categoriesStrict bool) *os.File {
url := loremFlickrBaseURL
switch prefix {
case "g":
url += "/g"
case "p":
url += "/p"
case "red":
url += "/red"
case "green":
url += "/green"
case "blue":
url += "/blue"
}
url += string('/') + strconv.Itoa(width) + string('/') + strconv.Itoa(height)
if len(categories) > 0 {
url += string('/')
for _, category := range categories {
url += category + string(',')
}
if categoriesStrict {
url += "/all"
}
}
resp, err := lf.HTTPClient.Get(url)
if err != nil {
log.Println("Error while requesting", url, ":", err)
panic(err)
}
defer resp.Body.Close()
f, err := lf.TempFileCreator.TempFile("loremflickr-img-*.jpg")
if err != nil {
log.Println("Error while creating a temp file:", err)
panic(err)
}
io.Copy(f, resp.Body)
return f
}