github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/swarm/download.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  //</624342605437603840>
    11  
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  	"os"
    17  	"path/filepath"
    18  	"strings"
    19  
    20  	"github.com/ethereum/go-ethereum/cmd/utils"
    21  	"github.com/ethereum/go-ethereum/log"
    22  	"github.com/ethereum/go-ethereum/swarm/api"
    23  	swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    24  	"gopkg.in/urfave/cli.v1"
    25  )
    26  
    27  func download(ctx *cli.Context) {
    28  	log.Debug("downloading content using swarm down")
    29  	args := ctx.Args()
    30  	dest := "."
    31  
    32  	switch len(args) {
    33  	case 0:
    34  		utils.Fatalf("Usage: swarm down [options] <bzz locator> [<destination path>]")
    35  	case 1:
    36  		log.Trace(fmt.Sprintf("swarm down: no destination path - assuming working dir"))
    37  	default:
    38  		log.Trace(fmt.Sprintf("destination path arg: %s", args[1]))
    39  		if absDest, err := filepath.Abs(args[1]); err == nil {
    40  			dest = absDest
    41  		} else {
    42  			utils.Fatalf("could not get download path: %v", err)
    43  		}
    44  	}
    45  
    46  	var (
    47  		bzzapi      = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    48  		isRecursive = ctx.Bool(SwarmRecursiveFlag.Name)
    49  		client      = swarm.NewClient(bzzapi)
    50  	)
    51  
    52  	if fi, err := os.Stat(dest); err == nil {
    53  		if isRecursive && !fi.Mode().IsDir() {
    54  			utils.Fatalf("destination path is not a directory!")
    55  		}
    56  	} else {
    57  		if !os.IsNotExist(err) {
    58  			utils.Fatalf("could not stat path: %v", err)
    59  		}
    60  	}
    61  
    62  	uri, err := api.Parse(args[0])
    63  	if err != nil {
    64  		utils.Fatalf("could not parse uri argument: %v", err)
    65  	}
    66  
    67  	dl := func(credentials string) error {
    68  //
    69  		if isRecursive {
    70  			if err := client.DownloadDirectory(uri.Addr, uri.Path, dest, credentials); err != nil {
    71  				if err == swarm.ErrUnauthorized {
    72  					return err
    73  				}
    74  				return fmt.Errorf("directory %s: %v", uri.Path, err)
    75  			}
    76  		} else {
    77  //
    78  			log.Debug("downloading file/path from a manifest", "uri.Addr", uri.Addr, "uri.Path", uri.Path)
    79  
    80  			err := client.DownloadFile(uri.Addr, uri.Path, dest, credentials)
    81  			if err != nil {
    82  				if err == swarm.ErrUnauthorized {
    83  					return err
    84  				}
    85  				return fmt.Errorf("file %s from address: %s: %v", uri.Path, uri.Addr, err)
    86  			}
    87  		}
    88  		return nil
    89  	}
    90  	if passwords := makePasswordList(ctx); passwords != nil {
    91  		password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), 0, passwords)
    92  		err = dl(password)
    93  	} else {
    94  		err = dl("")
    95  	}
    96  	if err != nil {
    97  		utils.Fatalf("download: %v", err)
    98  	}
    99  }
   100