-
Notifications
You must be signed in to change notification settings - Fork 2
/
ioutil.go
77 lines (54 loc) · 1.24 KB
/
ioutil.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
package pixelart
import (
"fmt"
"log"
"io"
"os"
"net/http"
// "image"
"image/png"
)
func ReadImage( path string ) *Image {
fmt.Printf( "==> reading %s...\n", path )
f, err := os.Open( path )
if err != nil {
log.Fatal(err)
}
defer f.Close()
img, err := png.Decode( f )
if err != nil {
log.Fatal(err)
}
bounds := img.Bounds()
fmt.Println( bounds )
// e.g. punks.png (0,0)-(2400,2400)
return &Image{ ConvertToNRGBA( img ) }
}
func Download( url, outpath string ) {
fmt.Printf( "==> Downloading %s...\n", url )
resp, err := http.Get( url )
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// todo/fix:
// (double) check for content type - why? why not?
fmt.Printf( "HTTP %v\n", resp.StatusCode )
// dump headers for debugging
for name, headers := range resp.Header {
for _, h := range headers {
fmt.Printf( " %v : %v\n", name, h )
}
}
f, err := os.Create( outpath )
if err != nil {
log.Fatal(err)
}
defer f.Close()
n, err := io.Copy( f, resp.Body ) // note: Copy( writer, reader )
if err != nil {
fmt.Printf( " !! ERROR - writing %d byte(s) to %s...\n", n, outpath )
log.Fatal(err)
}
fmt.Printf( " writing %d byte(s) to %s...\n", n, outpath )
}