/
parse.go
68 lines (53 loc) · 1.44 KB
/
parse.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
package media
import (
"errors"
"io"
)
var (
// ErrUnknownContentType is when media is an unknown type
ErrUnknownContentType = errors.New("media: unknown media type")
// ErrUnsupportedSize is when media type doesn't implement size
ErrUnsupportedSize = errors.New("media: unsupported size")
// ErrPNGMissingIHDR is when a png is missing the HDR header
ErrPNGMissingIHDR = errors.New("media: invalid png missing IHDR")
// ErrBMPInvalidHeaderLength is when a bmp has invalid header length
ErrBMPInvalidHeaderLength = errors.New("media: invalid bmp header length")
)
// ContentType is the type of the media
type ContentType string
const (
// PNGType media type
PNGType ContentType = "image/png"
// GIFType media type
GIFType = "image/gif"
// BMPType media type
BMPType = "image/bmp"
// JPEGType media type
JPEGType = "image/jpeg"
)
// Dimensions holds the Dimensions
type Dimensions struct {
Width int
Height int
}
// Media is an interface for getting information from media
type Media interface {
Size() Dimensions
Type() ContentType
}
// Parse returns the media information including type and dimensions
func Parse(r io.Reader) (Media, error) {
contentType, readBytes, err := DetectContentType(r)
if err != nil {
return nil, err
}
switch contentType {
case GIFType:
return gifDimensions(readBytes)
case PNGType:
return pngDimensions(readBytes)
case BMPType:
return bmpDimensions(readBytes)
}
return nil, ErrUnsupportedSize
}