aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/upload.go
blob: 8ba0e7c5f0c0289210e0242d9e38c554f047b097 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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 (
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "mime"
    "net/http"
    "os"
    "os/user"
    "path"
    "path/filepath"
    "strings"

    "github.com/ethereum/go-ethereum/cmd/utils"
    swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    "gopkg.in/urfave/cli.v1"
)

func upload(ctx *cli.Context) {

    args := ctx.Args()
    var (
        bzzapi       = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
        recursive    = ctx.GlobalBool(SwarmRecursiveFlag.Name)
        wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
        defaultPath  = ctx.GlobalString(SwarmUploadDefaultPath.Name)
        fromStdin    = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
        mimeType     = ctx.GlobalString(SwarmUploadMimeType.Name)
        client       = swarm.NewClient(bzzapi)
        toEncrypt    = ctx.Bool(SwarmEncryptedFlag.Name)
        file         string
    )

    if len(args) != 1 {
        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 {
        file = expandPath(args[0])
    }

    if !wantManifest {
        f, err := swarm.Open(file)
        if err != nil {
            utils.Fatalf("Error opening file: %s", err)
        }
        defer f.Close()
        hash, err := client.UploadRaw(f, f.Size, toEncrypt)
        if err != nil {
            utils.Fatalf("Upload failed: %s", err)
        }
        fmt.Println(hash)
        return
    }

    stat, err := os.Stat(file)
    if err != nil {
        utils.Fatalf("Error opening file: %s", err)
    }

    // 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)
    if stat.IsDir() {
        doUpload = func() (string, error) {
            if !recursive {
                return "", errors.New("Argument is a directory and recursive upload is disabled")
            }
            return client.UploadDirectory(file, defaultPath, "", toEncrypt)
        }
    } else {
        doUpload = func() (string, error) {
            f, err := swarm.Open(file)
            if err != nil {
                return "", fmt.Errorf("error opening file: %s", err)
            }
            defer f.Close()
            if mimeType == "" {
                mimeType = detectMimeType(file)
            }
            f.ContentType = mimeType
            return client.Upload(f, "", toEncrypt)
        }
    }
    hash, err := doUpload()
    if err != nil {
        utils.Fatalf("Upload failed: %s", err)
    }
    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:]
        }
    }
    return path.Clean(os.ExpandEnv(p))
}

func homeDir() string {
    if home := os.Getenv("HOME"); home != "" {
        return home
    }
    if usr, err := user.Current(); err == nil {
        return usr.HomeDir
    }
    return ""
}

func detectMimeType(file string) string {
    if ext := filepath.Ext(file); ext != "" {
        return mime.TypeByExtension(ext)
    }
    f, err := os.Open(file)
    if err != nil {
        return ""
    }
    defer f.Close()
    buf := make([]byte, 512)
    if n, _ := f.Read(buf); n > 0 {
        return http.DetectContentType(buf)
    }
    return ""
}