-
Notifications
You must be signed in to change notification settings - Fork 15
/
package.go
415 lines (334 loc) · 9.12 KB
/
package.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package sendsafelyuploader
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"github.com/docker/go-units"
"golang.org/x/sync/errgroup"
)
// DefaultChunkSize is 2.5 MB, recommended by sendsafely
var DefaultChunkSize int64 = 2.5 * units.MiB
type Package struct {
Uploader *Uploader
Info PackageInfo
URL string
Checksum string
Files []*File
ChunkSize int64
Concurrency int
Retries uint
m sync.Mutex
}
type PackageInfo struct {
PackageID string `json:"packageId"`
PackageCode string `json:"packageCode"`
ServerSecret string `json:"serverSecret"`
SSResponse
}
type packageOption func(*Package)
func WithChunkSize(s int64) packageOption {
return func(p *Package) {
p.ChunkSize = s
}
}
func WithConcurrency(c int) packageOption {
return func(p *Package) {
p.Concurrency = c
}
}
func WithRetries(r uint) packageOption {
return func(p *Package) {
p.Retries = r
}
}
func (p *Package) calculateNumberOfParts(fileSize int64) int64 {
return int64(math.Ceil(float64(fileSize) / float64(p.ChunkSize)))
}
func (u *Uploader) CreateDropzonePackage(options ...packageOption) (*Package, error) {
endpoint := "/drop-zone/v2.0/package/"
p := &Package{
Uploader: u,
ChunkSize: DefaultChunkSize,
}
for _, opt := range options {
opt(p)
}
body, err := u.sendRequest(http.MethodPut, endpoint, nil)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &p.Info); err != nil {
return nil, err
}
p.createChecksum()
return p, nil
}
/*
Given a file object,
1) Get file split parts - # of parts and chunk size
2) get upload URLs for those chunks
3) read the chunks, pipe thru encryption
4) upload the file to the URL
*/
func (p *Package) AddFileToPackage(file *os.File) (*File, error) {
endpoint := fmt.Sprintf("/drop-zone/v2.0/package/%s/file", p.Info.PackageCode)
fInfo, err := file.Stat()
if err != nil {
return nil, err
}
f := &File{
Info: &FileInfo{
Name: filepath.Base(file.Name()),
Size: fInfo.Size(),
},
Ptr: file,
PartURLs: make([]string, p.calculateNumberOfParts(fInfo.Size())),
}
f.UploadInfo = &FileUpload{
Name: f.Info.Name,
UploadType: "DROP_ZONE",
Part: 1,
Parts: len(f.PartURLs),
Size: f.Info.Size,
}
fileUploadJSON, err := json.Marshal(f.UploadInfo)
if err != nil {
return nil, err
}
body, err := p.Uploader.sendRequest(http.MethodPut, endpoint, fileUploadJSON, withReqJSONHeader())
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &f.Info)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal server response: '%s' ; got unmarshal error %s", string(body), err)
}
if err := p.GetUploadURLs(f); err != nil {
return nil, err
}
f.Status = make(chan int)
return f, nil
}
// Sets the upload URLs for the file parts in the provided file pointer
// only 25 part urls are returned - so if more than that are required, retry
func (p *Package) GetUploadURLs(file *File) error {
if p.Info.PackageCode == "" {
return fmt.Errorf("No package code provided for file: '%s', cannot get list of upload URLs", file.Info.Name)
}
endpoint := fmt.Sprintf("/drop-zone/v2.0/package/%s/file/%s/upload-urls", p.Info.PackageCode, file.Info.ID) //u.FileInfo.FileID)
f := func(part int) error {
file.UploadInfo.Part = part
fileUploadJSON, err := json.Marshal(file.UploadInfo)
if err != nil {
return err
}
body, err := p.Uploader.sendRequest(http.MethodPost, endpoint, fileUploadJSON, withReqJSONHeader())
if err != nil {
return err
}
uInfo := UploadUrlInfo{}
err = json.Unmarshal(body, &uInfo)
if err != nil {
return err
}
if uInfo.Response != "SUCCESS" {
fmt.Printf("uploadInfoName: %s, Part: %d\n", file.UploadInfo.Name, file.UploadInfo.Part)
return fmt.Errorf("Failed to get upload URLs: response: '%s'; message: '%s'", uInfo.Response, uInfo.Message)
}
for _, u := range uInfo.URLS {
file.PartURLs[u.Part-1] = u.URL
}
return nil
}
// calculate number of calls to get
batchSize := 25
calls := int(math.Ceil(float64(file.Info.Parts) / float64(batchSize)))
for i := 0; i < calls; i++ {
if err := f(1 + i*batchSize); err != nil {
return err
}
}
return nil
}
func (p *Package) UploadFileParts(file *File) error {
defer close(file.Status)
g := new(errgroup.Group)
g.SetLimit(p.Concurrency)
r := bufio.NewReader(file.Ptr)
bytePool := sync.Pool{New: func() interface{} {
return make([]byte, p.ChunkSize)
}}
for _, URL := range file.PartURLs {
URL := URL
buff := bytePool.Get().([]byte)
b := []byte{}
c, err := r.Read(buff)
b = append(b, buff[:c]...)
if err != nil {
if err == io.EOF {
} else {
return err
}
}
g.Go(func() error {
encryptB, err := p.Encrypt(bytes.NewReader(b))
if err != nil {
return err
}
err = p.uploadFilePart(encryptB, URL)
if err != nil {
return err
}
file.Status <- 1
return nil
})
}
err := g.Wait()
return err
}
// try up to Package.Retries
func (p *Package) uploadFilePart(part *bytes.Buffer, url string) error {
if part.Len() == 0 {
return fmt.Errorf("File part is empty")
}
if url == "" {
return fmt.Errorf("URL is empty")
}
f := func() error {
req, err := http.NewRequest(http.MethodPut, url, part)
req.Header.Set("ss-request-api", "DROP_ZONE")
if err != nil {
return err
}
resp, err := p.Uploader.Client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Unable to upload file: Bad HTTP Status Code: %d; Unable to read response body", resp.StatusCode)
}
return fmt.Errorf("Unable to upload file: Invalid HTTP status response; HTTP Error code %d; request body %s", resp.StatusCode, string(respBody))
}
defer resp.Body.Close()
return nil
}
for {
if err := f(); err != nil {
p.m.Lock()
if p.Retries > 0 {
p.Retries--
p.m.Unlock()
continue
} else {
p.m.Unlock()
return err
}
}
break
}
return nil
}
func (p *Package) MarkFileComplete(file *File) error {
endpoint := fmt.Sprintf("/drop-zone/v2.0/package/%s/file/%s/upload-complete", p.Info.PackageCode, file.Info.ID)
body, err := p.Uploader.sendRequest(http.MethodPost, endpoint, nil, withReqJSONHeader())
if err != nil {
return err
}
var resp FileInfo
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
if resp.Response != "SUCCESS" {
return fmt.Errorf("Failed to finalize file '%s' in package '%s' with message: %s", file.Info.Name, p.Info.PackageID, resp.Message)
}
return nil
}
// FinalizePackage marks the package complete and returns a link to the package,
// or the error if package finalization did not succeed. Sets Package.URL on success
func (p *Package) FinalizePackage() error {
endpoint := fmt.Sprintf("/drop-zone/v2.0/package/%s/finalize", p.Info.PackageCode)
type Rb struct {
Checksum string
}
var rb Rb
rb.Checksum = p.Checksum
rbJson, err := json.Marshal(rb)
if err != nil {
return err
}
body, err := p.Uploader.sendRequest(http.MethodPost, endpoint, rbJson, withReqJSONHeader())
if err != nil {
return err
}
var resp FinalizeInfo
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
if resp.Response != "SUCCESS" {
return fmt.Errorf("Failed to finalize package '%s' with message: %s", p.Info.PackageID, resp.Message)
}
p.URL = resp.Message + "#keyCode=" + p.Uploader.ClientSecret
return nil
}
func (p *Package) SubmitHostedDropzone(name string, email string) error {
dropzoneData := url.Values{}
dropzoneData.Set("name", name)
dropzoneData.Set("email", email)
dropzoneData.Set("packageCode", p.Info.PackageCode)
dropzoneData.Set("publicApiKey", p.Uploader.APIKey)
endpoint := "/auth/json/?action=submitHostedDropzone"
body, err := p.Uploader.sendRequest(http.MethodPost, endpoint, []byte(dropzoneData.Encode()), withReqFormHeader())
if err != nil {
return err
}
var dzInfo HostedDropzoneInfo
err = json.Unmarshal(body, &dzInfo)
if err != nil {
return err
}
if dzInfo.Success != "true" {
return fmt.Errorf("Unable to submit Hosted Dropzone with error %s", dzInfo.Message)
}
return p.finalizeHostedDropzone(dzInfo)
}
func (p *Package) finalizeHostedDropzone(dzInfo HostedDropzoneInfo) error {
finalDZData := url.Values{}
finalDZData.Set("data", dzInfo.Data)
finalDZData.Set("digest", dzInfo.Digest)
finalDZData.Set("secureLink", p.URL)
for _, url := range dzInfo.IntegrationURLs {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(finalDZData.Encode())))
if err != nil {
return err
}
if err := withReqFormHeader()(req); err != nil {
return err
}
resp, err := p.Uploader.Client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Unable to upload file: Bad HTTP Status Code: %d; Unable to read response body", resp.StatusCode)
}
return fmt.Errorf("Unable to upload file: Invalid HTTP status response; HTTP Error code %d; request body %s", resp.StatusCode, string(respBody))
}
defer resp.Body.Close()
}
return nil
}