upload.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(SwarmRecursiveUploadFlag.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. file string
  45. )
  46. if len(args) != 1 {
  47. if fromStdin {
  48. tmp, err := ioutil.TempFile("", "swarm-stdin")
  49. if err != nil {
  50. utils.Fatalf("error create tempfile: %s", err)
  51. }
  52. defer os.Remove(tmp.Name())
  53. n, err := io.Copy(tmp, os.Stdin)
  54. if err != nil {
  55. utils.Fatalf("error copying stdin to tempfile: %s", err)
  56. } else if n == 0 {
  57. utils.Fatalf("error reading from stdin: zero length")
  58. }
  59. file = tmp.Name()
  60. } else {
  61. utils.Fatalf("Need filename as the first and only argument")
  62. }
  63. } else {
  64. file = expandPath(args[0])
  65. }
  66. if !wantManifest {
  67. f, err := swarm.Open(file)
  68. if err != nil {
  69. utils.Fatalf("Error opening file: %s", err)
  70. }
  71. defer f.Close()
  72. hash, err := client.UploadRaw(f, f.Size)
  73. if err != nil {
  74. utils.Fatalf("Upload failed: %s", err)
  75. }
  76. fmt.Println(hash)
  77. return
  78. }
  79. stat, err := os.Stat(file)
  80. if err != nil {
  81. utils.Fatalf("Error opening file: %s", err)
  82. }
  83. // define a function which either uploads a directory or single file
  84. // based on the type of the file being uploaded
  85. var doUpload func() (hash string, err error)
  86. if stat.IsDir() {
  87. doUpload = func() (string, error) {
  88. if !recursive {
  89. return "", errors.New("Argument is a directory and recursive upload is disabled")
  90. }
  91. return client.UploadDirectory(file, defaultPath, "")
  92. }
  93. } else {
  94. doUpload = func() (string, error) {
  95. f, err := swarm.Open(file)
  96. if err != nil {
  97. return "", fmt.Errorf("error opening file: %s", err)
  98. }
  99. defer f.Close()
  100. if mimeType == "" {
  101. mimeType = detectMimeType(file)
  102. }
  103. f.ContentType = mimeType
  104. return client.Upload(f, "")
  105. }
  106. }
  107. hash, err := doUpload()
  108. if err != nil {
  109. utils.Fatalf("Upload failed: %s", err)
  110. }
  111. fmt.Println(hash)
  112. }
  113. // Expands a file path
  114. // 1. replace tilde with users home dir
  115. // 2. expands embedded environment variables
  116. // 3. cleans the path, e.g. /a/b/../c -> /a/c
  117. // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
  118. func expandPath(p string) string {
  119. if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
  120. if home := homeDir(); home != "" {
  121. p = home + p[1:]
  122. }
  123. }
  124. return path.Clean(os.ExpandEnv(p))
  125. }
  126. func homeDir() string {
  127. if home := os.Getenv("HOME"); home != "" {
  128. return home
  129. }
  130. if usr, err := user.Current(); err == nil {
  131. return usr.HomeDir
  132. }
  133. return ""
  134. }
  135. func detectMimeType(file string) string {
  136. if ext := filepath.Ext(file); ext != "" {
  137. return mime.TypeByExtension(ext)
  138. }
  139. f, err := os.Open(file)
  140. if err != nil {
  141. return ""
  142. }
  143. defer f.Close()
  144. buf := make([]byte, 512)
  145. if n, _ := f.Read(buf); n > 0 {
  146. return http.DetectContentType(buf)
  147. }
  148. return ""
  149. }