Skip to content

Commit

Permalink
Merge pull request #86 from nicolastakashi/master
Browse files Browse the repository at this point in the history
chore: add upload of a blob file
  • Loading branch information
erunion committed Nov 9, 2018
2 parents 96b877b + 5ac5662 commit 1927074
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions lib/vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -462,7 +467,7 @@ Vimeo.prototype.upload = function (
}

_self._performTusUpload(
filePath,
file,
fileSize,
attempt,
completeCallback,
Expand All @@ -487,14 +492,15 @@ 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,
progressCallback,
errorCallback
) {
var _self = this
var fileSize

if (typeof params === 'function') {
errorCallback = progressCallback
Expand All @@ -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') {
Expand Down Expand Up @@ -537,7 +549,7 @@ Vimeo.prototype.replace = function (
attempt.uri = videoUri

_self._performTusUpload(
filePath,
file,
fileSize,
attempt,
completeCallback,
Expand All @@ -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],
Expand Down

0 comments on commit 1927074

Please sign in to comment.