github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/download.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  package main
    25  
    26  import (
    27  	"fmt"
    28  	"os"
    29  	"path/filepath"
    30  	"strings"
    31  
    32  	"github.com/ethereum/go-ethereum/cmd/utils"
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/swarm/api"
    35  	swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    36  	"gopkg.in/urfave/cli.v1"
    37  )
    38  
    39  func download(ctx *cli.Context) {
    40  	log.Debug("downloading content using swarm down")
    41  	args := ctx.Args()
    42  	dest := "."
    43  
    44  	switch len(args) {
    45  	case 0:
    46  		utils.Fatalf("Usage: swarm down [options] <bzz locator> [<destination path>]")
    47  	case 1:
    48  		log.Trace(fmt.Sprintf("swarm down: no destination path - assuming working dir"))
    49  	default:
    50  		log.Trace(fmt.Sprintf("destination path arg: %s", args[1]))
    51  		if absDest, err := filepath.Abs(args[1]); err == nil {
    52  			dest = absDest
    53  		} else {
    54  			utils.Fatalf("could not get download path: %v", err)
    55  		}
    56  	}
    57  
    58  	var (
    59  		bzzapi      = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    60  		isRecursive = ctx.Bool(SwarmRecursiveFlag.Name)
    61  		client      = swarm.NewClient(bzzapi)
    62  	)
    63  
    64  	if fi, err := os.Stat(dest); err == nil {
    65  		if isRecursive && !fi.Mode().IsDir() {
    66  			utils.Fatalf("destination path is not a directory!")
    67  		}
    68  	} else {
    69  		if !os.IsNotExist(err) {
    70  			utils.Fatalf("could not stat path: %v", err)
    71  		}
    72  	}
    73  
    74  	uri, err := api.Parse(args[0])
    75  	if err != nil {
    76  		utils.Fatalf("could not parse uri argument: %v", err)
    77  	}
    78  
    79  	dl := func(credentials string) error {
    80  //
    81  		if isRecursive {
    82  			if err := client.DownloadDirectory(uri.Addr, uri.Path, dest, credentials); err != nil {
    83  				if err == swarm.ErrUnauthorized {
    84  					return err
    85  				}
    86  				return fmt.Errorf("directory %s: %v", uri.Path, err)
    87  			}
    88  		} else {
    89  //
    90  			log.Debug("downloading file/path from a manifest", "uri.Addr", uri.Addr, "uri.Path", uri.Path)
    91  
    92  			err := client.DownloadFile(uri.Addr, uri.Path, dest, credentials)
    93  			if err != nil {
    94  				if err == swarm.ErrUnauthorized {
    95  					return err
    96  				}
    97  				return fmt.Errorf("file %s from address: %s: %v", uri.Path, uri.Addr, err)
    98  			}
    99  		}
   100  		return nil
   101  	}
   102  	if passwords := makePasswordList(ctx); passwords != nil {
   103  		password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), 0, passwords)
   104  		err = dl(password)
   105  	} else {
   106  		err = dl("")
   107  	}
   108  	if err != nil {
   109  		utils.Fatalf("download: %v", err)
   110  	}
   111  }