From b8a3caa70b8053ae98bc2a74a282b283e997ba75 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 22 Feb 2019 16:37:04 +0800 Subject: [PATCH] add ipfs client --- blockchain/txbuilder/actions.go | 55 +++++++++++++++++++++++++++++ vendor/github.com/ipfs/go-ipfs-api/shell.go | 4 +-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/blockchain/txbuilder/actions.go b/blockchain/txbuilder/actions.go index 6c4ee849..13268be1 100644 --- a/blockchain/txbuilder/actions.go +++ b/blockchain/txbuilder/actions.go @@ -4,7 +4,12 @@ import ( "context" stdjson "encoding/json" "errors" + "fmt" + "io" + "os" + "strings" + ipfs "github.com/ipfs/go-ipfs-api" "github.com/vapor/common" "github.com/vapor/consensus" "github.com/vapor/encoding/json" @@ -137,3 +142,53 @@ func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error { func (a *retireAction) ActionType() string { return "retire" } + +const ( + file uint32 = iota + data +) + +type dataAction struct { + Type uint32 `json:"type"` + Data string `json:"data"` +} + +func (a *dataAction) Build(ctx context.Context, b *TemplateBuilder) error { + + var r io.Reader + + switch a.Type { + case file: + // 检查文件是否存在 + fi, err := os.Stat(a.Data) + if os.IsNotExist(err) { + return err + } + if fi.IsDir() { + return fmt.Errorf("data [%s] is directory", a.Data) + } + r, err = os.Open(a.Data) + if err != nil { + return err + } + + case data: + if a.Data == "" { + return errors.New("data is empty") + } + // 生成文件对象 + r = strings.NewReader(a.Data) + default: + } + + // 连接ipfs节点 + sh := ipfs.NewShell("localhost:5001") + cid, err := sh.Add(r) + if err != nil { + return err + } + + fmt.Println(cid) + + return nil +} diff --git a/vendor/github.com/ipfs/go-ipfs-api/shell.go b/vendor/github.com/ipfs/go-ipfs-api/shell.go index f0b24f87..1c44e027 100644 --- a/vendor/github.com/ipfs/go-ipfs-api/shell.go +++ b/vendor/github.com/ipfs/go-ipfs-api/shell.go @@ -20,8 +20,6 @@ import ( ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" tar "github.com/whyrusleeping/tar-utils" - - p2pmetrics "github.com/libp2p/go-libp2p-metrics" ) const ( @@ -474,11 +472,13 @@ func (s *Shell) ObjectStat(key string) (*ObjectStats, error) { // ObjectStat gets stats for the DAG object named by key. It returns // the stats of the requested Object or an error. +/* func (s *Shell) StatsBW(ctx context.Context) (*p2pmetrics.Stats, error) { v := &p2pmetrics.Stats{} err := s.Request("stats/bw").Exec(ctx, &v) return v, err } +*/ type SwarmStreamInfo struct { Protocol string -- 2.11.0