upload.go 4.8 KB

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