-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
61 lines (47 loc) · 1.21 KB
/
upload.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
package buildpacks
import (
"bytes"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"io"
"mime/multipart"
"path/filepath"
)
func CalculateRequestSize(buildpackSize int64, bpPath string, fieldName string) (int64, error) {
body := &bytes.Buffer{}
form := multipart.NewWriter(body)
bpFileName := filepath.Base(bpPath)
_, err := form.CreateFormFile(fieldName, bpFileName)
if err != nil {
return 0, err
}
err = form.Close()
if err != nil {
return 0, err
}
return int64(body.Len()) + buildpackSize, nil
}
func CreateMultipartBodyAndHeader(buildpack io.Reader, bpPath string, fieldName string) (string, io.ReadSeeker, <-chan error) {
writerOutput, writerInput := cloudcontroller.NewPipeBomb()
form := multipart.NewWriter(writerInput)
writeErrors := make(chan error)
go func() {
defer close(writeErrors)
defer writerInput.Close()
bpFileName := filepath.Base(bpPath)
writer, err := form.CreateFormFile(fieldName, bpFileName)
if err != nil {
writeErrors <- err
return
}
_, err = io.Copy(writer, buildpack)
if err != nil {
writeErrors <- err
return
}
err = form.Close()
if err != nil {
writeErrors <- err
}
}()
return form.FormDataContentType(), writerOutput, writeErrors
}