/
metadata.go
65 lines (53 loc) · 1.62 KB
/
metadata.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
package media
import (
"fmt"
_ "image/gif" // This is required to initialize the GIF decoder
_ "image/jpeg" // This is required to initialize the JPEG decoder
_ "image/png" // This is required to initialize the PNG decoder
"io"
"net/http"
_ "github.com/biessek/golang-ico"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
type Metadata struct {
Width int
Height int
ContentSize int64
ContentType string
}
var defaultMaxBytes = 524288 // 512 KB
// FetchMetadataFromHeader returns the image metadata from the given URL. However, it does not download the image.
// It will attempt to get the ContentType, Width, and Height from downloading at most maxBytes. If the metadata cannot
// be extracted within the first maxBytes, an error is returned. This is useful for validating images without
// downloading the entire image.
func FetchMetadataFromHeader(imgURL string, maxBytes int) (Metadata, error) {
resp, err := http.Get(imgURL)
if err != nil {
return Metadata{}, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
var imgData []byte
buf := make([]byte, 8192) // 8 KB chunks
totalRead := 0
for totalRead < maxBytes {
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
return Metadata{}, err
}
if n == 0 {
break
}
imgData = append(imgData, buf[:n]...)
totalRead += n
img, err := NewImageFromBytes(imgData)
if err == nil {
//fmt.Printf("Found in first %d bytes\n", totalRead)
return img.Metadata, nil
}
}
return Metadata{}, fmt.Errorf("could not extract metadata within the first %d KB", maxBytes/1024)
}