-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec8087e
commit e16ac55
Showing
2 changed files
with
76 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,71 @@ | ||
var busboy = require('connect-busboy'), | ||
fs = require('fs-extra'), | ||
streamifier = require('streamifier'); | ||
fs = require('fs-extra'), | ||
streamifier = require('streamifier'); | ||
|
||
module.exports = function(options) { | ||
options = options || {}; | ||
options = options || {}; | ||
|
||
return function(req, res, next) { | ||
return busboy(options)(req, res, function() { | ||
return function(req, res, next) { | ||
return busboy(options)(req, res, function() { | ||
|
||
// If no busboy req obj, then no uploads are taking place | ||
if (!req.busboy) | ||
return next(); | ||
// If no busboy req obj, then no uploads are taking place | ||
if (!req.busboy) | ||
return next(); | ||
|
||
req.files = null; | ||
req.files = null; | ||
|
||
req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { | ||
req.body = req.body || {}; | ||
req.body[fieldname] = val; | ||
}); | ||
req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { | ||
req.body = req.body || {}; | ||
req.body[fieldname] = val; | ||
}); | ||
|
||
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | ||
var buf = new Buffer(0); | ||
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | ||
var buf = new Buffer(0); | ||
|
||
file.on('data', function(data) { | ||
buf = Buffer.concat([buf, data]); | ||
if (options.debug) { | ||
return console.log('Uploading %s -> %s', fieldname, filename); | ||
} | ||
}); | ||
file.on('data', function(data) { | ||
buf = Buffer.concat([buf, data]); | ||
if (options.debug) { | ||
return console.log('Uploading %s -> %s', fieldname, filename); | ||
} | ||
}); | ||
|
||
file.on('end', function() { | ||
if (!req.files) | ||
req.files = {}; | ||
// see: https://github.com/richardgirges/express-fileupload/issues/14 | ||
// firefox uploads empty file in case of cache miss when f5ing page. | ||
// resulting in unexpected behavior. if there is no file data, the file is invalid. | ||
if(!buf.length) | ||
return; | ||
file.on('end', function() { | ||
if (!req.files) | ||
req.files = {}; | ||
|
||
|
||
// see: https://github.com/richardgirges/express-fileupload/issues/14 | ||
// firefox uploads empty file in case of cache miss when f5ing page. | ||
// resulting in unexpected behavior. if there is no file data, the file is invalid. | ||
|
||
if(!buf.length) | ||
return; | ||
|
||
return req.files[fieldname] = { | ||
name: filename, | ||
data: buf, | ||
encoding: encoding, | ||
mimetype: mimetype, | ||
mv: function(path, callback) { | ||
var fstream; | ||
fstream = fs.createWriteStream(path); | ||
streamifier.createReadStream(buf).pipe(fstream); | ||
fstream.on('error', function(error) { | ||
callback(error); | ||
}); | ||
fstream.on('close', function() { | ||
callback(null); | ||
}); | ||
} | ||
}; | ||
}); | ||
}); | ||
return req.files[fieldname] = { | ||
name: filename, | ||
data: buf, | ||
encoding: encoding, | ||
mimetype: mimetype, | ||
mv: function(path, callback) { | ||
var fstream; | ||
fstream = fs.createWriteStream(path); | ||
streamifier.createReadStream(buf).pipe(fstream); | ||
fstream.on('error', function(error) { | ||
callback(error); | ||
}); | ||
fstream.on('close', function() { | ||
callback(null); | ||
}); | ||
} | ||
}; | ||
|
||
|
||
}); | ||
}); | ||
|
||
req.busboy.on('finish', next); | ||
req.busboy.on('finish', next); | ||
|
||
req.pipe(req.busboy); | ||
}); | ||
}; | ||
req.pipe(req.busboy); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
var express = require('express'), | ||
fileUpload = require('../lib/index.js'), | ||
app = express(); | ||
fileUpload = require('../lib/index.js'), | ||
app = express(); | ||
|
||
app.use('/form', express.static(__dirname + '/upload.test.html')); | ||
|
||
// default options | ||
app.use(fileUpload()); | ||
|
||
app.get('/ping', function(req, res) { | ||
res.send('pong'); | ||
res.send('pong'); | ||
}); | ||
|
||
app.post('/upload', function(req, res) { | ||
var sampleFile, uploadPath; | ||
var sampleFile, uploadPath; | ||
|
||
if (!req.files) { | ||
res.status(400).send('No files were uploaded.'); | ||
return; | ||
} | ||
if (!req.files) { | ||
res.status(400).send('No files were uploaded.'); | ||
return; | ||
} | ||
|
||
sampleFile = req.files.sampleFile; | ||
sampleFile = req.files.sampleFile; | ||
|
||
uploadPath = __dirname + '/uploadedfiles/' + sampleFile.name; | ||
uploadPath = __dirname + '/uploadedfiles/' + sampleFile.name; | ||
|
||
sampleFile.mv(uploadPath, function(err) { | ||
if (err) { | ||
res.status(500).send(err); | ||
} | ||
else { | ||
res.send('File uploaded to ' + uploadPath); | ||
} | ||
}); | ||
sampleFile.mv(uploadPath, function(err) { | ||
if (err) { | ||
res.status(500).send(err); | ||
} | ||
else { | ||
res.send('File uploaded to ' + uploadPath); | ||
} | ||
}); | ||
}); | ||
|
||
app.listen(8000, function() { | ||
console.log('Express server listening on port 8000'); | ||
console.log('Express server listening on port 8000'); | ||
}) |