2016-11-11 01:25:24 +03: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 (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-12-13 14:48:30 +03:00
|
|
|
"os/user"
|
|
|
|
"path"
|
2016-11-11 01:25:24 +03:00
|
|
|
"strings"
|
2016-12-10 20:45:52 +03:00
|
|
|
|
2017-03-02 16:06:16 +03:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2017-04-05 01:20:07 +03:00
|
|
|
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
2016-12-10 20:45:52 +03:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2016-11-11 01:25:24 +03:00
|
|
|
)
|
|
|
|
|
2016-12-10 20:45:52 +03:00
|
|
|
func upload(ctx *cli.Context) {
|
|
|
|
args := ctx.Args()
|
2016-11-11 01:25:24 +03:00
|
|
|
var (
|
2016-12-13 11:51:42 +03:00
|
|
|
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
|
2016-12-10 20:45:52 +03:00
|
|
|
recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name)
|
|
|
|
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
|
2016-12-13 14:48:30 +03:00
|
|
|
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
|
2016-11-11 01:25:24 +03:00
|
|
|
)
|
2016-12-10 20:45:52 +03:00
|
|
|
if len(args) != 1 {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Need filename as the first and only argument")
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-12-10 20:45:52 +03:00
|
|
|
file = args[0]
|
2017-04-05 01:20:07 +03:00
|
|
|
client = swarm.NewClient(bzzapi)
|
2016-11-11 01:25:24 +03:00
|
|
|
)
|
2016-12-13 14:48:30 +03:00
|
|
|
fi, err := os.Stat(expandPath(file))
|
2016-11-11 01:25:24 +03:00
|
|
|
if err != nil {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Failed to stat file: %v", err)
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2016-12-10 20:45:52 +03:00
|
|
|
if !recursive {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Argument is a directory and recursive upload is disabled")
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
2017-01-09 17:19:56 +03:00
|
|
|
if !wantManifest {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Manifest is required for directory uploads")
|
2017-01-09 17:19:56 +03:00
|
|
|
}
|
2017-04-05 01:20:07 +03:00
|
|
|
mhash, err := client.UploadDirectory(file, defaultPath)
|
2017-01-09 17:19:56 +03:00
|
|
|
if err != nil {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Failed to upload directory: %v", err)
|
2017-01-09 17:19:56 +03:00
|
|
|
}
|
|
|
|
fmt.Println(mhash)
|
|
|
|
return
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
2017-04-05 01:20:07 +03:00
|
|
|
entry, err := client.UploadFile(file, fi)
|
2016-11-11 01:25:24 +03:00
|
|
|
if err != nil {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Upload failed: %v", err)
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
2017-04-05 01:20:07 +03:00
|
|
|
mroot := swarm.Manifest{Entries: []swarm.ManifestEntry{entry}}
|
2016-12-13 14:48:30 +03:00
|
|
|
if !wantManifest {
|
|
|
|
// Print the manifest. This is the only output to stdout.
|
|
|
|
mrootJSON, _ := json.MarshalIndent(mroot, "", " ")
|
|
|
|
fmt.Println(string(mrootJSON))
|
|
|
|
return
|
|
|
|
}
|
2017-04-05 01:20:07 +03:00
|
|
|
hash, err := client.UploadManifest(mroot)
|
2016-12-13 14:48:30 +03:00
|
|
|
if err != nil {
|
2017-03-02 16:06:16 +03:00
|
|
|
utils.Fatalf("Manifest upload failed: %v", err)
|
2016-12-13 14:48:30 +03: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 {
|
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
|
|
|
if home := homeDir(); home != "" {
|
|
|
|
p = home + p[1:]
|
2016-11-11 01:25:24 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:48:30 +03:00
|
|
|
return path.Clean(os.ExpandEnv(p))
|
|
|
|
}
|
2016-11-11 01:25:24 +03:00
|
|
|
|
2016-12-13 14:48:30 +03: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-11 01:25:24 +03:00
|
|
|
}
|