Skip to content
Snippets Groups Projects
Commit b39bc78c authored by Travis Ralston's avatar Travis Ralston
Browse files

Early support for putting files into IPFS (not wired)

parent 3219711b
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,7 @@ require ( ...@@ -29,6 +29,7 @@ require (
github.com/ipfs/go-cid v0.0.4 github.com/ipfs/go-cid v0.0.4
github.com/ipfs/go-ipfs v0.4.22-0.20191119151441-b8ec598d5801 github.com/ipfs/go-ipfs v0.4.22-0.20191119151441-b8ec598d5801
github.com/ipfs/go-ipfs-config v0.0.11 github.com/ipfs/go-ipfs-config v0.0.11
github.com/ipfs/go-ipfs-files v0.0.4
github.com/ipfs/go-ipfs-http-client v0.0.5 github.com/ipfs/go-ipfs-http-client v0.0.5
github.com/ipfs/interface-go-ipfs-core v0.2.6 github.com/ipfs/interface-go-ipfs-core v0.2.6
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
......
package ipfs_proxy package ipfs_proxy
import ( import (
"io"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/common/config" "github.com/turt2live/matrix-media-repo/common/config"
"github.com/turt2live/matrix-media-repo/common/rcontext" "github.com/turt2live/matrix-media-repo/common/rcontext"
...@@ -59,3 +61,7 @@ func getImpl() IPFSImplementation { ...@@ -59,3 +61,7 @@ func getImpl() IPFSImplementation {
func GetObject(contentId string, ctx rcontext.RequestContext) (*ipfs_models.IPFSObject, error) { func GetObject(contentId string, ctx rcontext.RequestContext) (*ipfs_models.IPFSObject, error) {
return getImpl().GetObject(contentId, ctx) return getImpl().GetObject(contentId, ctx)
} }
func PutObject(data io.Reader, ctx rcontext.RequestContext) (string, error) {
return getImpl().PutObject(data, ctx)
}
package ipfs_proxy package ipfs_proxy
import ( import (
"io"
"github.com/turt2live/matrix-media-repo/common/rcontext" "github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/ipfs_proxy/ipfs_models" "github.com/turt2live/matrix-media-repo/ipfs_proxy/ipfs_models"
) )
type IPFSImplementation interface { type IPFSImplementation interface {
GetObject(contentId string, ctx rcontext.RequestContext) (*ipfs_models.IPFSObject, error) GetObject(contentId string, ctx rcontext.RequestContext) (*ipfs_models.IPFSObject, error)
PutObject(data io.Reader, ctx rcontext.RequestContext) (string, error)
Stop() Stop()
} }
...@@ -3,6 +3,7 @@ package ipfs_embedded ...@@ -3,6 +3,7 @@ package ipfs_embedded
import ( import (
"bytes" "bytes"
"context" "context"
"io"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sync" "sync"
...@@ -10,6 +11,7 @@ import ( ...@@ -10,6 +11,7 @@ import (
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
ipfsConfig "github.com/ipfs/go-ipfs-config" ipfsConfig "github.com/ipfs/go-ipfs-config"
files "github.com/ipfs/go-ipfs-files"
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi" "github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/node/libp2p" "github.com/ipfs/go-ipfs/core/node/libp2p"
...@@ -27,14 +29,18 @@ import ( ...@@ -27,14 +29,18 @@ import (
) )
type IPFSEmbedded struct { type IPFSEmbedded struct {
api icore.CoreAPI api icore.CoreAPI
node *core.IpfsNode node *core.IpfsNode
ctx context.Context
cancelCtxFn context.CancelFunc
} }
func NewEmbeddedIPFSNode() (IPFSEmbedded, error) { func NewEmbeddedIPFSNode() (IPFSEmbedded, error) {
// Startup routine modified from: // Startup routine modified from:
// https://github.com/ipfs/go-ipfs/blob/083ef47ce84a5bd9a93f0ce0afaf668881dc1f35/docs/examples/go-ipfs-as-a-library/main.go // https://github.com/ipfs/go-ipfs/blob/083ef47ce84a5bd9a93f0ce0afaf668881dc1f35/docs/examples/go-ipfs-as-a-library/main.go
ctx, cancel := context.WithCancel(context.Background())
blank := IPFSEmbedded{} blank := IPFSEmbedded{}
// Load plugins // Load plugins
...@@ -85,7 +91,7 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) { ...@@ -85,7 +91,7 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) {
} }
logrus.Info("Building IPFS embedded node") logrus.Info("Building IPFS embedded node")
node, err := core.NewNode(context.Background(), nodeOptions) node, err := core.NewNode(ctx, nodeOptions)
if err != nil { if err != nil {
return blank, err return blank, err
} }
...@@ -135,7 +141,7 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) { ...@@ -135,7 +141,7 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) {
for _, peerInfo := range peerInfos { for _, peerInfo := range peerInfos {
go func(peerInfo *peerstore.PeerInfo) { go func(peerInfo *peerstore.PeerInfo) {
defer wg.Done() defer wg.Done()
err := api.Swarm().Connect(context.Background(), *peerInfo) err := api.Swarm().Connect(ctx, *peerInfo)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} else { } else {
...@@ -147,8 +153,10 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) { ...@@ -147,8 +153,10 @@ func NewEmbeddedIPFSNode() (IPFSEmbedded, error) {
logrus.Info("Done building IPFS embedded node") logrus.Info("Done building IPFS embedded node")
return IPFSEmbedded{ return IPFSEmbedded{
api: api, api: api,
node: node, node: node,
ctx: ctx,
cancelCtxFn: cancel,
}, nil }, nil
} }
...@@ -160,7 +168,7 @@ func (i IPFSEmbedded) GetObject(contentId string, ctx rcontext.RequestContext) ( ...@@ -160,7 +168,7 @@ func (i IPFSEmbedded) GetObject(contentId string, ctx rcontext.RequestContext) (
} }
ctx.Log.Info("Resolving path and node") ctx.Log.Info("Resolving path and node")
timeoutCtx, cancel := context.WithTimeout(ctx.Context, 10 * time.Second) timeoutCtx, cancel := context.WithTimeout(ctx.Context, 10*time.Second)
defer cancel() defer cancel()
ipfsPath := icorepath.IpfsPath(ipfsCid) ipfsPath := icorepath.IpfsPath(ipfsCid)
node, err := i.api.ResolveNode(timeoutCtx, ipfsPath) node, err := i.api.ResolveNode(timeoutCtx, ipfsPath)
...@@ -177,6 +185,16 @@ func (i IPFSEmbedded) GetObject(contentId string, ctx rcontext.RequestContext) ( ...@@ -177,6 +185,16 @@ func (i IPFSEmbedded) GetObject(contentId string, ctx rcontext.RequestContext) (
}, nil }, nil
} }
func (i IPFSEmbedded) PutObject(data io.Reader, ctx rcontext.RequestContext) (string, error) {
ipfsFile := files.NewReaderFile(data)
p, err := i.api.Unixfs().Add(ctx.Context, ipfsFile)
if err != nil {
return "", err
}
return p.Cid().String(), nil
}
func (i IPFSEmbedded) Stop() { func (i IPFSEmbedded) Stop() {
i.cancelCtxFn()
i.node.Close() i.node.Close()
} }
...@@ -2,8 +2,10 @@ package ipfs_local ...@@ -2,8 +2,10 @@ package ipfs_local
import ( import (
"bytes" "bytes"
"io"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
httpapi "github.com/ipfs/go-ipfs-http-client" httpapi "github.com/ipfs/go-ipfs-http-client"
"github.com/ipfs/interface-go-ipfs-core/path" "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/turt2live/matrix-media-repo/common/rcontext" "github.com/turt2live/matrix-media-repo/common/rcontext"
...@@ -42,6 +44,15 @@ func (i IPFSLocal) GetObject(contentId string, ctx rcontext.RequestContext) (*ip ...@@ -42,6 +44,15 @@ func (i IPFSLocal) GetObject(contentId string, ctx rcontext.RequestContext) (*ip
}, nil }, nil
} }
func (i IPFSLocal) PutObject(data io.Reader, ctx rcontext.RequestContext) (string, error) {
ipfsFile := files.NewReaderFile(data)
p, err := i.client.Unixfs().Add(ctx.Context, ipfsFile)
if err != nil {
return "", err
}
return p.Cid().String(), nil
}
func (i IPFSLocal) Stop() { func (i IPFSLocal) Stop() {
// Nothing to do // Nothing to do
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment