forked from websemantics/vimeo-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimeo-upload.js
448 lines (401 loc) · 14.6 KB
/
vimeo-upload.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
| Vimeo-Upload: Upload videos to your Vimeo account directly from a
| browser or a Node.js app
|
| ╭───╮╭─╮
| │ ││ │╭─╮╭──┬──┬─╮╭───╮╭───╮
| │ ││ │├─┤│ ╭╮ ╭╮ ││ ─ ││╭╮ │ ╭────────┬─────────────────────╮
| ╰╮ ╰╯╭╯│ ││ ││ ││ ││ ─┤│╰╯ │ | UPLOAD │ ▒▒▒▒▒▒▒▒▒▒▒░░░░ %75 |
| ╰────╯ ╰─╯╰─╯╰─╯╰─╯╰───╯╰───╯ ╰────────┴─────────────────────╯
|
|
| This project was released under Apache 2.0" license.
|
| @link http://websemantics.ca
| @author Web Semantics, Inc. Dev Team <team@websemantics.ca>
| @author Adnan M.Sagar, PhD. <adnan@websemantics.ca>
| @credits Built on cors-upload-sample, https://github.com/googledrive/cors-upload-sample
*/
;
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function() {
return (root.VimeoUpload = factory())
})
} else if (typeof module === 'object' && module.exports) {
module.exports = factory()
} else {
root.VimeoUpload = factory()
}
}(this, function() {
// -------------------------------------------------------------------------
// RetryHandler Class
/**
* Helper for implementing retries with backoff. Initial retry
* delay is 1 second, increasing by 2x (+jitter) for subsequent retries
*
* @constructor
*/
var RetryHandler = function() {
this.interval = 1000 // Start at one second
this.maxInterval = 60 * 1000; // Don't wait longer than a minute
}
/**
* Invoke the function after waiting
*
* @param {function} fn Function to invoke
*/
RetryHandler.prototype.retry = function(fn) {
setTimeout(fn, this.interval)
this.interval = this.nextInterval_()
}
/**
* Reset the counter (e.g. after successful request)
*/
RetryHandler.prototype.reset = function() {
this.interval = 1000
}
/**
* Calculate the next wait time.
* @return {number} Next wait interval, in milliseconds
*
* @private
*/
RetryHandler.prototype.nextInterval_ = function() {
var interval = this.interval * 2 + this.getRandomInt_(0, 1000)
return Math.min(interval, this.maxInterval)
}
/**
* Get a random int in the range of min to max. Used to add jitter to wait times.
*
* @param {number} min Lower bounds
* @param {number} max Upper bounds
* @private
*/
RetryHandler.prototype.getRandomInt_ = function(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
// -------------------------------------------------------------------------
// Private data
/* Library defaults, can be changed using the 'defaults' member method,
- api_url (string), vimeo api url
- name (string), default video name
- description (string), default video description
- contentType (string), video content type
- token (string), vimeo api token
- file (object), video file
- metadata (array), data to associate with the video
- upgrade_to_1080 (boolean), set video resolution to high definition
- offset (int),
- chunkSize (int),
- retryHandler (RetryHandler), hanlder class
- onComplete (function), handler for onComplete event
- onProgress (function), handler for onProgress event
- onError (function), handler for onError event
*/
var defaults = {
api_url: 'https://api.vimeo.com',
name: 'Default name',
description: 'Default description',
contentType: 'application/octet-stream',
token: null,
file: {},
metadata: [],
upgrade_to_1080: false,
offset: 0,
chunkSize: 0,
retryHandler: new RetryHandler(),
onComplete: function() {},
onProgress: function() {},
onError: function() {}
}
/**
* Helper class for resumable uploads using XHR/CORS. Can upload any Blob-like item, whether
* files or in-memory constructs.
*
* @example
* var content = new Blob(["Hello world"], {"type": "text/plain"})
* var uploader = new VimeoUpload({
* file: content,
* token: accessToken,
* onComplete: function(data) { ... }
* onError: function(data) { ... }
* })
* uploader.upload()
*
* @constructor
* @param {object} options Hash of options
* @param {string} options.token Access token
* @param {blob} options.file Blob-like item to upload
* @param {string} [options.fileId] ID of file if replacing
* @param {object} [options.params] Additional query parameters
* @param {string} [options.contentType] Content-type, if overriding the type of the blob.
* @param {object} [options.metadata] File metadata
* @param {function} [options.onComplete] Callback for when upload is complete
* @param {function} [options.onProgress] Callback for status for the in-progress upload
* @param {function} [options.onError] Callback if upload fails
*/
var me = function(opts) {
/* copy user options or use default values */
for (var i in defaults) {
this[i] = (opts[i] !== undefined) ? opts[i] : defaults[i]
}
this.contentType = opts.contentType || this.file.type || defaults.contentType
this.httpMethod = opts.fileId ? 'PUT' : 'POST'
this.videoData = {
name: (opts.name > '') ? opts.name : defaults.name,
description: (opts.description > '') ? opts.description : defaults.description,
'privacy.view': opts.private ? 'nobody' : 'anybody'
}
if (!(this.url = opts.url)) {
var params = opts.params || {} /* TODO params.uploadType = 'resumable' */
this.url = this.buildUrl_(opts.fileId, params, opts.baseUrl)
}
}
// -------------------------------------------------------------------------
// Public methods
/*
Override class defaults
Parameters:
- opts (object): name value pairs
*/
me.prototype.defaults = function(opts) {
return defaults /* TODO $.extend(true, defaults, opts) */
}
/**
* Initiate the upload (Get vimeo ticket number and upload url)
*/
me.prototype.upload = function() {
var xhr = new XMLHttpRequest()
xhr.open(this.httpMethod, this.url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onload = function(e) {
// get vimeo upload url, user (for available quote), ticket id and complete url
if (e.target.status < 400) {
var response = JSON.parse(e.target.responseText)
this.url = response.upload_link_secure
this.user = response.user
this.ticket_id = response.ticket_id
this.complete_url = defaults.api_url + response.complete_uri
this.sendFile_()
} else {
this.onUploadError_(e)
}
}.bind(this)
xhr.onerror = this.onUploadError_.bind(this)
xhr.send(JSON.stringify({
type: 'streaming',
upgrade_to_1080: this.upgrade_to_1080
}))
}
// -------------------------------------------------------------------------
// Private methods
/**
* Send the actual file content.
*
* @private
*/
me.prototype.sendFile_ = function() {
var content = this.file
var end = this.file.size
if (this.offset || this.chunkSize) {
// Only bother to slice the file if we're either resuming or uploading in chunks
if (this.chunkSize) {
end = Math.min(this.offset + this.chunkSize, this.file.size)
}
content = content.slice(this.offset, end)
}
var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Type', this.contentType)
// xhr.setRequestHeader('Content-Length', this.file.size)
xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.file.size)
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress)
}
xhr.onload = this.onContentUploadSuccess_.bind(this)
xhr.onerror = this.onContentUploadError_.bind(this)
xhr.send(content)
}
/**
* Query for the state of the file for resumption.
*
* @private
*/
me.prototype.resume_ = function() {
var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Range', 'bytes */' + this.file.size)
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type)
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress)
}
xhr.onload = this.onContentUploadSuccess_.bind(this)
xhr.onerror = this.onContentUploadError_.bind(this)
xhr.send()
}
/**
* Extract the last saved range if available in the request.
*
* @param {XMLHttpRequest} xhr Request object
*/
me.prototype.extractRange_ = function(xhr) {
var range = xhr.getResponseHeader('Range')
if (range) {
this.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1
}
}
/**
* The final step is to call vimeo.videos.upload.complete to queue up
* the video for transcoding.
*
* If successful call 'onUpdateVideoData_'
*
* @private
*/
me.prototype.complete_ = function(xhr) {
var xhr = new XMLHttpRequest()
xhr.open('DELETE', this.complete_url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
xhr.onload = function(e) {
// Get the video location (videoId)
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location')
// Example of location: ' /videos/115365719', extract the video id only
var video_id = location.split('/').pop()
// Update the video metadata
this.onUpdateVideoData_(video_id)
} else {
this.onCompleteError_(e)
}
}.bind(this)
xhr.onerror = this.onCompleteError_.bind(this)
xhr.send()
}
/**
* Update the Video Data and add the metadata to the upload object
*
* @private
* @param {string} [id] Video Id
*/
me.prototype.onUpdateVideoData_ = function(video_id) {
var url = this.buildUrl_(video_id, [], defaults.api_url + '/videos/')
var httpMethod = 'PATCH'
var xhr = new XMLHttpRequest()
xhr.open(httpMethod, url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
xhr.onload = function(e) {
// add the metadata
this.onGetMetadata_(e, video_id)
}.bind(this)
xhr.send(this.buildQuery_(this.videoData))
}
/**
* Retrieve the metadata from a successful onUpdateVideoData_ response
* This is is useful when uploading unlisted videos as the URI has changed.
*
* If successful call 'onUpdateVideoData_'
*
* @private
* @param {object} e XHR event
* @param {string} [id] Video Id
*/
me.prototype.onGetMetadata_ = function(e, video_id) {
// Get the video location (videoId)
if (e.target.status < 400) {
if (e.target.response) {
// add the returned metadata to the metadata array
var meta = JSON.parse(e.target.response)
// get the new index of the item
var index = this.metadata.push(meta) - 1
// call the complete method
this.onComplete(video_id, index)
} else {
this.onCompleteError_(e)
}
}
}
/**
* Handle successful responses for uploads. Depending on the context,
* may continue with uploading the next chunk of the file or, if complete,
* invokes vimeo complete service.
*
* @private
* @param {object} e XHR event
*/
me.prototype.onContentUploadSuccess_ = function(e) {
if (e.target.status == 200 || e.target.status == 201) {
this.complete_()
} else if (e.target.status == 308) {
this.extractRange_(e.target)
this.retryHandler.reset()
this.sendFile_()
}
}
/**
* Handles errors for uploads. Either retries or aborts depending
* on the error.
*
* @private
* @param {object} e XHR event
*/
me.prototype.onContentUploadError_ = function(e) {
if (e.target.status && e.target.status < 500) {
this.onError(e.target.response)
} else {
this.retryHandler.retry(this.resume_())
}
}
/**
* Handles errors for the complete request.
*
* @private
* @param {object} e XHR event
*/
me.prototype.onCompleteError_ = function(e) {
this.onError(e.target.response); // TODO - Retries for initial upload
}
/**
* Handles errors for the initial request.
*
* @private
* @param {object} e XHR event
*/
me.prototype.onUploadError_ = function(e) {
this.onError(e.target.response); // TODO - Retries for initial upload
}
/**
* Construct a query string from a hash/object
*
* @private
* @param {object} [params] Key/value pairs for query string
* @return {string} query string
*/
me.prototype.buildQuery_ = function(params) {
params = params || {}
return Object.keys(params).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
}).join('&')
}
/**
* Build the drive upload URL
*
* @private
* @param {string} [id] File ID if replacing
* @param {object} [params] Query parameters
* @return {string} URL
*/
me.prototype.buildUrl_ = function(id, params, baseUrl) {
var url = baseUrl || defaults.api_url + '/me/videos'
if (id) {
url += id
}
var query = this.buildQuery_(params)
if (query) {
url += '?' + query
}
return url
}
return me
}))