aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/download.go
blob: c2418f744c8487640fc8420a600e5ed5ba137756 (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
// Copyright 2018 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/>.
package main

import (
    "fmt"
    "os"
    "path/filepath"
    "strings"

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

func download(ctx *cli.Context) {
    log.Debug("downloading content using swarm down")
    args := ctx.Args()
    dest := "."

    switch len(args) {
    case 0:
        utils.Fatalf("Usage: swarm down [options] <bzz locator> [<destination path>]")
    case 1:
        log.Trace(fmt.Sprintf("swarm down: no destination path - assuming working dir"))
    default:
        log.Trace(fmt.Sprintf("destination path arg: %s", args[1]))
        if absDest, err := filepath.Abs(args[1]); err == nil {
            dest = absDest
        } else {
            utils.Fatalf("could not get download path: %v", err)
        }
    }

    var (
        bzzapi      = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
        isRecursive = ctx.Bool(SwarmRecursiveFlag.Name)
        client      = swarm.NewClient(bzzapi)
    )

    if fi, err := os.Stat(dest); err == nil {
        if isRecursive && !fi.Mode().IsDir() {
            utils.Fatalf("destination path is not a directory!")
        }
    } else {
        if !os.IsNotExist(err) {
            utils.Fatalf("could not stat path: %v", err)
        }
    }

    uri, err := api.Parse(args[0])
    if err != nil {
        utils.Fatalf("could not parse uri argument: %v", err)
    }

    // assume behaviour according to --recursive switch
    if isRecursive {
        if err := client.DownloadDirectory(uri.Addr, uri.Path, dest); err != nil {
            utils.Fatalf("encoutered an error while downloading directory: %v", err)
        }
    } else {
        // we are downloading a file
        log.Debug(fmt.Sprintf("downloading file/path from a manifest. hash: %s, path:%s", uri.Addr, uri.Path))

        err := client.DownloadFile(uri.Addr, uri.Path, dest)
        if err != nil {
            utils.Fatalf("could not download %s from given address: %s. error: %v", uri.Path, uri.Addr, err)
        }
    }
}