github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/swarm/hash.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:31</date>
    10  //</624342605743788032>
    11  
    12  
    13  //
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/ethereum/go-ethereum/cmd/utils"
    22  	"github.com/ethereum/go-ethereum/swarm/storage"
    23  	"gopkg.in/urfave/cli.v1"
    24  )
    25  
    26  func hash(ctx *cli.Context) {
    27  	args := ctx.Args()
    28  	if len(args) < 1 {
    29  		utils.Fatalf("Usage: swarm hash <file name>")
    30  	}
    31  	f, err := os.Open(args[0])
    32  	if err != nil {
    33  		utils.Fatalf("Error opening file " + args[1])
    34  	}
    35  	defer f.Close()
    36  
    37  	stat, _ := f.Stat()
    38  	fileStore := storage.NewFileStore(storage.NewMapChunkStore(), storage.NewFileStoreParams())
    39  	addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
    40  	if err != nil {
    41  		utils.Fatalf("%v\n", err)
    42  	} else {
    43  		fmt.Printf("%v\n", addr)
    44  	}
    45  }
    46