download.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // assume behaviour according to --recursive switch
  64. if isRecursive {
  65. if err := client.DownloadDirectory(uri.Addr, uri.Path, dest); err != nil {
  66. utils.Fatalf("encoutered an error while downloading directory: %v", err)
  67. }
  68. } else {
  69. // we are downloading a file
  70. log.Debug(fmt.Sprintf("downloading file/path from a manifest. hash: %s, path:%s", uri.Addr, uri.Path))
  71. err := client.DownloadFile(uri.Addr, uri.Path, dest)
  72. if err != nil {
  73. utils.Fatalf("could not download %s from given address: %s. error: %v", uri.Path, uri.Addr, err)
  74. }
  75. }
  76. }