2016-11-10 22:25:24 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Command bzzup uploads files to the swarm HTTP API.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-06 07:39:10 +00:00
|
|
|
"errors"
|
2016-11-10 22:25:24 +00:00
|
|
|
"fmt"
|
2017-04-06 12:21:16 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-11-10 22:25:24 +00:00
|
|
|
"os"
|
2016-12-13 11:48:30 +00:00
|
|
|
"os/user"
|
|
|
|
"path"
|
2017-04-06 22:22:22 +00:00
|
|
|
"path/filepath"
|
2016-11-10 22:25:24 +00:00
|
|
|
"strings"
|
2016-12-10 17:45:52 +00:00
|
|
|
|
2017-04-04 22:20:07 +00:00
|
|
|
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
2018-10-01 11:39:39 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2016-12-10 17:45:52 +00:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2016-11-10 22:25:24 +00:00
|
|
|
)
|
|
|
|
|
2018-10-12 12:51:38 +00:00
|
|
|
var upCommand = cli.Command{
|
|
|
|
Action: upload,
|
|
|
|
CustomHelpTemplate: helpTemplate,
|
|
|
|
Name: "up",
|
|
|
|
Usage: "uploads a file or directory to swarm using the HTTP API",
|
|
|
|
ArgsUsage: "<file>",
|
|
|
|
Flags: []cli.Flag{SwarmEncryptedFlag},
|
|
|
|
Description: "uploads a file or directory to swarm using the HTTP API and prints the root hash",
|
|
|
|
}
|
2017-04-06 12:21:16 +00:00
|
|
|
|
2018-10-12 12:51:38 +00:00
|
|
|
func upload(ctx *cli.Context) {
|
2016-12-10 17:45:52 +00:00
|
|
|
args := ctx.Args()
|
2016-11-10 22:25:24 +00:00
|
|
|
var (
|
2016-12-13 08:51:42 +00:00
|
|
|
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
|
2018-06-20 12:06:27 +00:00
|
|
|
recursive = ctx.GlobalBool(SwarmRecursiveFlag.Name)
|
2016-12-10 17:45:52 +00:00
|
|
|
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
|
2016-12-13 11:48:30 +00:00
|
|
|
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
|
2017-04-06 12:21:16 +00:00
|
|
|
fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
|
|
|
|
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
|
2017-04-06 22:22:22 +00:00
|
|
|
client = swarm.NewClient(bzzapi)
|
2018-06-20 12:06:27 +00:00
|
|
|
toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name)
|
2017-04-06 22:22:22 +00:00
|
|
|
file string
|
2016-11-10 22:25:24 +00:00
|
|
|
)
|
2017-04-06 12:21:16 +00:00
|
|
|
|
2016-12-10 17:45:52 +00:00
|
|
|
if len(args) != 1 {
|
2017-04-06 12:21:16 +00:00
|
|
|
if fromStdin {
|
|
|
|
tmp, err := ioutil.TempFile("", "swarm-stdin")
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("error create tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tmp.Name())
|
|
|
|
n, err := io.Copy(tmp, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("error copying stdin to tempfile: %s", err)
|
|
|
|
} else if n == 0 {
|
|
|
|
utils.Fatalf("error reading from stdin: zero length")
|
|
|
|
}
|
|
|
|
file = tmp.Name()
|
|
|
|
} else {
|
|
|
|
utils.Fatalf("Need filename as the first and only argument")
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-06 22:22:22 +00:00
|
|
|
file = expandPath(args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if !wantManifest {
|
|
|
|
f, err := swarm.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error opening file: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-06-20 12:06:27 +00:00
|
|
|
hash, err := client.UploadRaw(f, f.Size, toEncrypt)
|
2017-04-06 22:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Upload failed: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println(hash)
|
|
|
|
return
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 22:22:22 +00:00
|
|
|
stat, err := os.Stat(file)
|
2016-11-10 22:25:24 +00:00
|
|
|
if err != nil {
|
2017-04-06 22:22:22 +00:00
|
|
|
utils.Fatalf("Error opening file: %s", err)
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|
2017-06-06 07:39:10 +00:00
|
|
|
|
|
|
|
// define a function which either uploads a directory or single file
|
|
|
|
// based on the type of the file being uploaded
|
|
|
|
var doUpload func() (hash string, err error)
|
2017-04-06 22:22:22 +00:00
|
|
|
if stat.IsDir() {
|
2017-06-06 07:39:10 +00:00
|
|
|
doUpload = func() (string, error) {
|
|
|
|
if !recursive {
|
|
|
|
return "", errors.New("Argument is a directory and recursive upload is disabled")
|
|
|
|
}
|
2018-08-10 14:12:55 +00:00
|
|
|
if defaultPath != "" {
|
|
|
|
// construct absolute default path
|
|
|
|
absDefaultPath, _ := filepath.Abs(defaultPath)
|
|
|
|
absFile, _ := filepath.Abs(file)
|
|
|
|
// make sure absolute directory ends with only one "/"
|
|
|
|
// to trim it from absolute default path and get relative default path
|
|
|
|
absFile = strings.TrimRight(absFile, "/") + "/"
|
|
|
|
if absDefaultPath != "" && absFile != "" && strings.HasPrefix(absDefaultPath, absFile) {
|
|
|
|
defaultPath = strings.TrimPrefix(absDefaultPath, absFile)
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 12:06:27 +00:00
|
|
|
return client.UploadDirectory(file, defaultPath, "", toEncrypt)
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|
2017-04-06 22:22:22 +00:00
|
|
|
} else {
|
2017-06-06 07:39:10 +00:00
|
|
|
doUpload = func() (string, error) {
|
|
|
|
f, err := swarm.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error opening file: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-10-01 11:39:39 +00:00
|
|
|
if mimeType != "" {
|
|
|
|
f.ContentType = mimeType
|
2017-06-06 07:39:10 +00:00
|
|
|
}
|
2018-06-20 12:06:27 +00:00
|
|
|
return client.Upload(f, "", toEncrypt)
|
2017-01-09 14:19:56 +00:00
|
|
|
}
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|
2017-06-06 07:39:10 +00:00
|
|
|
hash, err := doUpload()
|
2016-11-10 22:25:24 +00:00
|
|
|
if err != nil {
|
2017-04-06 22:22:22 +00:00
|
|
|
utils.Fatalf("Upload failed: %s", err)
|
2016-12-13 11:48:30 +00:00
|
|
|
}
|
|
|
|
fmt.Println(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expands a file path
|
|
|
|
// 1. replace tilde with users home dir
|
|
|
|
// 2. expands embedded environment variables
|
|
|
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
|
|
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
|
|
|
func expandPath(p string) string {
|
2018-09-25 12:54:47 +00:00
|
|
|
if i := strings.Index(p, ":"); i > 0 {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
if i := strings.Index(p, "@"); i > 0 {
|
|
|
|
return p
|
|
|
|
}
|
2016-12-13 11:48:30 +00:00
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
|
|
|
if home := homeDir(); home != "" {
|
|
|
|
p = home + p[1:]
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 11:48:30 +00:00
|
|
|
return path.Clean(os.ExpandEnv(p))
|
|
|
|
}
|
2016-11-10 22:25:24 +00:00
|
|
|
|
2016-12-13 11:48:30 +00:00
|
|
|
func homeDir() string {
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
if usr, err := user.Current(); err == nil {
|
|
|
|
return usr.HomeDir
|
|
|
|
}
|
|
|
|
return ""
|
2016-11-10 22:25:24 +00:00
|
|
|
}
|