Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for uploading files to buffer instead of filesystem #267

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions example/post.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require('../test/common');
var http = require('http'),
util = require('util'),
formidable = require('formidable'),
formidable = require('..'), // Change '..' to 'formidable' in your code
server;

var TEST_PORT = process.argv[2] || 3000;

server = http.createServer(function(req, res) {
if (req.url == '/') {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/post" method="post">'+
Expand All @@ -14,7 +16,7 @@ server = http.createServer(function(req, res) {
'<input type="submit" value="Submit">'+
'</form>'
);
} else if (req.url == '/post') {
} else if (req.url === '/post') {
var form = new formidable.IncomingForm(),
fields = [];

Expand Down
11 changes: 7 additions & 4 deletions example/upload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require('../test/common');
var http = require('http'),
util = require('util'),
formidable = require('formidable'),
formidable = require('../index'), // Change '..' to 'formidable' in your code
server;

var TEST_TMP = process.env.TMP || process.env.TMPDIR || process.env.TEMP || '/tmp' || process.cwd();
var TEST_PORT = process.argv[2] || 3000;

server = http.createServer(function(req, res) {
if (req.url == '/') {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
Expand All @@ -14,8 +17,8 @@ server = http.createServer(function(req, res) {
'<input type="submit" value="Upload">'+
'</form>'
);
} else if (req.url == '/upload') {
var form = new formidable.IncomingForm(),
} else if (req.url === '/upload') {
var form = new formidable.IncomingForm({noFileSystem: true}),
files = [],
fields = [];

Expand Down
92 changes: 68 additions & 24 deletions lib/incoming_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function IncomingForm(opts) {
this.headers = null;
this.type = null;
this.hash = opts.hash || false;
this.noFileSystem = opts.noFileSystem || false;
this.files = [];

this.bytesReceived = null;
this.bytesExpected = null;
Expand Down Expand Up @@ -186,37 +188,79 @@ IncomingForm.prototype.handlePart = function(part) {
return;
}

this._flushing++;
// Upload file(s) to self.files instead of to filesystem
if (this.noFileSystem) {
part.on('data', function(buffer) {
if (buffer.length === 0) {
return;
}
var i = 0;
var new_file = true;
for (i=0; i<self.files.length; i++) {
if (self.files[i].filename === part.filename) {
new_file = false;
self.files[i].buffers.push(buffer);
self.files[i].length += buffer.length;
break;
}
}
if (new_file) {
self.files.push({
filename: part.filename,
length: buffer.length,
buffers: [buffer],
file: null
});
}
});

var file = new File({
path: this._uploadPath(part.filename),
name: part.filename,
type: part.mime,
hash: self.hash
});
part.on('end', function() {
var i = 0, j = 0, offset = 0;
for (i=0; i<self.files.length; i++) {
offset = 0;
self.files[i].file = new Buffer(self.files[i].length);
for (j=0; j<self.files[i].buffers.length; j++) {
self.files[i].buffers[j].copy(self.files[i].file, offset);
offset += self.files[i].buffers[j].length;
}
self.files[i].buffers = [];
self.emit('file', part.name, self.files[i].file.toString(self.encoding));
}
});
}
else {
this._flushing++;

var file = new File({
path: this._uploadPath(part.filename),
name: part.filename,
type: part.mime,
hash: self.hash
});

this.emit('fileBegin', part.name, file);
this.emit('fileBegin', part.name, file);

file.open();
this.openedFiles.push(file);
file.open();
this.openedFiles.push(file);

part.on('data', function(buffer) {
if (buffer.length == 0) {
return;
}
self.pause();
file.write(buffer, function() {
self.resume();
part.on('data', function(buffer) {
if (buffer.length == 0) {
return;
}
self.pause();
file.write(buffer, function() {
self.resume();
});
});
});

part.on('end', function() {
file.end(function() {
self._flushing--;
self.emit('file', part.name, file);
self._maybeEnd();
part.on('end', function() {
file.end(function() {
self._flushing--;
self.emit('file', part.name, file);
self._maybeEnd();
});
});
});
}
};

function dummyParser(self) {
Expand Down