1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 23:16:11 +08:00
Files
kubo/core/coreunix/metadata.go
Jeromy f4d7369c4a bitswap: protocol extension to handle cids
This change adds the /ipfs/bitswap/1.1.0 protocol. The new protocol
adds a 'payload' field to the protobuf message and deprecates the
existing 'blocks' field. The 'payload' field is an array of pairs of cid
prefixes and block data. The cid prefixes are used to ensure the correct
codecs and hash functions are used to handle the block on the receiving
end.

License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
2016-10-10 08:19:31 -07:00

53 lines
997 B
Go

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/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/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.Node)
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
}
return ft.MetadataFromBytes(nd.Data())
}