Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Added support for uploading files.
Browse files Browse the repository at this point in the history
  • Loading branch information
sethgho committed Mar 11, 2012
1 parent 3762082 commit 2882ad7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/Parse.js
Expand Up @@ -17,6 +17,11 @@ Parse.prototype = {
parseRequest.call(this, 'POST', '/1/classes/' + className, object, callback); 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 // get objects from class store
find: function (className, query, callback) { find: function (className, query, callback) {
if (typeof query === 'string') { if (typeof query === 'string') {
Expand All @@ -38,12 +43,13 @@ Parse.prototype = {
}; };


// Parse.com https api request // 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 auth = 'Basic ' + new Buffer(this._application_id + ':' + this._master_key).toString('base64');
var headers = { var headers = {
Authorization: auth, Authorization: auth,
Connection: 'Keep-alive' Connection: 'Keep-alive'
}; };

var body = null; var body = null;


switch (method) { switch (method) {
Expand All @@ -54,8 +60,14 @@ function parseRequest(method, path, data, callback) {
break; break;
case 'POST': case 'POST':
case 'PUT': case 'PUT':
body = JSON.stringify(data); if(contentType){
headers['Content-type'] = 'application/json'; 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; headers['Content-length'] = body.length;
break; break;
case 'DELETE': case 'DELETE':
Expand Down Expand Up @@ -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.end();


req.on('error', function (err) { req.on('error', function (err) {
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Expand Up @@ -25,6 +25,19 @@ examples
console.log(response); 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 ### find one


// the Foo with id = 'someId' // the Foo with id = 'someId'
Expand Down

0 comments on commit 2882ad7

Please sign in to comment.