nodejs-box
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -49,4 +49,16 @@ var VERSION = '0.0.1', | ||
// Uploads a file to a given folder | ||
Files.prototype.upload = function(filename, folder, callback){ | ||
// Utility method used to overload a method based on the number of arguments it has | ||
// http://ejohn.org/blog/javascript-method-overloading/ | ||
function addMethod(object, name, fn){ | ||
var old = object[ name ]; | ||
object[ name ] = function(){ | ||
if ( fn.length == arguments.length ) | ||
return fn.apply( this, arguments ); | ||
else if ( typeof old == 'function' ) | ||
return old.apply( this, arguments ); | ||
}; | ||
} | ||
// Uploads a file to a given folder... no custom filename | ||
addMethod(Files.prototype, 'upload', function(filepath, folder, callback) { | ||
request | ||
@@ -56,3 +68,3 @@ .post(this.options.upload_url+'/'+this.resource+'/content') | ||
.field('parent_id', folder) | ||
.attach('filename', filename) | ||
.attach('filename', filepath) | ||
.end(function(res){ | ||
@@ -64,4 +76,26 @@ if(res.error) | ||
}); | ||
}; | ||
}); | ||
// Uploads a file to a given folder a with custom filename | ||
addMethod(Files.prototype, 'upload', function(filepath, filename, folder, callback) { | ||
request | ||
.post(this.options.upload_url+'/'+this.resource+'/content') | ||
.set('Authorization', this.options.auth) | ||
.field('attributes', JSON.stringify({ | ||
parent: { | ||
id: folder | ||
}, | ||
name: filename | ||
})) | ||
.attach('file', filepath) | ||
.end(function(res){ | ||
if(res.error) { | ||
return callback('Error: ' + res.error.message); | ||
} | ||
console.log(res); | ||
callback(null, res.body); | ||
}); | ||
}); | ||
// Retrieves a download link for the given file | ||
@@ -68,0 +102,0 @@ Files.prototype.download = function(file, callback){ |
{ | ||
"name": "nodejs-box", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Box API client for Node.JS", | ||
@@ -5,0 +5,0 @@ "repository": { |
67086
235