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