forked from kothar/go-backblaze
/
put.go
147 lines (122 loc) · 3.04 KB
/
put.go
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
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/gosuri/uiprogress"
"github.com/gosuri/uiprogress/util/strutil"
"gopkg.in/kothar/go-backblaze.v0"
)
// TODO support directories
// TODO support replacing all previous versions
// Put is a command
type Put struct {
Threads int `short:"j" long:"threads" default:"5" description:"Maximum simultaneous uploads to process"`
Meta map[string]string `long:"meta" description:"Assign metadata to uploaded files"`
}
func init() {
parser.AddCommand("put", "Store a file",
"Uploads one or more files. Specify the bucket with -b, and the filenames to upload as extra arguments.",
&Put{})
}
// Execute the put command
func (o *Put) Execute(args []string) error {
client, err := Client()
if err != nil {
return err
}
bucket, err := client.Bucket(opts.Bucket)
if err != nil {
return err
}
if bucket == nil {
return errors.New("Bucket not found: " + opts.Bucket)
}
uiprogress.Start()
tasks := make(chan string, o.Threads)
group := sync.WaitGroup{}
// Create workers
for i := 0; i < o.Threads; i++ {
group.Add(1)
go func() {
for file := range tasks {
_, err := upload(bucket, file, o.Meta)
if err != nil {
fmt.Println(err)
}
// TODO handle termination on error
}
group.Done()
}()
}
for _, file := range args {
tasks <- file
}
close(tasks)
group.Wait()
return nil
}
type progressReader struct {
bar *uiprogress.Bar
r io.ReadSeeker
}
func (p *progressReader) Read(b []byte) (int, error) {
read, err := p.r.Read(b)
p.bar.Set(p.bar.Current() + read)
return read, err
}
func (p *progressReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
p.bar.Set(int(offset))
case 1:
p.bar.Set(p.bar.Current() + int(offset))
case 2:
p.bar.Set(p.bar.Total - int(offset))
}
return p.r.Seek(offset, whence)
}
func upload(bucket *backblaze.Bucket, file string, meta map[string]string) (*backblaze.File, error) {
stat, err := os.Stat(file)
if err != nil {
return nil, err
}
reader, err := os.Open(file)
if err != nil {
return nil, err
}
defer reader.Close()
var r io.Reader = reader
if opts.Verbose {
bar := uiprogress.AddBar(int(stat.Size()))
// TODO Stop bar refresh when complete
if stat.Size() > 1024*100 {
start := time.Now()
elapsed := time.Duration(1)
count := 0
bar.AppendFunc(func(b *uiprogress.Bar) string {
count++
if count < 2 {
return ""
}
// elapsed := b.TimeElapsed()
if b.Current() < b.Total {
elapsed = time.Now().Sub(start)
}
speed := uint64(float64(b.Current()) / elapsed.Seconds())
return humanize.IBytes(speed) + "/sec"
})
}
bar.AppendCompleted()
bar.PrependFunc(func(b *uiprogress.Bar) string { return fmt.Sprintf("%10s", humanize.IBytes(uint64(b.Total))) })
bar.PrependFunc(func(b *uiprogress.Bar) string { return strutil.Resize(file, 50) })
bar.Width = 20
r = &progressReader{bar, reader}
}
return bucket.UploadFile(filepath.Base(file), meta, r)
}