Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <[email protected]>
  • Loading branch information
Lars Gierth committed Jun 22, 2016
1 parent 5547750 commit aa6dec1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
48 changes: 45 additions & 3 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ 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"
chunk "github.com/ipfs/go-ipfs/importer/chunk"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)

type Path path.Path

type Link struct {
Name string
Size uint64
Hash mh.Multihash
}

type CoreAPI struct {
ctx context.Context
node *core.IpfsNode
Expand All @@ -27,7 +38,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 +48,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.String() == "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
return nil, coreiface.ErrDir
}
dagnode, err := api.resolve(p)
if err != nil {
return nil, err
Expand All @@ -51,7 +65,10 @@ 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) {
if p.String() == "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
return make([]coreiface.Link, 0), nil
}
dagnode, err := api.resolve(p)
if err != nil {
return nil, err
Expand All @@ -62,3 +79,28 @@ func (api *CoreAPI) Ls(p string) ([]coreiface.Link, error) {
}
return links, nil
}

func (api *CoreAPI) Add(data coreiface.Data) (coreiface.Path, error) {
splitter := chunk.DefaultSplitter(data)
dagnode, err := importer.BuildDagFromReader(api.node.DAG, splitter)
if err != nil {
return "", err
}
k, err := api.node.DAG.Add(dagnode)
if err != nil {
return "", err
}
return coreiface.Path("/ipfs/" + k.String()), nil
}

func (api *CoreAPI) ObjectSetData(p coreiface.Path, data coreiface.Data) (coreiface.Path, error) {
return p, nil
}

func (api *CoreAPI) ObjectAddLink(p coreiface.Path, name string, link coreiface.Path) (coreiface.Path, error) {
return p, nil
}

func (api *CoreAPI) ObjectRmLink(p coreiface.Path, name string) (coreiface.Path, error) {
return p, nil
}
12 changes: 12 additions & 0 deletions core/coreapi/coreapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package coreapi_test

import (
"testing"

coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
)

func TestImplementsInterface(t *testing.T) {

}
25 changes: 14 additions & 11 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@ 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, error) // http DELETE
// PUT and DELETE only for subdirs: /ipfs/<hash>/foo ???
}

type Object struct {
Links []Link
Data Data
type Path interface {
String() string
}

type Link struct {
Name string // utf-8
Size uint64
Hash mh.Multihash
type Link interface {
Name() string
Size() uint64
Hash() mh.Multihash
}

type Data interface {
io.Reader
io.Seeker
io.Closer
}

Expand Down
31 changes: 12 additions & 19 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,22 +295,24 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}
}

func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
nd, err := i.newDagFromReader(r.Body)
if err != nil {
internalWebError(w, err)
return
}
// 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))
}

k, err := i.api.IpfsNode().DAG.Add(nd)
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
p, err := i.api.Add(r.Body)
if err != nil {
internalWebError(w, err)
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", k.String())
http.Redirect(w, r, ipfsPathPrefix+k.String(), http.StatusCreated)
w.Header().Set("X-IPFS-Path", p)
http.Redirect(w, r, ipfsPathPrefix+p, http.StatusCreated)
}

func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit aa6dec1

Please sign in to comment.