|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/errors" |
| 7 | + "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" |
| 8 | + "io/ioutil" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/ipfs/go-ipfs/core/coreapi/interface" |
| 12 | + |
| 13 | + ipfspath "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path" |
| 14 | + ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format" |
| 15 | +) |
| 16 | + |
| 17 | +type ipldNode struct { |
| 18 | + ctx context.Context //TODO: should we re-consider adding ctx to ipld interfaces? |
| 19 | + path iface.ResolvedPath |
| 20 | + api *HttpApi |
| 21 | +} |
| 22 | + |
| 23 | +func (n *ipldNode) RawData() []byte { |
| 24 | + r, err := n.api.Block().Get(n.ctx, n.path) |
| 25 | + if err != nil { |
| 26 | + panic(err) // TODO: eww, should we add errors too / better ideas? |
| 27 | + } |
| 28 | + |
| 29 | + b, err := ioutil.ReadAll(r) |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + |
| 34 | + return b |
| 35 | +} |
| 36 | + |
| 37 | +func (n *ipldNode) Cid() cid.Cid { |
| 38 | + return n.path.Cid() |
| 39 | +} |
| 40 | + |
| 41 | +func (n *ipldNode) String() string { |
| 42 | + return fmt.Sprintf("[Block %s]", n.Cid()) |
| 43 | +} |
| 44 | + |
| 45 | +func (n *ipldNode) Loggable() map[string]interface{} { |
| 46 | + return nil //TODO: we can't really do better here, can we? |
| 47 | +} |
| 48 | + |
| 49 | +// TODO: should we use 'full'/real ipld codecs for this? js-ipfs-api does that. |
| 50 | +// We can also give people a choice |
| 51 | +func (n *ipldNode) Resolve(path []string) (interface{}, []string, error) { |
| 52 | + p := ipfspath.Join([]string{n.path.String(), ipfspath.Join(path)}) |
| 53 | + |
| 54 | + var out interface{} |
| 55 | + n.api.request("dag/get", p).Exec(n.ctx, &out) |
| 56 | + |
| 57 | + // TODO: this is more than likely wrong, fix if we decide to stick with this 'http-ipld-node' hack |
| 58 | + for len(path) > 0 { |
| 59 | + switch o := out.(type) { |
| 60 | + case map[string]interface{}: |
| 61 | + v, ok := o[path[0]] |
| 62 | + if !ok { |
| 63 | + // TODO: ipld links |
| 64 | + return nil, nil, errors.New("no element under this path") |
| 65 | + } |
| 66 | + out = v |
| 67 | + case []interface{}: |
| 68 | + n, err := strconv.ParseUint(path[0], 10, 32) |
| 69 | + if err != nil { |
| 70 | + return nil, nil, err |
| 71 | + } |
| 72 | + if len(o) < int(n) { |
| 73 | + return nil, nil, errors.New("no element under this path") |
| 74 | + } |
| 75 | + out = o[n] |
| 76 | + } |
| 77 | + path = path[1:] |
| 78 | + } |
| 79 | + |
| 80 | + return out, path, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (n *ipldNode) Tree(path string, depth int) []string { |
| 84 | + panic("implement me") |
| 85 | +} |
| 86 | + |
| 87 | +func (n *ipldNode) ResolveLink(path []string) (*ipld.Link, []string, error) { |
| 88 | + panic("implement me") |
| 89 | +} |
| 90 | + |
| 91 | +func (n *ipldNode) Copy() ipld.Node { |
| 92 | + panic("implement me") |
| 93 | +} |
| 94 | + |
| 95 | +func (n *ipldNode) Links() []*ipld.Link { |
| 96 | + panic("implement me") |
| 97 | +} |
| 98 | + |
| 99 | +func (n *ipldNode) Stat() (*ipld.NodeStat, error) { |
| 100 | + panic("implement me") |
| 101 | +} |
| 102 | + |
| 103 | +func (n *ipldNode) Size() (uint64, error) { |
| 104 | + panic("implement me") |
| 105 | +} |
| 106 | + |
| 107 | +var _ ipld.Node = &ipldNode{} |
0 commit comments