|
1 | 1 | package httpapi
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
| 6 | + "errors" |
| 7 | + "fmt" |
5 | 8 | "io"
|
6 | 9 |
|
| 10 | + "github.com/ipfs/go-cid" |
7 | 11 | "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
8 |
| - "github.com/ipfs/go-ipfs/core/coreapi/interface/options" |
| 12 | + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" |
| 13 | + mh "github.com/multiformats/go-multihash" |
9 | 14 | )
|
10 | 15 |
|
11 | 16 | type BlockAPI HttpApi
|
12 | 17 |
|
13 |
| -func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...options.BlockPutOption) (iface.BlockStat, error) { |
14 |
| - return nil, ErrNotImplemented |
| 18 | +type blockStat struct { |
| 19 | + Key string |
| 20 | + BSize int `json:"Size"` |
| 21 | +} |
| 22 | + |
| 23 | +func (s *blockStat) Size() int { |
| 24 | + return s.BSize |
| 25 | +} |
| 26 | + |
| 27 | +func (s *blockStat) valid() (iface.ResolvedPath, error) { |
| 28 | + c, err := cid.Parse(s.Key) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + return iface.IpldPath(c), nil |
| 34 | +} |
| 35 | + |
| 36 | +func (s *blockStat) Path() iface.ResolvedPath { |
| 37 | + p, _ := s.valid() |
| 38 | + return p |
| 39 | +} |
| 40 | + |
| 41 | +func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) { |
| 42 | + options, _, err := caopts.BlockPutOptions(opts...) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + mht, ok := mh.Codes[options.MhType] |
| 48 | + if !ok { |
| 49 | + return nil, fmt.Errorf("unknowm mhType %d", options.MhType) |
| 50 | + } |
| 51 | + |
| 52 | + req := api.core().request("block/put"). |
| 53 | + Option("mhtype", mht). |
| 54 | + Option("mhlen", options.MhLength). |
| 55 | + Option("format", options.Codec). |
| 56 | + FileBody(r) |
| 57 | + |
| 58 | + var out blockStat |
| 59 | + if err := req.Exec(ctx, &out); err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + if _, err := out.valid(); err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return &out, nil |
15 | 67 | }
|
16 | 68 |
|
17 | 69 | func (api *BlockAPI) Get(ctx context.Context, p iface.Path) (io.Reader, error) {
|
18 | 70 | resp, err := api.core().request("block/get", p.String()).Send(context.Background())
|
19 | 71 | if err != nil {
|
20 | 72 | return nil, err
|
21 | 73 | }
|
| 74 | + if resp.Error != nil { |
| 75 | + return nil, resp.Error |
| 76 | + } |
22 | 77 |
|
23 |
| - //TODO: is close on the reader enough? |
24 |
| - //defer resp.Close() |
| 78 | + //TODO: make get return ReadCloser to avoid copying |
| 79 | + defer resp.Close() |
| 80 | + b := new(bytes.Buffer) |
| 81 | + if _, err := io.Copy(b, resp.Output); err != nil { |
| 82 | + return nil, err |
| 83 | + } |
25 | 84 |
|
26 |
| - //TODO: make blockApi return ReadCloser |
27 |
| - return resp.Output, resp.Error |
| 85 | + return b, nil |
28 | 86 | }
|
29 | 87 |
|
30 |
| -func (api *BlockAPI) Rm(ctx context.Context, p iface.Path, opts ...options.BlockRmOption) error { |
31 |
| - return ErrNotImplemented |
| 88 | +func (api *BlockAPI) Rm(ctx context.Context, p iface.Path, opts ...caopts.BlockRmOption) error { |
| 89 | + options, err := caopts.BlockRmOptions(opts...) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + removedBlock := struct { |
| 95 | + Hash string `json:",omitempty"` |
| 96 | + Error string `json:",omitempty"` |
| 97 | + }{} |
| 98 | + |
| 99 | + req := api.core().request("block/rm"). |
| 100 | + Option("force", options.Force). |
| 101 | + Arguments(p.String()) |
| 102 | + |
| 103 | + if err := req.Exec(ctx, &removedBlock); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if removedBlock.Error != "" { |
| 108 | + return errors.New(removedBlock.Error) |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
32 | 112 | }
|
33 | 113 |
|
34 | 114 | func (api *BlockAPI) Stat(ctx context.Context, p iface.Path) (iface.BlockStat, error) {
|
35 |
| - return nil, ErrNotImplemented |
| 115 | + var out blockStat |
| 116 | + err := api.core().request("block/stat", p.String()).Exec(ctx, &out) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + if _, err := out.valid(); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return &out, nil |
36 | 125 | }
|
37 | 126 |
|
38 | 127 | func (api *BlockAPI) core() *HttpApi {
|
|
0 commit comments