-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_functions.go
71 lines (59 loc) · 1.4 KB
/
image_functions.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
package main
import (
"io"
"io/ioutil"
"net/http"
"os"
)
// Given an image URL, return a pointer to the image
func getImage(URL string) io.ReadCloser {
resp, err := http.Get(URL)
if err != nil {
panic(err)
}
defer resp.Body.Close()
return resp.Body
}
func saveImage(img io.ReadCloser, fileName string) {
imgBytes, err := ioutil.ReadAll(img)
if err != nil {
panic(err)
}
outputPath := fileName //filepath.Join(outputDir, fileName)
err = ioutil.WriteFile(outputPath, imgBytes, 0644)
if err != nil {
panic(err)
}
}
// Merging functions to one: because the deferred HTTP close closes the stream
// before a new function can access it atm, and these functions are always
// going to be piped together
func getAndSaveImage(url string, filePath string, fileName string) error {
var err error
outputPath := filePath + fileName
// 0. Check if the file already exists (moved to main)
// 1. Open the HTTPS stream to get the image
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// 2. Get the body ready to write
var imgBytes []byte
imgBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// 3a. Make the directory if not already there
err = os.MkdirAll(filePath, 0755)
if err != nil {
return err
}
// 3b. write the file
err = ioutil.WriteFile(outputPath, imgBytes, 0644)
if err != nil {
return err
}
return nil
}