diff --git a/lib/vimeo.js b/lib/vimeo.js index dd0073b..07669b1 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 if (typeof params === 'function') { errorCallback = progressCallback @@ -432,10 +433,14 @@ 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.') + } else { + fileSize = file.size + } } // Ignore any specified upload approach and size. @@ -462,7 +467,7 @@ Vimeo.prototype.upload = function ( } _self._performTusUpload( - filePath, + file, fileSize, attempt, completeCallback, @@ -487,7 +492,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, @@ -495,6 +500,7 @@ Vimeo.prototype.replace = function ( errorCallback ) { var _self = this + var fileSize if (typeof params === 'function') { errorCallback = progressCallback @@ -503,13 +509,19 @@ 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 = 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) + } else { + fileSize = file.size + params.file_name = file.name + } // Ignore any specified upload approach and size. if (typeof params.upload === 'undefined') { @@ -537,7 +549,7 @@ Vimeo.prototype.replace = function ( attempt.uri = videoUri _self._performTusUpload( - filePath, + file, fileSize, attempt, completeCallback, @@ -560,15 +572,21 @@ 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, { + + var fileUpload = file + + if(typeof file === 'string') { + fileUpload = fs.createReadStream(file) + } + + var upload = new tus.Upload(fileUpload, { endpoint: 'none', uploadSize: fileSize, retryDelays: [0, 1000, 3000, 5000],