-
Notifications
You must be signed in to change notification settings - Fork 62
/
metadata.go
57 lines (46 loc) · 1.06 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
package coreunix
import (
core "github.com/ipfs/go-ipfs/core"
dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
)
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
c, err := cid.Decode(skey)
if err != nil {
return "", err
}
nd, err := n.DAG.Get(n.Context(), c)
if err != nil {
return "", err
}
mdnode := new(dag.ProtoNode)
mdata, err := ft.BytesForMetadata(m)
if err != nil {
return "", err
}
mdnode.SetData(mdata)
if err := mdnode.AddNodeLinkClean("file", nd); err != nil {
return "", err
}
nk, err := n.DAG.Add(mdnode)
if err != nil {
return "", err
}
return nk.String(), nil
}
func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
c, err := cid.Decode(skey)
if err != nil {
return nil, err
}
nd, err := n.DAG.Get(n.Context(), c)
if err != nil {
return nil, err
}
pbnd, ok := nd.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}
return ft.MetadataFromBytes(pbnd.Data())
}