-
Notifications
You must be signed in to change notification settings - Fork 63
/
files_send_file_to_s3.go
197 lines (153 loc) · 5.12 KB
/
files_send_file_to_s3.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
package pubnub
import (
"bytes"
"encoding/json"
"github.com/pubnub/go/v7/crypto"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"github.com/pubnub/go/v7/pnerr"
)
var emptySendFileToS3Response *PNSendFileToS3Response
type sendFileToS3Builder struct {
opts *sendFileToS3Opts
}
func newSendFileToS3Builder(pubnub *PubNub) *sendFileToS3Builder {
return newSendFileToS3BuilderWithContext(pubnub, pubnub.ctx)
}
func newSendFileToS3Opts(pubnub *PubNub, ctx Context) *sendFileToS3Opts {
return &sendFileToS3Opts{endpointOpts: endpointOpts{pubnub: pubnub, ctx: ctx}}
}
func newSendFileToS3BuilderWithContext(pubnub *PubNub,
context Context) *sendFileToS3Builder {
builder := sendFileToS3Builder{
opts: newSendFileToS3Opts(pubnub, context)}
return &builder
}
func (b *sendFileToS3Builder) CipherKey(cipherKey string) *sendFileToS3Builder {
b.opts.CipherKey = cipherKey
return b
}
func (b *sendFileToS3Builder) FileUploadRequestData(fileUploadRequestData PNFileUploadRequest) *sendFileToS3Builder {
b.opts.FileUploadRequestData = fileUploadRequestData
return b
}
func (b *sendFileToS3Builder) File(f *os.File) *sendFileToS3Builder {
b.opts.File = f
return b
}
// QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
func (b *sendFileToS3Builder) QueryParam(queryParam map[string]string) *sendFileToS3Builder {
b.opts.QueryParam = queryParam
return b
}
// Transport sets the Transport for the sendFileToS3 request.
func (b *sendFileToS3Builder) Transport(tr http.RoundTripper) *sendFileToS3Builder {
b.opts.Transport = tr
return b
}
// Execute runs the sendFileToS3 request.
func (b *sendFileToS3Builder) Execute() (*PNSendFileToS3Response, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptySendFileToS3Response, status, err
}
return newPNSendFileToS3Response(rawJSON, b.opts, status)
}
type sendFileToS3Opts struct {
endpointOpts
File *os.File
FileUploadRequestData PNFileUploadRequest
QueryParam map[string]string
CipherKey string
Transport http.RoundTripper
}
func (o *sendFileToS3Opts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
return nil
}
func (o *sendFileToS3Opts) buildPath() (string, error) {
return o.FileUploadRequestData.URL, nil
}
func (o *sendFileToS3Opts) buildQuery() (*url.Values, error) {
return &url.Values{}, nil
}
func (o *sendFileToS3Opts) buildBodyMultipartFileUpload() (bytes.Buffer, *multipart.Writer, int64, error) {
fileInfo, _ := o.File.Stat()
s := fileInfo.Size()
buffer := make([]byte, 512)
_, err := o.File.Read(buffer)
if err != nil {
return bytes.Buffer{}, nil, s, err
}
o.File.Seek(0, 0)
contentType := http.DetectContentType(buffer)
var fileBody bytes.Buffer
writer := multipart.NewWriter(&fileBody)
for _, v := range o.FileUploadRequestData.FormFields {
o.pubnub.Config.Log.Printf("FormFields: Key: %s Value: %s\n", v.Key, v.Value)
if v.Key == "Content-Type" {
v.Value = contentType
}
_ = writer.WriteField(v.Key, v.Value)
}
filePart, errFilePart := writer.CreateFormFile("file", fileInfo.Name())
if errFilePart != nil {
o.pubnub.Config.Log.Printf("ERROR: writer CreateFormFile: %s\n", errFilePart.Error())
return bytes.Buffer{}, writer, s, errFilePart
}
if o.CipherKey == "" && o.pubnub.getCryptoModule() == nil {
_, errIOCopy := io.Copy(filePart, o.File)
if errIOCopy != nil {
o.pubnub.Config.Log.Printf("ERROR: io Copy error: %s\n", errIOCopy.Error())
return bytes.Buffer{}, writer, s, errIOCopy
}
} else {
var e error
cryptoModule := o.pubnub.getCryptoModule()
if o.CipherKey != "" {
cryptoModule, e = crypto.NewLegacyCryptoModule(o.CipherKey, true)
if e != nil {
o.pubnub.Config.Log.Printf("ERROR: %s\n", e.Error())
return bytes.Buffer{}, writer, s, e
}
}
e = encryptStreamAndCopyTo(cryptoModule, o.File, filePart)
if e != nil {
o.pubnub.Config.Log.Printf("ERROR: %s\n", e.Error())
return bytes.Buffer{}, writer, s, e
}
}
errWriterClose := writer.Close()
if errWriterClose != nil {
o.pubnub.Config.Log.Printf("ERROR: Writer close: %s\n", errWriterClose.Error())
return bytes.Buffer{}, writer, s, errWriterClose
}
return fileBody, writer, s, nil
}
func (o *sendFileToS3Opts) httpMethod() string {
return "POSTFORM"
}
func (o *sendFileToS3Opts) operationType() OperationType {
return PNSendFileToS3Operation
}
// PNSendFileToS3Response is the File Upload API Response for Get Spaces
type PNSendFileToS3Response struct {
}
func newPNSendFileToS3Response(jsonBytes []byte, o *sendFileToS3Opts,
status StatusResponse) (*PNSendFileToS3Response, StatusResponse, error) {
resp := &PNSendFileToS3Response{}
err := json.Unmarshal(jsonBytes, &resp)
if err != nil {
e := pnerr.NewResponseParsingError("Error unmarshalling response",
ioutil.NopCloser(bytes.NewBufferString(string(jsonBytes))), err)
return emptySendFileToS3Response, status, e
}
o.pubnub.Config.Log.Printf("newPNSendFileToS3Response status.StatusCode==> %d", status.StatusCode)
return resp, status, nil
}