github.com/pslzym/go-ethereum@v1.8.17-0.20180926104442-4b6824e07b1b/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 func download(ctx *cli.Context) { 32 log.Debug("downloading content using swarm down") 33 args := ctx.Args() 34 dest := "." 35 36 switch len(args) { 37 case 0: 38 utils.Fatalf("Usage: swarm down [options] <bzz locator> [<destination path>]") 39 case 1: 40 log.Trace(fmt.Sprintf("swarm down: no destination path - assuming working dir")) 41 default: 42 log.Trace(fmt.Sprintf("destination path arg: %s", args[1])) 43 if absDest, err := filepath.Abs(args[1]); err == nil { 44 dest = absDest 45 } else { 46 utils.Fatalf("could not get download path: %v", err) 47 } 48 } 49 50 var ( 51 bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") 52 isRecursive = ctx.Bool(SwarmRecursiveFlag.Name) 53 client = swarm.NewClient(bzzapi) 54 ) 55 56 if fi, err := os.Stat(dest); err == nil { 57 if isRecursive && !fi.Mode().IsDir() { 58 utils.Fatalf("destination path is not a directory!") 59 } 60 } else { 61 if !os.IsNotExist(err) { 62 utils.Fatalf("could not stat path: %v", err) 63 } 64 } 65 66 uri, err := api.Parse(args[0]) 67 if err != nil { 68 utils.Fatalf("could not parse uri argument: %v", err) 69 } 70 71 dl := func(credentials string) error { 72 // assume behaviour according to --recursive switch 73 if isRecursive { 74 if err := client.DownloadDirectory(uri.Addr, uri.Path, dest, credentials); err != nil { 75 if err == swarm.ErrUnauthorized { 76 return err 77 } 78 return fmt.Errorf("directory %s: %v", uri.Path, err) 79 } 80 } else { 81 // we are downloading a file 82 log.Debug("downloading file/path from a manifest", "uri.Addr", uri.Addr, "uri.Path", uri.Path) 83 84 err := client.DownloadFile(uri.Addr, uri.Path, dest, credentials) 85 if err != nil { 86 if err == swarm.ErrUnauthorized { 87 return err 88 } 89 return fmt.Errorf("file %s from address: %s: %v", uri.Path, uri.Addr, err) 90 } 91 } 92 return nil 93 } 94 if passwords := makePasswordList(ctx); passwords != nil { 95 password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), 0, passwords) 96 err = dl(password) 97 } else { 98 err = dl("") 99 } 100 if err != nil { 101 utils.Fatalf("download: %v", err) 102 } 103 }