2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-env browser, jquery */
|
2016-03-04 10:17:35 -05:00
|
|
|
/**
|
|
|
|
* Helper for implementing retries with backoff. Initial retry
|
|
|
|
* delay is 1 second, increasing by 2x (+jitter) for subsequent retries
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
var RetryHandler = function () {
|
|
|
|
this.interval = 1000 // Start at one second
|
|
|
|
this.maxInterval = 60 * 1000 // Don't wait longer than a minute
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke the function after waiting
|
|
|
|
*
|
|
|
|
* @param {function} fn Function to invoke
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
RetryHandler.prototype.retry = function (fn) {
|
|
|
|
setTimeout(fn, this.interval)
|
|
|
|
this.interval = this.nextInterval_()
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the counter (e.g. after successful request.)
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
RetryHandler.prototype.reset = function () {
|
|
|
|
this.interval = 1000
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the next wait time.
|
|
|
|
* @return {number} Next wait interval, in milliseconds
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
RetryHandler.prototype.nextInterval_ = function () {
|
|
|
|
var interval = this.interval * 2 + this.getRandomInt_(0, 1000)
|
|
|
|
return Math.min(interval, this.maxInterval)
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
RetryHandler.prototype.getRandomInt_ = function (min, max) {
|
|
|
|
return Math.floor(Math.random() * (max - min + 1) + min)
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 MediaUploader({
|
|
|
|
* 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
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
var MediaUploader = function (options) {
|
|
|
|
var noop = function () {}
|
|
|
|
this.file = options.file
|
|
|
|
this.contentType = options.contentType || this.file.type || 'application/octet-stream'
|
2016-03-04 10:17:35 -05:00
|
|
|
this.metadata = options.metadata || {
|
|
|
|
'title': this.file.name,
|
|
|
|
'mimeType': this.contentType
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
this.token = options.token
|
|
|
|
this.onComplete = options.onComplete || noop
|
|
|
|
this.onProgress = options.onProgress || noop
|
|
|
|
this.onError = options.onError || noop
|
|
|
|
this.offset = options.offset || 0
|
|
|
|
this.chunkSize = options.chunkSize || 0
|
|
|
|
this.retryHandler = new RetryHandler()
|
2016-03-04 10:17:35 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
this.url = options.url
|
2016-03-04 10:17:35 -05:00
|
|
|
if (!this.url) {
|
2017-03-08 13:41:05 -05:00
|
|
|
var params = options.params || {}
|
|
|
|
params.uploadType = 'resumable'
|
|
|
|
this.url = this.buildUrl_(options.fileId, params, options.baseUrl)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
this.httpMethod = options.fileId ? 'PUT' : 'POST'
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate the upload.
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.upload = function () {
|
|
|
|
var xhr = new XMLHttpRequest()
|
2016-03-04 10:17:35 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.open(this.httpMethod, this.url, true)
|
|
|
|
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
xhr.setRequestHeader('X-Upload-Content-Length', this.file.size)
|
|
|
|
xhr.setRequestHeader('X-Upload-Content-Type', this.contentType)
|
2016-03-04 10:17:35 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.onload = function (e) {
|
2016-03-04 10:17:35 -05:00
|
|
|
if (e.target.status < 400) {
|
2017-03-08 13:41:05 -05:00
|
|
|
var location = e.target.getResponseHeader('Location')
|
|
|
|
this.url = location
|
|
|
|
this.sendFile_()
|
2016-03-04 10:17:35 -05:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
this.onUploadError_(e)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}.bind(this)
|
|
|
|
xhr.onerror = this.onUploadError_.bind(this)
|
|
|
|
xhr.send(JSON.stringify(this.metadata))
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the actual file content.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.sendFile_ = function () {
|
|
|
|
var content = this.file
|
|
|
|
var end = this.file.size
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
if (this.offset || this.chunkSize) {
|
|
|
|
// Only bother to slice the file if we're either resuming or uploading in chunks
|
|
|
|
if (this.chunkSize) {
|
2017-03-08 13:41:05 -05:00
|
|
|
end = Math.min(this.offset + this.chunkSize, this.file.size)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
content = content.slice(this.offset, end)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
var xhr = new XMLHttpRequest()
|
|
|
|
xhr.open('PUT', this.url, true)
|
|
|
|
xhr.setRequestHeader('Content-Type', this.contentType)
|
|
|
|
xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.file.size)
|
|
|
|
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type)
|
2016-03-04 10:17:35 -05:00
|
|
|
if (xhr.upload) {
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.upload.addEventListener('progress', this.onProgress)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.onload = this.onContentUploadSuccess_.bind(this)
|
|
|
|
xhr.onerror = this.onContentUploadError_.bind(this)
|
|
|
|
xhr.send(content)
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query for the state of the file for resumption.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.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)
|
2016-03-04 10:17:35 -05:00
|
|
|
if (xhr.upload) {
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.upload.addEventListener('progress', this.onProgress)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
xhr.onload = this.onContentUploadSuccess_.bind(this)
|
|
|
|
xhr.onerror = this.onContentUploadError_.bind(this)
|
|
|
|
xhr.send()
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the last saved range if available in the request.
|
|
|
|
*
|
|
|
|
* @param {XMLHttpRequest} xhr Request object
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.extractRange_ = function (xhr) {
|
|
|
|
var range = xhr.getResponseHeader('Range')
|
2016-03-04 10:17:35 -05:00
|
|
|
if (range) {
|
2017-03-08 13:41:05 -05:00
|
|
|
this.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle successful responses for uploads. Depending on the context,
|
|
|
|
* may continue with uploading the next chunk of the file or, if complete,
|
|
|
|
* invokes the caller's callback.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {object} e XHR event
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.onContentUploadSuccess_ = function (e) {
|
|
|
|
if (e.target.status === 200 || e.target.status === 201) {
|
|
|
|
this.onComplete(e.target.response)
|
|
|
|
} else if (e.target.status === 308) {
|
|
|
|
this.extractRange_(e.target)
|
|
|
|
this.retryHandler.reset()
|
|
|
|
this.sendFile_()
|
2016-03-04 10:17:35 -05:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
this.onContentUploadError_(e)
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles errors for uploads. Either retries or aborts depending
|
|
|
|
* on the error.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {object} e XHR event
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.onContentUploadError_ = function (e) {
|
2016-03-04 10:17:35 -05:00
|
|
|
if (e.target.status && e.target.status < 500) {
|
2017-03-08 13:41:05 -05:00
|
|
|
this.onError(e.target.response)
|
2016-03-04 10:17:35 -05:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
this.retryHandler.retry(this.resume_.bind(this))
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles errors for the initial request.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {object} e XHR event
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.onUploadError_ = function (e) {
|
|
|
|
this.onError(e.target.response) // TODO - Retries for initial upload
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a query string from a hash/object
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {object} [params] Key/value pairs for query string
|
|
|
|
* @return {string} query string
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.buildQuery_ = function (params) {
|
|
|
|
params = params || {}
|
|
|
|
return Object.keys(params).map(function (key) {
|
|
|
|
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
|
|
|
|
}).join('&')
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the drive upload URL
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} [id] File ID if replacing
|
|
|
|
* @param {object} [params] Query parameters
|
|
|
|
* @return {string} URL
|
|
|
|
*/
|
2017-03-08 13:41:05 -05:00
|
|
|
MediaUploader.prototype.buildUrl_ = function (id, params, baseUrl) {
|
|
|
|
var url = baseUrl || 'https://www.googleapis.com/upload/drive/v2/files/'
|
2016-03-04 10:17:35 -05:00
|
|
|
if (id) {
|
2017-03-08 13:41:05 -05:00
|
|
|
url += id
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
var query = this.buildQuery_(params)
|
2016-03-04 10:17:35 -05:00
|
|
|
if (query) {
|
2017-03-08 13:41:05 -05:00
|
|
|
url += '?' + query
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
return url
|
|
|
|
}
|
2016-03-04 10:17:35 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
window.MediaUploader = MediaUploader
|