This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
/
cat.go
71 lines (60 loc) · 1.67 KB
/
cat.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
package ipfs
import (
"context"
ipath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
"gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
"io/ioutil"
"strings"
"time"
"github.com/go-errors/errors"
coreiface "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core"
)
// Fetch data from IPFS given the hash
func Cat(n *core.IpfsNode, path string, timeout time.Duration) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !strings.HasPrefix(path, "/ipfs/") {
path = "/ipfs/" + path
}
api, err := coreapi.NewCoreAPI(n)
if err != nil {
return nil, err
}
pth, err := coreiface.ParsePath(path)
if err != nil {
return nil, err
}
nd, err := api.Unixfs().Get(ctx, pth)
if err != nil {
return nil, err
}
r, ok := nd.(files.File)
if !ok {
return nil, errors.New("Received incorrect type from Unixfs().Get()")
}
return ioutil.ReadAll(r)
}
func ResolveThenCat(n *core.IpfsNode, ipnsPath ipath.Path, timeout time.Duration, quorum uint, usecache bool) ([]byte, error) {
var ret []byte
pid, err := peer.IDB58Decode(ipnsPath.Segments()[0])
if err != nil {
return nil, err
}
hash, err := Resolve(n, pid, timeout, quorum, usecache)
if err != nil {
return ret, err
}
p := make([]string, len(ipnsPath.Segments()))
p[0] = hash
for i := 0; i < len(ipnsPath.Segments())-1; i++ {
p[i+1] = ipnsPath.Segments()[i+1]
}
b, err := Cat(n, ipath.Join(p), timeout)
if err != nil {
return ret, err
}
return b, nil
}