From 504847e437e728e67fa7155b788baeb57ac41b40 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Sun, 21 Oct 2018 15:18:30 +0100 Subject: [PATCH 1/8] chore: add upload of a blob file --- lib/vimeo.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/lib/vimeo.js b/lib/vimeo.js index dd0073b..e87fabb 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -472,6 +472,74 @@ Vimeo.prototype.upload = function ( }) } +/** + * Upload a file with a File Object. + * + * This should be used to upload file object. If you want a form for your site to upload direct to + * Vimeo, you should look at the `POST /me/videos` endpoint. + * + * https://developer.vimeo.com/api/endpoints/videos#POST/users/{user_id}/videos + * + * @param {File} file File you wish to upload. + * @param {object=} params Parameters to send when creating a new video (name, + * privacy restrictions, etc.). See the API documentation for + * supported parameters. + * @param {Function} completeCallback Callback to be executed when the upload completes. + * @param {Function} progressCallback Callback to be executed when upload progress is updated. + * @param {Function} errorCallback Callback to be executed when the upload returns an error. + */ +Vimeo.prototype.uploadFile = function( + file, + params, + completeCallback, + progressCallback, + errorCallback +) { + var _self = this + + if (typeof params === 'function') { + errorCallback = progressCallback + progressCallback = completeCallback + completeCallback = params + params = {} + } + + var fileSize = file.size + + // Ignore any specified upload approach and size. + if (typeof params.upload === 'undefined') { + params.upload = { + approach: 'tus', + size: fileSize + } + } else { + params.upload.approach = 'tus' + params.upload.size = fileSize + } + + var options = { + path: '/me/videos?fields=uri,name,upload', + method: 'POST', + query: params + } + + // Use JSON filtering so we only receive the data that we need to make an upload happen. + this.request(options, function(err, attempt, status) { + if (err) { + return errorCallback('Unable to initiate an upload. [' + err + ']') + } + + _self._performTusFileUpload( + file, + fileSize, + attempt, + completeCallback, + progressCallback, + errorCallback + ) + }) +} + /** * Replace the source of a single Vimeo video. * @@ -582,3 +650,38 @@ Vimeo.prototype._performTusUpload = function ( upload.url = attempt.upload.upload_link upload.start() } + +/** + * Take an upload attempt and perform the actual upload via tus. + * + * https://tus.io/ + * + * @param {File} file File you wish to upload. + * @param {integer} fileSize Size of the file that will be uploaded. + * @param {Object} attempt Upload attempt data. + * @param {Function} completeCallback Callback to be executed when the upload completes. + * @param {Function} progressCallback Callback to be executed when the upload progress is updated. + * @param {Function} errorCallback Callback to be executed when the upload returns an error. + */ +Vimeo.prototype._performTusFileUpload = function( + file, + fileSize, + attempt, + completeCallback, + progressCallback, + errorCallback +) { + var upload = new tus.Upload(file, { + endpoint: 'none', + uploadSize: fileSize, + retryDelays: [0, 1000, 3000, 5000], + onError: errorCallback, + onProgress: progressCallback, + onSuccess: function() { + return completeCallback(attempt.uri) + } + }) + + upload.url = attempt.upload.upload_link + upload.start() +} From f93a3e2f56cfae1b20f6bf08a038181fcf9b32c3 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Thu, 8 Nov 2018 10:57:53 +0000 Subject: [PATCH 2/8] Refactory file upload by blob --- lib/vimeo.js | 126 +++++++-------------------------------------------- 1 file changed, 16 insertions(+), 110 deletions(-) diff --git a/lib/vimeo.js b/lib/vimeo.js index e87fabb..45ec58d 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -417,13 +417,14 @@ Vimeo.prototype.generateClientCredentials = function (scope, fn) { * @param {Function} errorCallback Callback to be executed when the upload returns an error. */ Vimeo.prototype.upload = function ( - filePath, + file, params, completeCallback, progressCallback, errorCallback ) { var _self = this + var fileSize = file.size if (typeof params === 'function') { errorCallback = progressCallback @@ -432,10 +433,12 @@ Vimeo.prototype.upload = function ( params = {} } - try { - var fileSize = fs.statSync(filePath).size - } catch (e) { - return errorCallback('Unable to locate file to upload.') + if(typeof file === 'string') { + try { + fileSize = fs.statSync(file).size + } catch (e) { + return errorCallback('Unable to locate file to upload.') + } } // Ignore any specified upload approach and size. @@ -462,74 +465,6 @@ Vimeo.prototype.upload = function ( } _self._performTusUpload( - filePath, - fileSize, - attempt, - completeCallback, - progressCallback, - errorCallback - ) - }) -} - -/** - * Upload a file with a File Object. - * - * This should be used to upload file object. If you want a form for your site to upload direct to - * Vimeo, you should look at the `POST /me/videos` endpoint. - * - * https://developer.vimeo.com/api/endpoints/videos#POST/users/{user_id}/videos - * - * @param {File} file File you wish to upload. - * @param {object=} params Parameters to send when creating a new video (name, - * privacy restrictions, etc.). See the API documentation for - * supported parameters. - * @param {Function} completeCallback Callback to be executed when the upload completes. - * @param {Function} progressCallback Callback to be executed when upload progress is updated. - * @param {Function} errorCallback Callback to be executed when the upload returns an error. - */ -Vimeo.prototype.uploadFile = function( - file, - params, - completeCallback, - progressCallback, - errorCallback -) { - var _self = this - - if (typeof params === 'function') { - errorCallback = progressCallback - progressCallback = completeCallback - completeCallback = params - params = {} - } - - var fileSize = file.size - - // Ignore any specified upload approach and size. - if (typeof params.upload === 'undefined') { - params.upload = { - approach: 'tus', - size: fileSize - } - } else { - params.upload.approach = 'tus' - params.upload.size = fileSize - } - - var options = { - path: '/me/videos?fields=uri,name,upload', - method: 'POST', - query: params - } - - // Use JSON filtering so we only receive the data that we need to make an upload happen. - this.request(options, function(err, attempt, status) { - if (err) { - return errorCallback('Unable to initiate an upload. [' + err + ']') - } - - _self._performTusFileUpload( file, fileSize, attempt, @@ -628,56 +563,27 @@ Vimeo.prototype.replace = function ( * @param {Function} errorCallback Callback to be executed when the upload returns an error. */ Vimeo.prototype._performTusUpload = function ( - filePath, + file, fileSize, attempt, completeCallback, progressCallback, errorCallback ) { - var file = fs.createReadStream(filePath) - var upload = new tus.Upload(file, { - endpoint: 'none', - uploadSize: fileSize, - retryDelays: [0, 1000, 3000, 5000], - onError: errorCallback, - onProgress: progressCallback, - onSuccess: function () { - return completeCallback(attempt.uri) - } - }) - upload.url = attempt.upload.upload_link - upload.start() -} + var fileUpload = file -/** - * Take an upload attempt and perform the actual upload via tus. - * - * https://tus.io/ - * - * @param {File} file File you wish to upload. - * @param {integer} fileSize Size of the file that will be uploaded. - * @param {Object} attempt Upload attempt data. - * @param {Function} completeCallback Callback to be executed when the upload completes. - * @param {Function} progressCallback Callback to be executed when the upload progress is updated. - * @param {Function} errorCallback Callback to be executed when the upload returns an error. - */ -Vimeo.prototype._performTusFileUpload = function( - file, - fileSize, - attempt, - completeCallback, - progressCallback, - errorCallback -) { - var upload = new tus.Upload(file, { + if(typeof file === 'string') { + fileUpload = fs.createReadStream(file) + } + + var upload = new tus.Upload(fileUpload, { endpoint: 'none', uploadSize: fileSize, retryDelays: [0, 1000, 3000, 5000], onError: errorCallback, onProgress: progressCallback, - onSuccess: function() { + onSuccess: function () { return completeCallback(attempt.uri) } }) From 2941552734f16c9048ce4dcb72f79a5e243f9f10 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Thu, 8 Nov 2018 17:11:25 +0000 Subject: [PATCH 3/8] Add behavior to replace file by path or object --- lib/vimeo.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/vimeo.js b/lib/vimeo.js index 45ec58d..76b4c2e 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -490,7 +490,7 @@ Vimeo.prototype.upload = function ( * @param {Function} errorCallback Callback to be executed when the upload returns an error. */ Vimeo.prototype.replace = function ( - filePath, + file, videoUri, params, completeCallback, @@ -498,6 +498,7 @@ Vimeo.prototype.replace = function ( errorCallback ) { var _self = this + var fileSize = file.size if (typeof params === 'function') { errorCallback = progressCallback @@ -506,13 +507,17 @@ Vimeo.prototype.replace = function ( params = {} } - try { - var fileSize = fs.statSync(filePath).size - } catch (e) { - return errorCallback('Unable to locate file to upload.') - } + params.file_name = file.name - params.file_name = path.basename(filePath) + if(typeof file === 'string') { + try { + fileSize = fs.statSync(file).size + } catch (e) { + return errorCallback('Unable to locate file to upload.') + } + + params.file_name = path.basename(file) + } // Ignore any specified upload approach and size. if (typeof params.upload === 'undefined') { @@ -540,7 +545,7 @@ Vimeo.prototype.replace = function ( attempt.uri = videoUri _self._performTusUpload( - filePath, + file, fileSize, attempt, completeCallback, From 558c39b03bfcfe8cf4d30edff319059f655a8048 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 9 Nov 2018 14:08:18 +0000 Subject: [PATCH 4/8] Good, tks sr. Co-Authored-By: nicolastakashi --- lib/vimeo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vimeo.js b/lib/vimeo.js index 76b4c2e..90eb186 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -424,7 +424,7 @@ Vimeo.prototype.upload = function ( errorCallback ) { var _self = this - var fileSize = file.size + var fileSize if (typeof params === 'function') { errorCallback = progressCallback From 9bf9bf0c1851cea81a4c3ab021ca7330376cbfa7 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 9 Nov 2018 14:09:02 +0000 Subject: [PATCH 5/8] Avoid none object access property Co-Authored-By: nicolastakashi --- lib/vimeo.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/vimeo.js b/lib/vimeo.js index 90eb186..32e45e8 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -438,6 +438,8 @@ Vimeo.prototype.upload = function ( fileSize = fs.statSync(file).size } catch (e) { return errorCallback('Unable to locate file to upload.') + } else { + fileSize = file.size } } From 0fc1cbecc1454c25c2f44d41fd9b7dacca2b3b11 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 9 Nov 2018 14:09:08 +0000 Subject: [PATCH 6/8] Avoid none object access property Co-Authored-By: nicolastakashi --- lib/vimeo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vimeo.js b/lib/vimeo.js index 32e45e8..6a75c05 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -500,7 +500,7 @@ Vimeo.prototype.replace = function ( errorCallback ) { var _self = this - var fileSize = file.size + var fileSize if (typeof params === 'function') { errorCallback = progressCallback From d52992a4d97b8df68c20df376f44bec8bc0313b8 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 9 Nov 2018 14:09:17 +0000 Subject: [PATCH 7/8] Avoid none object access property Co-Authored-By: nicolastakashi --- lib/vimeo.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/vimeo.js b/lib/vimeo.js index 6a75c05..14deb15 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -509,7 +509,6 @@ Vimeo.prototype.replace = function ( params = {} } - params.file_name = file.name if(typeof file === 'string') { try { From 5ac566202963ebd9c752f93cd45dd5ba6a650b3b Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 9 Nov 2018 14:09:24 +0000 Subject: [PATCH 8/8] Avoid none object access property Co-Authored-By: nicolastakashi --- lib/vimeo.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/vimeo.js b/lib/vimeo.js index 14deb15..07669b1 100644 --- a/lib/vimeo.js +++ b/lib/vimeo.js @@ -518,6 +518,9 @@ Vimeo.prototype.replace = function ( } params.file_name = path.basename(file) + } else { + fileSize = file.size + params.file_name = file.name } // Ignore any specified upload approach and size.