upload.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 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. // Command bzzup uploads files to the swarm HTTP API.
  17. package main
  18. import (
  19. "errors"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "os"
  24. "os/user"
  25. "path"
  26. "path/filepath"
  27. "strings"
  28. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  29. "github.com/ethereum/go-ethereum/cmd/utils"
  30. "gopkg.in/urfave/cli.v1"
  31. )
  32. func upload(ctx *cli.Context) {
  33. args := ctx.Args()
  34. var (
  35. bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
  36. recursive = ctx.GlobalBool(SwarmRecursiveFlag.Name)
  37. wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
  38. defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
  39. fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
  40. mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
  41. client = swarm.NewClient(bzzapi)
  42. toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name)
  43. file string
  44. )
  45. if len(args) != 1 {
  46. if fromStdin {
  47. tmp, err := ioutil.TempFile("", "swarm-stdin")
  48. if err != nil {
  49. utils.Fatalf("error create tempfile: %s", err)
  50. }
  51. defer os.Remove(tmp.Name())
  52. n, err := io.Copy(tmp, os.Stdin)
  53. if err != nil {
  54. utils.Fatalf("error copying stdin to tempfile: %s", err)
  55. } else if n == 0 {
  56. utils.Fatalf("error reading from stdin: zero length")
  57. }
  58. file = tmp.Name()
  59. } else {
  60. utils.Fatalf("Need filename as the first and only argument")
  61. }
  62. } else {
  63. file = expandPath(args[0])
  64. }
  65. if !wantManifest {
  66. f, err := swarm.Open(file)
  67. if err != nil {
  68. utils.Fatalf("Error opening file: %s", err)
  69. }
  70. defer f.Close()
  71. hash, err := client.UploadRaw(f, f.Size, toEncrypt)
  72. if err != nil {
  73. utils.Fatalf("Upload failed: %s", err)
  74. }
  75. fmt.Println(hash)
  76. return
  77. }
  78. stat, err := os.Stat(file)
  79. if err != nil {
  80. utils.Fatalf("Error opening file: %s", err)
  81. }
  82. // define a function which either uploads a directory or single file
  83. // based on the type of the file being uploaded
  84. var doUpload func() (hash string, err error)
  85. if stat.IsDir() {
  86. doUpload = func() (string, error) {
  87. if !recursive {
  88. return "", errors.New("Argument is a directory and recursive upload is disabled")
  89. }
  90. if defaultPath != "" {
  91. // construct absolute default path
  92. absDefaultPath, _ := filepath.Abs(defaultPath)
  93. absFile, _ := filepath.Abs(file)
  94. // make sure absolute directory ends with only one "/"
  95. // to trim it from absolute default path and get relative default path
  96. absFile = strings.TrimRight(absFile, "/") + "/"
  97. if absDefaultPath != "" && absFile != "" && strings.HasPrefix(absDefaultPath, absFile) {
  98. defaultPath = strings.TrimPrefix(absDefaultPath, absFile)
  99. }
  100. }
  101. return client.UploadDirectory(file, defaultPath, "", toEncrypt)
  102. }
  103. } else {
  104. doUpload = func() (string, error) {
  105. f, err := swarm.Open(file)
  106. if err != nil {
  107. return "", fmt.Errorf("error opening file: %s", err)
  108. }
  109. defer f.Close()
  110. if mimeType != "" {
  111. f.ContentType = mimeType
  112. }
  113. return client.Upload(f, "", toEncrypt)
  114. }
  115. }
  116. hash, err := doUpload()
  117. if err != nil {
  118. utils.Fatalf("Upload failed: %s", err)
  119. }
  120. fmt.Println(hash)
  121. }
  122. // Expands a file path
  123. // 1. replace tilde with users home dir
  124. // 2. expands embedded environment variables
  125. // 3. cleans the path, e.g. /a/b/../c -> /a/c
  126. // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
  127. func expandPath(p string) string {
  128. if i := strings.Index(p, ":"); i > 0 {
  129. return p
  130. }
  131. if i := strings.Index(p, "@"); i > 0 {
  132. return p
  133. }
  134. if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
  135. if home := homeDir(); home != "" {
  136. p = home + p[1:]
  137. }
  138. }
  139. return path.Clean(os.ExpandEnv(p))
  140. }
  141. func homeDir() string {
  142. if home := os.Getenv("HOME"); home != "" {
  143. return home
  144. }
  145. if usr, err := user.Current(); err == nil {
  146. return usr.HomeDir
  147. }
  148. return ""
  149. }