diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 415e3aaaafc..e766b79ef7a 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -10,6 +10,19 @@ import ( ipld "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node" ) +type CoreAPI struct { + node *core.IpfsNode +} + +func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI { + api := &CoreAPI{n} + return api +} + +func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { + return (*UnixfsAPI)(api) +} + func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) { pp, err := path.ParsePath(p) if err != nil { diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index cf64719476e..cb62b53042b 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -9,11 +9,6 @@ import ( cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid" ) -// type CoreAPI interface { -// ID() CoreID -// Version() CoreVersion -// } - type Link ipld.Link type Reader interface { @@ -21,6 +16,10 @@ type Reader interface { io.Closer } +type CoreAPI interface { + Unixfs() UnixfsAPI +} + type UnixfsAPI interface { Add(context.Context, io.Reader) (*cid.Cid, error) Cat(context.Context, string) (Reader, error) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 3eddd9d1d68..45a0cbe1164 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -4,7 +4,6 @@ import ( "context" "io" - core "github.com/ipfs/go-ipfs/core" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreunix "github.com/ipfs/go-ipfs/core/coreunix" uio "github.com/ipfs/go-ipfs/unixfs/io" @@ -12,14 +11,7 @@ import ( cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid" ) -type UnixfsAPI struct { - node *core.IpfsNode -} - -func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI { - api := &UnixfsAPI{n} - return api -} +type UnixfsAPI CoreAPI func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) { k, err := coreunix.AddWithContext(ctx, api.node, r) diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index 61270551045..3cda629afd6 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { if err != nil { return nil, nil, err } - api := coreapi.NewUnixfsAPI(node) + api := coreapi.NewCoreAPI(node).Unixfs() return node, api, nil } diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index caa8311b8b6..b35f3ecf809 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption { Headers: cfg.Gateway.HTTPHeaders, Writable: writable, PathPrefixes: cfg.Gateway.PathPrefixes, - }, coreapi.NewUnixfsAPI(n)) + }, coreapi.NewCoreAPI(n)) for _, p := range paths { mux.Handle(p+"/", gateway) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index c2844089d5a..7122b0124e1 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -37,10 +37,10 @@ const ( type gatewayHandler struct { node *core.IpfsNode config GatewayConfig - api coreiface.UnixfsAPI + api coreiface.CoreAPI } -func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler { +func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler { i := &gatewayHandler{ node: n, config: c, @@ -158,7 +158,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ipnsHostname = true } - dr, err := i.api.Cat(ctx, urlPath) + dr, err := i.api.Unixfs().Cat(ctx, urlPath) dir := false switch err { case nil: @@ -231,7 +231,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr return } - links, err := i.api.Ls(ctx, urlPath) + links, err := i.api.Unixfs().Ls(ctx, urlPath) if err != nil { internalWebError(w, err) return @@ -260,7 +260,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } // return index page instead. - dr, err := i.api.Cat(ctx, p.String()) + dr, err := i.api.Unixfs().Cat(ctx, p.String()) if err != nil { internalWebError(w, err) return @@ -327,7 +327,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - k, err := i.api.Add(ctx, r.Body) + k, err := i.api.Unixfs().Add(ctx, r.Body) if err != nil { internalWebError(w, err) return