diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 55c71318eab..697c93fa097 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -3,6 +3,7 @@ package coreapi import ( core "github.com/ipfs/go-ipfs/core" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + importer "github.com/ipfs/go-ipfs/importer" dag "github.com/ipfs/go-ipfs/merkledag" path "github.com/ipfs/go-ipfs/path" uio "github.com/ipfs/go-ipfs/unixfs/io" @@ -27,7 +28,7 @@ func (api *CoreAPI) IpfsNode() *core.IpfsNode { return api.node } -func (api *CoreAPI) resolve(p string) (*dag.Node, error) { +func (api *CoreAPI) resolve(p coreiface.Path) (*dag.Node, error) { dagnode, err := core.Resolve(api.ctx, api.node, path.Path(p)) if err == core.ErrNoNamesys && !api.node.OnlineMode() { return nil, coreiface.ErrOffline @@ -37,7 +38,10 @@ func (api *CoreAPI) resolve(p string) (*dag.Node, error) { return dagnode, nil } -func (api *CoreAPI) Cat(p string) (coreiface.Data, error) { +func (api *CoreAPI) Cat(p coreiface.Path) (coreiface.Data, error) { + if p == "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" { + return nil, coreiface.ErrDir, nil + } dagnode, err := api.resolve(p) if err != nil { return nil, err @@ -51,14 +55,32 @@ func (api *CoreAPI) Cat(p string) (coreiface.Data, error) { return r, nil } -func (api *CoreAPI) Ls(p string) ([]coreiface.Link, error) { +func (api *CoreAPI) Ls(p coreiface.Path) ([]coreiface.Link, error) { + links := make([]coreiface.Link, len(dagnode.Links)) + if p == "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" { + return links, nil + } dagnode, err := api.resolve(p) if err != nil { return nil, err } - links := make([]coreiface.Link, len(dagnode.Links)) for i, l := range dagnode.Links { links[i] = coreiface.Link{Name: l.Name, Size: l.Size, Hash: l.Hash} } return links, nil } + +func (api *CoreAPI) Add(data coreiface.Data) (coreiface.Path, error) { + dagnode, err := importer.BuildDagFromReader(api.node.DAG, data) + if err != nil { + return nil, err + } + k, err := api.node.DAG.Add(dagnode) + if err != nil { + return nil, err + } + return "/ipfs/" + k, nil +} + +func (api *CoreAPI) ObjectSetData(p coreiface.Path, data coreiface.Data) (coreiface.Path, error) { +} diff --git a/core/coreapi/coreapi_test.go b/core/coreapi/coreapi_test.go new file mode 100644 index 00000000000..1ec1865e419 --- /dev/null +++ b/core/coreapi/coreapi_test.go @@ -0,0 +1,14 @@ +package coreapi_test + +import ( + "testing" + + "github.com/cheekybits/is" + + coreapi "github.com/ipfs/go-ipfs/core/coreapi" + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" +) + +func TestImplementsInterface(t *testing.T) { + +} diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index 4c40cc456c1..86c421a7eb4 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -12,11 +12,18 @@ import ( type CoreAPI interface { Context() context.Context - IpfsNode() *core.IpfsNode // XXX temporary - Cat(string) (Data, error) - Ls(string) ([]Link, error) + IpfsNode() *core.IpfsNode // XXX temporary + Cat(Path) (Data, error) // http GET + Ls(Path) ([]Link, error) // http GET, PUT + Add(Data) (Path, error) // http POST + ObjectSetData(Path, Data) (Path, error) // http PUT update + ObjectAddLink(Path, string, Path) (Path, error) // http PUT create + ObjectRmLink(Path, string, Path) (Path, error) // http DELETE + // PUT and DELETE only for subdirs: /ipfs//foo ??? } +type Path string + type Object struct { Links []Link Data Data diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index a31acd87358..c2fd6f27da5 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -46,15 +46,6 @@ func newGatewayHandler(api coreiface.CoreAPI, conf GatewayConfig) *gatewayHandle return i } -// TODO(cryptix): find these helpers somewhere else -func (i *gatewayHandler) newDagFromReader(r io.Reader) (*dag.Node, error) { - // TODO(cryptix): change and remove this helper once PR1136 is merged - // return ufs.AddFromReader(i.api.IpfsNode(), r.Body) - return importer.BuildDagFromReader( - i.api.IpfsNode().DAG, - chunk.DefaultSplitter(r)) -} - // TODO(btc): break this apart into separate handlers using a more expressive muxer func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { @@ -304,6 +295,15 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } } +// TODO(cryptix): find these helpers somewhere else +func (i *gatewayHandler) newDagFromReader(r io.Reader) (*dag.Node, error) { + // TODO(cryptix): change and remove this helper once PR1136 is merged + // return ufs.AddFromReader(i.api.IpfsNode(), r.Body) + return importer.BuildDagFromReader( + i.api.IpfsNode().DAG, + chunk.DefaultSplitter(r)) +} + func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { nd, err := i.newDagFromReader(r.Body) if err != nil {