github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/swarm/download.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/ethereum/go-ethereum/cmd/utils"
    25  	"github.com/ethereum/go-ethereum/log"
    26  	"github.com/ethereum/go-ethereum/swarm/api"
    27  	swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    28  	"gopkg.in/urfave/cli.v1"
    29  )
    30  
    31  var downloadCommand = cli.Command{
    32  	Action:      download,
    33  	Name:        "down",
    34  	Flags:       []cli.Flag{SwarmRecursiveFlag, SwarmAccessPasswordFlag},
    35  	Usage:       "downloads a swarm manifest or a file inside a manifest",
    36  	ArgsUsage:   " <uri> [<dir>]",
    37  	Description: `Downloads a swarm bzz uri to the given dir. When no dir is provided, working directory is assumed. --recursive flag is expected when downloading a manifest with multiple entries.`,
    38  }
    39  
    40  func download(ctx *cli.Context) {
    41  	log.Debug("downloading content using swarm down")
    42  	args := ctx.Args()
    43  	dest := "."
    44  
    45  	switch len(args) {
    46  	case 0:
    47  		utils.Fatalf("Usage: swarm down [options] <bzz locator> [<destination path>]")
    48  	case 1:
    49  		log.Trace(fmt.Sprintf("swarm down: no destination path - assuming working dir"))
    50  	default:
    51  		log.Trace(fmt.Sprintf("destination path arg: %s", args[1]))
    52  		if absDest, err := filepath.Abs(args[1]); err == nil {
    53  			dest = absDest
    54  		} else {
    55  			utils.Fatalf("could not get download path: %v", err)
    56  		}
    57  	}
    58  
    59  	var (
    60  		bzzapi      = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    61  		isRecursive = ctx.Bool(SwarmRecursiveFlag.Name)
    62  		client      = swarm.NewClient(bzzapi)
    63  	)
    64  
    65  	if fi, err := os.Stat(dest); err == nil {
    66  		if isRecursive && !fi.Mode().IsDir() {
    67  			utils.Fatalf("destination path is not a directory!")
    68  		}
    69  	} else {
    70  		if !os.IsNotExist(err) {
    71  			utils.Fatalf("could not stat path: %v", err)
    72  		}
    73  	}
    74  
    75  	uri, err := api.Parse(args[0])
    76  	if err != nil {
    77  		utils.Fatalf("could not parse uri argument: %v", err)
    78  	}
    79  
    80  	dl := func(credentials string) error {
    81  		// assume behaviour according to --recursive switch
    82  		if isRecursive {
    83  			if err := client.DownloadDirectory(uri.Addr, uri.Path, dest, credentials); err != nil {
    84  				if err == swarm.ErrUnauthorized {
    85  					return err
    86  				}
    87  				return fmt.Errorf("directory %s: %v", uri.Path, err)
    88  			}
    89  		} else {
    90  			// we are downloading a file
    91  			log.Debug("downloading file/path from a manifest", "uri.Addr", uri.Addr, "uri.Path", uri.Path)
    92  
    93  			err := client.DownloadFile(uri.Addr, uri.Path, dest, credentials)
    94  			if err != nil {
    95  				if err == swarm.ErrUnauthorized {
    96  					return err
    97  				}
    98  				return fmt.Errorf("file %s from address: %s: %v", uri.Path, uri.Addr, err)
    99  			}
   100  		}
   101  		return nil
   102  	}
   103  	if passwords := makePasswordList(ctx); passwords != nil {
   104  		password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), 0, passwords)
   105  		err = dl(password)
   106  	} else {
   107  		err = dl("")
   108  	}
   109  	if err != nil {
   110  		utils.Fatalf("download: %v", err)
   111  	}
   112  }