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