/
util.go
51 lines (41 loc) · 914 Bytes
/
util.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
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
type typeExtractor struct {
Type string `json:"$type"`
}
func TypeExtract(b []byte) (string, error) {
var te typeExtractor
if err := json.Unmarshal(b, &te); err != nil {
return "", err
}
return te.Type, nil
}
type Blob struct {
Cid string `json:"cid"`
MimeType string `json:"mimeType"`
}
type CborChecker struct {
Type string `cborgen:"$type"`
}
func CborTypeExtract(b []byte) (string, error) {
var tcheck CborChecker
if err := tcheck.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
fmt.Printf("bad bytes: %x\n", b)
return "", err
}
return tcheck.Type, nil
}
func CborTypeExtractReader(r io.Reader) (string, []byte, error) {
buf := new(bytes.Buffer)
tr := io.TeeReader(r, buf)
var tcheck CborChecker
if err := tcheck.UnmarshalCBOR(tr); err != nil {
return "", nil, err
}
return tcheck.Type, buf.Bytes(), nil
}