diff --git a/lib/Parse.js b/lib/Parse.js index 14c8b88..c6427b1 100644 --- a/lib/Parse.js +++ b/lib/Parse.js @@ -17,6 +17,11 @@ Parse.prototype = { parseRequest.call(this, 'POST', '/1/classes/' + className, object, callback); }, + // add files + insertFile: function(fileName, data, contentType, callback){ + parseRequest.call(this, 'POST', '/1/files/' + fileName, data, callback, contentType); + }, + // get objects from class store find: function (className, query, callback) { if (typeof query === 'string') { @@ -38,12 +43,13 @@ Parse.prototype = { }; // Parse.com https api request -function parseRequest(method, path, data, callback) { +function parseRequest(method, path, data, callback, contentType) { var auth = 'Basic ' + new Buffer(this._application_id + ':' + this._master_key).toString('base64'); var headers = { Authorization: auth, Connection: 'Keep-alive' }; + var body = null; switch (method) { @@ -54,8 +60,14 @@ function parseRequest(method, path, data, callback) { break; case 'POST': case 'PUT': - body = JSON.stringify(data); - headers['Content-type'] = 'application/json'; + if(contentType){ + body = data; + headers['Content-type'] = contentType; + console.log('Sending data type: ' + contentType + ' of length: ' + body.length); + }else{ + headers['Content-type'] = 'application/json'; + body = JSON.stringify(data); + } headers['Content-length'] = body.length; break; case 'DELETE': @@ -109,7 +121,12 @@ function parseRequest(method, path, data, callback) { }); }); - body && req.write(body); + if(contentType) + { + body && req.write(body,'binary'); + }else{ + body && req.write(body); + } req.end(); req.on('error', function (err) { diff --git a/readme.md b/readme.md index c7912ea..366136d 100644 --- a/readme.md +++ b/readme.md @@ -25,6 +25,19 @@ examples console.log(response); }); +### insert file + var fs = require('fs'), + fileName = 'myMedia.mp3'; + fs.readFile(fileName, function (err, data) { + if (err) throw err; + app.insertFile(fileName, data, 'audio/mpeg', function(err, response){ + if(err) throw err; + console.log('Name: ' + response.name); + console.log('Url: ' + response.url); + }); + }); + + ### find one // the Foo with id = 'someId'