-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
83 lines (69 loc) · 1.78 KB
/
image.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
package xkcd
import (
"image"
// Some xkcd comics are in gif format.
_ "image/gif"
// Some xkcd comics are in jpeg format.
_ "image/jpeg"
// Some xkcd comics are in png format.
_ "image/png"
"io"
"net/http"
"path"
)
// Image represents an xkcd comic image.
type Image struct {
image.Image
sourceURL string
sourceFormat string
}
// NewImage takes an io.Reader and returns an Image struct.
func NewImage(r io.Reader) (*Image, error) {
var img Image
var err error
img.Image, img.sourceFormat, err = image.Decode(r)
return &img, err
}
// GetImage is a convenience function to download a comic's metadata and
// then download the comic's image. If you already have the comic
// metadata, use Comic's Image() method.
func GetImage(n int) (*Image, error) {
c, err := Get(n)
if err != nil {
return nil, err
}
return c.Image()
}
func getImageFromURL(url string) (*Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
img, err := NewImage(resp.Body)
if err != nil {
return nil, err
}
img.sourceURL = url
return img, nil
}
// SourceURL is URL from which the comic image was retrieved. If image
// wasn't retrieved from a URL (e.g. when it is created by calling
// NewImage directly), an empty string will be returned.
func (img *Image) SourceURL() string {
return img.sourceURL
}
// SourceName is the original file name of the comic image. If the Image
// wasn't retrieved from a URL (e.g. when it is created by calling
// NewImage directly), an empty string will be returned.
func (img *Image) SourceName() string {
url := img.SourceURL()
if url == "" {
return ""
}
return path.Base(url)
}
// SourceFormat is the file format that the image was decoded from.
func (img *Image) SourceFormat() string {
return img.sourceFormat
}