/
assembly.go
346 lines (296 loc) · 10.8 KB
/
assembly.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
package transloadit
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"time"
)
type Assembly struct {
client *Client
// Notify url to send a request to once the assembly finishes.
// See https://transloadit.com/docs#notifications.
NotifyUrl string
// Optional template id to use instead of adding steps.
TemplateId string
// Wait until the assembly completes (or is canceled).
Blocking bool
steps map[string]map[string]interface{}
readers []*upload
}
type upload struct {
Field string
Name string
Reader io.ReadCloser
}
type AssemblyReplay struct {
assemblyId string
client *Client
// Notify url to send a request to once the assembly finishes.
// See https://transloadit.com/docs#notifications.
NotifyUrl string
// Wait until the assembly completes (or is canceled).
Blocking bool
reparseTemplate bool
steps map[string]map[string]interface{}
}
type AssemblyList struct {
Assemblies []*AssemblyListItem `json:"items"`
Count int `json:"count"`
}
type AssemblyListItem struct {
AssemblyId string `json:"id"`
AccountId string `json:"account_id"`
TemplateId string `json:"template_id"`
Instance string `json:"instance"`
NotifyUrl string `json:"notify_url"`
RedirectUrl string `json:"redirect_url"`
ExecutionDuration float32 `json:"execution_duration"`
ExecutionStart time.Time `json:"execution_start"`
Created time.Time `json:"created"`
Ok string `json:"ok"`
Error string `json:"error"`
Files string `json:"files"`
}
type AssemblyInfo struct {
AssemblyId string `json:"assembly_id"`
ParentId string `json:"parent_id"`
AssemblyUrl string `json:"assembly_url"`
AssemblySslUrl string `json:"assembly_ssl_url"`
BytesReceived int `json:"bytes_received"`
BytesExpected int `json:"bytes_expected"`
ClientAgent string `json:"client_agent"`
ClientIp string `json:"client_ip"`
ClientReferer string `json:"client_referer"`
StartDate string `json:"start_date"`
IsInfinite bool `json:"is_infinite"`
HasDupeJobs bool `json:"has_dupe_jobs"`
UploadDuration float32 `json:"upload_duration"`
NotifyUrl string `json:"notify_url"`
NotifyStart string `json:"notify_start"`
NotifyStatus string `json:"notify_status"`
NotifyDuation float32 `json:"notify_duration"`
LastJobCompleted string `json:"last_job_completed"`
ExecutionDuration float32 `json:"execution_duration"`
ExecutionStart string `json:"execution_start"`
Created string `json:"created"`
Ok string `json:"ok"`
Message string `json:"message"`
Files string `json:"files"`
Fields map[string]interface{} `json:"fields"`
BytesUsage int `json:"bytes_usage"`
FilesToStoreOnS3 int `json:"files_to_store_on_s3"`
QueuedFilesToStoreOnS3 int `json:"queued_files_to_store_on_s3"`
ExecutingJobs []interface{} `json:"executing_jobs"`
StartedJobs []interface{} `json:"started_jobs"`
ParentAssemblyStatus *AssemblyInfo `json:"parent_assembly_status"`
Uploads []*FileInfo `json:"uploads"`
Results map[string][]*FileInfo `json:"results"`
Params string `json:"params"`
Error string `json:"error"`
}
type FileInfo struct {
Id string `json:"id"`
Name string `json:"name"`
Basename string `json:"basename"`
Ext string `json:"ext"`
Size int `json:"size"`
Mime string `json:"mime"`
Type string `json:"type"`
Field string `json:"field"`
Md5Hash string `json:"md5hash"`
OriginalMd5Hash string `json:"original_md5hash"`
OriginalId string `json:"original_id"`
OriginalBasename string `json:"original_basename"`
Url string `json:"url"`
SslUrl string `json:"ssl_url"`
Meta map[string]interface{} `json:"meta"`
}
// Create a new assembly instance which can be executed later.
func (client *Client) CreateAssembly() *Assembly {
return &Assembly{
client: client,
steps: make(map[string]map[string]interface{}),
readers: make([]*upload, 0),
}
}
// Add another reader to upload later.
func (assembly *Assembly) AddReader(field, name string, reader io.ReadCloser) {
assembly.readers = append(assembly.readers, &upload{
Field: field,
Name: name,
Reader: reader,
})
}
// Add another file to upload later.
func (assembly *Assembly) AddFile(field, name string) error {
file, err := os.Open(name)
if err != nil {
return err
}
assembly.AddReader(field, name, file)
return nil
}
// Add a step to the assembly.
func (assembly *Assembly) AddStep(name string, details map[string]interface{}) {
assembly.steps[name] = details
}
// Start the assembly and upload all files.
// When an error is returned you should also check AssemblyInfo.Error for more
// information about the error. This happens when there is an error returned by
// the Transloadit API:
// info, err := assembly.Upload()
// if err != nil {
// if info != nil && info.Error != "" {
// // See info.Error
// }
// panic(err)
// }
func (assembly *Assembly) Upload() (*AssemblyInfo, error) {
req, err := assembly.makeRequest()
if err != nil {
return nil, fmt.Errorf("failed to create assembly request: %s", err)
}
var info AssemblyInfo
_, err = assembly.client.doRequest(req, &info)
if info.Error != "" {
return &info, fmt.Errorf("failed to create assembly: %s", info.Error)
}
if !assembly.Blocking {
return &info, err
}
watcher := assembly.client.WaitForAssembly(info.AssemblyUrl)
select {
case res := <-watcher.Response:
// Assembly completed
return res, nil
case err := <-watcher.Error:
// Error appeared
return nil, err
}
}
func (assembly *Assembly) makeRequest() (*http.Request, error) {
// Get bored instance to upload files to
bored, err := assembly.client.getBoredInstance()
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
url := "http://api2-" + bored + "/assemblies"
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add files to upload
for _, reader := range assembly.readers {
part, err := writer.CreateFormFile(reader.Field, reader.Name)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
_, err = io.Copy(part, reader.Reader)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
err = reader.Reader.Close()
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
}
options := make(map[string]interface{})
if len(assembly.steps) != 0 {
options["steps"] = assembly.steps
}
if assembly.TemplateId != "" {
options["template_id"] = assembly.TemplateId
}
if assembly.NotifyUrl != "" {
options["notify_url"] = assembly.NotifyUrl
}
params, signature, err := assembly.client.sign(options)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
// Add additional keys and values
err = writer.WriteField("params", params)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
err = writer.WriteField("signature", signature)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
// Close multipart writer
err = writer.Close()
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
// Create HTTP request
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, fmt.Errorf("unable to create upload request: %s", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
// Get information about an assembly using its url.
func (client *Client) GetAssembly(assemblyUrl string) (*AssemblyInfo, error) {
var info AssemblyInfo
_, err := client.request("GET", assemblyUrl, nil, &info)
return &info, err
}
// Cancel an assembly using its url.
func (client *Client) CancelAssembly(assemblyUrl string) (Response, error) {
return client.request("DELETE", assemblyUrl, nil, nil)
}
// Create a new AssemblyReplay instance.
func (client *Client) ReplayAssembly(assemblyId string) *AssemblyReplay {
return &AssemblyReplay{
client: client,
steps: make(map[string]map[string]interface{}),
assemblyId: assemblyId,
}
}
// Add a step to override the original ones.
func (assembly *AssemblyReplay) AddStep(name string, details map[string]interface{}) {
assembly.steps[name] = details
}
// Reparse the template when replaying. Useful if the template has changed since the orignal assembly was created.
func (assembly *AssemblyReplay) ReparseTemplate() {
assembly.reparseTemplate = true
}
// Start the assembly replay.
func (assembly *AssemblyReplay) Start() (*AssemblyInfo, error) {
options := map[string]interface{}{
"steps": assembly.steps,
}
if assembly.reparseTemplate {
options["reparse_template"] = 1
}
if assembly.NotifyUrl != "" {
options["notify_url"] = assembly.NotifyUrl
}
var info AssemblyInfo
_, err := assembly.client.request("POST", "assemblies/"+assembly.assemblyId+"/replay", options, &info)
if info.Error != "" {
return &info, fmt.Errorf("failed to start assembly replay: %s", info.Error)
}
if !assembly.Blocking {
return &info, err
}
// Assembly replay response doesn't contains assembly url
assemblyUrl := assembly.client.config.Endpoint + "/assemblies/" + info.AssemblyId
watcher := assembly.client.WaitForAssembly(assemblyUrl)
select {
case res := <-watcher.Response:
// Assembly completed
return res, nil
case err := <-watcher.Error:
// Error appeared
return nil, err
}
}
// List all assemblies matching the criterias.
func (client *Client) ListAssemblies(options *ListOptions) (*AssemblyList, error) {
var assemblies AssemblyList
_, err := client.listRequest("assemblies", options, &assemblies)
return &assemblies, err
}