download.go 3.0 KB

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