Permalink
Browse files

Add file upload support to API.

  • Loading branch information...
1 parent ea87bde commit 18e116d80d7850e5f4c72fed9c8275eb5a9e5d2d @gmwils committed Nov 30, 2011
Showing with 29 additions and 2 deletions.
  1. +2 −0 README.md
  2. +1 −1 TODO.md
  3. +25 −1 lib/client.js
  4. +1 −0 package.json
View
@@ -17,6 +17,8 @@ var transloadit = require('node-transloadit');
var client = new transloadit('AUTH_KEY', 'AUTH_SECRET');
+client.addStream(name, file_name, mime_type, size, stream);
+client.addFile(file_name, file_path);
client.send(params, ok_callback, fail_callback);
```
View
@@ -1 +1 @@
- * Add support for file uploads from the client
+
View
@@ -2,6 +2,8 @@ var http = require('http');
var multi = require('multiparter');
var crypto = require('crypto');
var datejs = require('datejs');
+var _ = require('underscore');
+var fs = require('fs');
var API_HOST = 'api2.transloadit.com';
var API_PATH = '/assemblies';
@@ -18,6 +20,7 @@ var API_PATH = '/assemblies';
var Client = module.exports = function(authKey, authSecret) {
this.authKey = authKey;
this.authSecret = authSecret;
+ this.streams = {};
this.apiOptions = {
host: API_HOST,
port: 80,
@@ -31,7 +34,24 @@ var methods = function() {
/**
* Public methods
*/
-
+ this.addStream = function(name, file, type, size, stream) {
+ stream.pause();
+ this.streams[name] = {
+ length: size,
+ stream: stream,
+ type: type,
+ file: file
+ };
+ };
+
+ this.addFile = function(file_name, file_path, mime_type) {
+ var stat = fs.statSync(file_path);
+ var stream = fs.createReadStream(file_path);
+ var _mime_type = mime_type || 'application/octet-stream';
+
+ this.addStream(file_name, file_name, _mime_type, stat.size, stream);
+ };
+
this.send = function(params, success_cb, failure_cb) {
var expires_date = new Date().add({ days: 1 });
@@ -47,6 +67,10 @@ var methods = function() {
req.setParam("params", json_params);
req.setParam("signature", signature);
+
+ _.each(this.streams, function(value, key) {
+ req.addStream(key, value.file, value.type, value.length, value.stream);
+ });
req.send(function(err, res) {
var body = '';
View
@@ -8,6 +8,7 @@
"dependencies": {
"multiparter": "0.1.4"
, "datejs": ""
+ , "underscore": "1.2.1"
},
"devDependencies": {
"mocha": ""

0 comments on commit 18e116d

Please sign in to comment.