From b257edde84d145bd32a561adfb0f0dafc1b55653 Mon Sep 17 00:00:00 2001 From: b5 <sparkle_pony_2000@qri.io> Date: Thu, 24 Sep 2020 12:04:12 -0400 Subject: [PATCH] feat(ipfs_http): support PUT on IPFS over http --- qipfs/qipfs_http/filestore.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/qipfs/qipfs_http/filestore.go b/qipfs/qipfs_http/filestore.go index 1ff967f..8aaf31f 100644 --- a/qipfs/qipfs_http/filestore.go +++ b/qipfs/qipfs_http/filestore.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "strings" logging "github.com/ipfs/go-log" coreiface "github.com/ipfs/interface-go-ipfs-core" @@ -93,8 +94,13 @@ func (fst *Filestore) Fetch(ctx context.Context, source cafs.Source, key string) return fst.getKey(ctx, key) } -func (fst *Filestore) Put(ctx context.Context, file qfs.File) (key string, err error) { - return "", fmt.Errorf("ipfs_http cannot put") +func (fst *Filestore) Put(ctx context.Context, file qfs.File) (string, error) { + resolvedPath, err := fst.capi.Unixfs().Add(ctx, wrapFile{file}) + if err != nil { + return "", fmt.Errorf("putting file in IPFS via HTTP: %q", err) + } + + return pathFromHash(resolvedPath.String()), nil } func (fst *Filestore) Delete(ctx context.Context, key string) error { @@ -129,6 +135,9 @@ func (fst *Filestore) NewAdder(pin, wrap bool) (cafs.Adder, error) { } func pathFromHash(hash string) string { + if strings.HasPrefix(hash, fmt.Sprintf("/%s", FilestoreType)) { + return hash + } return fmt.Sprintf("/%s/%s", FilestoreType, hash) }