-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture.go
49 lines (42 loc) · 837 Bytes
/
capture.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
package bigbro
import (
"bytes"
"encoding/base64"
"image/png"
"os"
"path"
"strconv"
"strings"
"time"
)
var (
CapturePath = "captures"
)
type Capture struct {
Actor string `json:"actor"`
Data string `json:"data"`
Time time.Time `json:"time"`
}
func WriteCapture(capture Capture) error {
fpath := path.Join(CapturePath, capture.Actor)
fname := strconv.Itoa(int(capture.Time.Unix())) + ".png"
err := os.MkdirAll(fpath, os.ModePerm)
if err != nil {
return err
}
unbased, err := base64.StdEncoding.DecodeString(capture.Data[strings.IndexByte(capture.Data, ',')+1:])
if err != nil {
return err
}
r := bytes.NewReader(unbased)
im, err := png.Decode(r)
if err != nil {
return err
}
f, err := os.Create(path.Join(fpath, fname))
if err != nil {
return err
}
err = png.Encode(f, im)
return err
}