upload.go 4.8 KB

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