OSDN Git Service

add ipfs client
authormars <mars@bytom.io>
Fri, 22 Feb 2019 08:37:04 +0000 (16:37 +0800)
committermars <mars@bytom.io>
Fri, 22 Feb 2019 08:37:04 +0000 (16:37 +0800)
blockchain/txbuilder/actions.go
vendor/github.com/ipfs/go-ipfs-api/shell.go

index 6c4ee84..13268be 100644 (file)
@@ -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
+}
index f0b24f8..1c44e02 100644 (file)
@@ -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