Permalink
Browse files

Added support for uploading files.

  • Loading branch information...
1 parent 3762082 commit 2882ad73c1392551cf15e3de56de8cd4cc6fd579 @sethgho sethgho committed Mar 11, 2012
Showing with 34 additions and 4 deletions.
  1. +21 −4 lib/Parse.js
  2. +13 −0 readme.md
View
@@ -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) {
View
@@ -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'

0 comments on commit 2882ad7

Please sign in to comment.