Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed Jun 21, 2016
1 parent 5547750 commit 0cf8bb8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
30 changes: 26 additions & 4 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
}
14 changes: 14 additions & 0 deletions core/coreapi/coreapi_test.go
Original file line number Diff line number Diff line change
@@ -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) {

}
13 changes: 10 additions & 3 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<hash>/foo ???
}

type Path string

type Object struct {
Links []Link
Data Data
Expand Down
18 changes: 9 additions & 9 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0cf8bb8

Please sign in to comment.