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