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