-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
58 lines (49 loc) · 1.53 KB
/
utils.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
package sergeant
import (
"bufio"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
)
// magicTable is used to detect the filetype of images attached to a Card.
// Courtest of https://stackoverflow.com/questions/25959386/how-to-check-if-a-file-is-a-valid-image
var magicTable = map[string]string{
"\xff\xd8\xff": "image/jpeg",
"\x89PNG\r\n\x1a\n": "image/png",
"GIF87a": "image/gif",
"GIF89a": "image/gif",
}
// mimeFromIncipit returns the mime type of an image file from its first few bytes or the empty string if the
// file does not look like a known file type.
// Courtesy of https://stackoverflow.com/questions/25959386/how-to-check-if-a-file-is-a-valid-image
func mimeFromIncipit(incipit []byte) string {
incipitStr := string(incipit)
for magic, mime := range magicTable {
if strings.HasPrefix(incipitStr, magic) {
return mime
}
}
return ""
}
// encodeAsDataURI returns the contents of an image as a data URI. This is used to save two additional requests.
func encodeAsDataURI(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
reader := bufio.NewReader(f)
content, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
mime := mimeFromIncipit(content)
if mime != "image/png" && mime != "image/jpeg" && mime != "image/gif" {
return "", fmt.Errorf("unrecognised mime type %q", mime)
}
prefix := fmt.Sprintf("data:%s;base64,", mime)
encoded := base64.StdEncoding.EncodeToString(content)
return prefix + encoded, nil
}