-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransloadit_client.js
222 lines (191 loc) · 6.27 KB
/
transloadit_client.js
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
Editor.TransloaditClient = SC.Object.extend({
assemblyId: null,
params: null,
assembly: null,
service: 'http://api2.transloadit.com/',
interval: 2500,
instance: null,
uploadUrl: function() {
return this.get('instance') + '/assemblies';
}.property('instance').cacheable(),
documentTitle: null,
uploads: null,
results: null,
ended: NO,
started: false,
wait: NO,
didRaiseError: function(instance) { console.log('didRaiseError', instance); },
didProgress: function(bytesReceived, bytesExpected, assembly) { console.log('didProgress', bytesReceived, bytesExpected, assembly); },
didCompleteUpload: function(upload, assembly) { console.log('didCompleteUpload', upload, assembly); },
didReceiveResult: function(step, result, assembly) { console.log('didReceiveResult', step, result, assembly); },
didCancel: function(assembly) { console.log('didCancel', assembly); },
didSucceed: function(assembly) { console.log('didSucceed', assembly); },
_timer: null,
_pollStarted: null,
_pollRetries: 0,
_seq: 0,
_lastPoll: 0,
init: function(){
sc_super();
this.set('uploads', []);
this.set('results', SC.Object.create({}));
},
getBoredInstance: function() {
var self = this;
this.instance = null;
$.jsonp({
url: this.get('service') + 'instances/bored',
timeout: 6000,
callbackParameter: 'callback',
success: function(instance) {
if (instance.error) {
self.set('ended', YES);
self.didRaiseError(instance);
return;
}
self.set('instance', 'http://' + instance.api2_host);
},
error: function(xhr, status) {
self.set('ended', YES);
var err =
{ error: 'CONNECTION_ERROR'
, message: 'There was a problem connecting to the upload server'
, reason: 'JSONP request status: ' + status
};
self.didRaiseError(err);
}
});
},
startPolling: function() {
var self = this;
if (SC.empty(this.get('assemblyId'))) {
var err =
{ error: 'CONFIGURATION_ERROR'
, message: 'You must set the assemblyId before starting to poll.'
, reason: ''
};
this.didRaiseError(err);
}
setTimeout(function() {
self._poll();
}, 300);
},
_poll: function(query) {
var self = this;
if (this.ended) {
return;
}
// Reduce Firefox Title Flickering
if ($.browser['mozilla'] && !this.documentTitle) {
this.documentTitle = document.title;
document.title = 'Loading...';
}
this._pollStarted = +new Date();
$.jsonp({
url: this.get('instance') + '/assemblies/' + this.get('assemblyId') + (query || '?seq=' + this._seq),
timeout: 6000,
callbackParameter: 'callback',
success: function(assembly) {
if (self.get('ended')) {
return;
}
self.set('assembly', assembly);
if (assembly.error == 'ASSEMBLY_NOT_FOUND') {
self._pollRetries++;
if (self._pollRetries > 15) {
document.title = self.documentTitle;
self.set('ended', YES);
self.didRaiseError(assembly);
return;
}
setTimeout(function() {
self._poll();
}, 400);
return;
} else if (assembly.error) {
self.set('ended', YES);
document.title = self.documentTitle;
self.didRaiseError(assembly);
return;
}
self._seq = assembly.last_seq;
if (!self.get('started')) {
self.set('started', YES);
}
self._pollRetries = 0;
var isUploading = (assembly.ok == 'ASSEMBLY_UPLOADING')
, isExecuting = (assembly.ok == 'ASSEMBLY_EXECUTING')
, isCanceled = (assembly.ok == 'ASSEMBLY_CANCELED')
, isComplete = (assembly.ok == 'ASSEMBLY_COMPLETED');
self.didProgress(assembly.bytes_received, assembly.bytes_expected, assembly);
for (var i = 0; i < assembly.uploads.length; i++) {
self.didCompleteUpload(assembly.uploads[i], assembly);
self.get('uploads').pushObject(assembly.uploads[i]);
}
for (var step in assembly.results) {
self.get('results').set(step, self.results[step] || []);
for (var j = 0; j < assembly.results[step].length; j++) {
self.didReceiveResult(step, assembly.results[step][j], assembly);
self.get('results').get(step).pushObject(assembly.results[step][j]);
}
}
if (isCanceled) {
self.set('ended', YES);
document.title = self.documentTitle;
self.didCancel(assembly);
return;
}
if (isComplete || (!self.get('wait') && isExecuting)) {
self.set('ended', YES);
document.title = self.documentTitle;
assembly.uploads = self.get('uploads');
assembly.results = self.get('results');
self.didSucceed(assembly);
return;
}
var ping = (self._pollStarted - +new Date)
, timeout = (ping < self.get('interval'))
? self.get('interval')
: ping;
self._timer = setTimeout(function() {
self._poll();
}, timeout);
self._lastPoll = +new Date;
},
error: function(xhr, status) {
if (self.get('ended')) {
return;
}
self._pollRetries++;
if (self._pollRetries > 3) {
document.title = self.documentTitle;
self.set('ended', YES);
var err =
{ error: 'CONNECTION_ERROR'
, message: 'There was a problem connecting to the upload server'
, reason: 'JSONP request status: '+status
};
self.didRaiseError(err);
return;
}
setTimeout(function() {
self._poll();
}, 350);
}
});
},
stop: function() {
document.title = this.documentTitle;
this.set('ended', YES);
},
cancel: function() {
// @todo this has still a race condition if a new upload is started
// while a the cancel request is still being executed. Shouldn't happen
// in real life, but needs fixing.
if (!this.get('ended')) {
var self = this;
clearTimeout(self._timer);
this._poll('?method=delete');
}
}
});