-
Notifications
You must be signed in to change notification settings - Fork 13
/
fixture.go
84 lines (65 loc) · 1.43 KB
/
fixture.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package car
import (
"context"
"io"
"strings"
"time"
files "github.com/ipfs/boxo/files"
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipld/go-ipld-prime"
mb "github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
)
type FixtureNode struct {
node format.Node
dsvc format.DAGService
}
func (n *FixtureNode) Cid() cid.Cid {
return n.node.Cid()
}
func (n *FixtureNode) Base32Cid() string {
redirectDirCID, err := mb.Encode(mb.Base32, n.Cid().Bytes())
if err != nil {
panic(err)
}
return redirectDirCID
}
func (n *FixtureNode) RawData() []byte {
return n.node.RawData()
}
func (n *FixtureNode) Formatted(codecStr string) []byte {
node := n.node.(ipld.Node)
return FormatDagNode(node, codecStr)
}
func (n *FixtureNode) ToFile() files.File {
f, err := unixfile.NewUnixfsFile(context.Background(), n.dsvc, n.node)
if err != nil {
panic(err)
}
r, ok := f.(files.File)
if !ok {
panic("not a file")
}
return r
}
func (n *FixtureNode) ReadFile() string {
f := n.ToFile()
buf := new(strings.Builder)
_, err := io.Copy(buf, f)
if err != nil {
panic(err)
}
return buf.String()
}
func RandomCID() cid.Cid {
now := time.Now().UTC()
timeBytes := []byte(now.Format(time.RFC3339))
mh, err := multihash.Sum(timeBytes, multihash.SHA2_256, -1)
if err != nil {
panic(err)
}
c := cid.NewCidV1(cid.Raw, mh)
return c
}