download.go 3.4 KB

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