Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-fileupload

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-fileupload - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

test/uploadedfiles/placeholder.txt

111

lib/index.js

@@ -1,61 +0,78 @@

var busboy = require('connect-busboy'),
fs = require('fs-extra'),
streamifier = require('streamifier');
var busboy = require('connect-busboy');
var fs = require('fs-extra');
var 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);
var safeFileNameRegex = /[^\w-]/g;
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]);
file.on('end', function() {
if (!req.files)
req.files = {};
if (options.debug) {
return console.log('Uploading %s -> %s', fieldname, filename);
}
});
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);
});
}
};
});
});
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;
req.busboy.on('finish', next);
if (options.safeFileNames) {
if (typeof options.safeFileNames === 'object')
safeFileNameRegex = options.safeFileNames;
req.pipe(req.busboy);
filename = filename.replace(safeFileNameRegex, '');
console.log('filename yo', filename);
}
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.pipe(req.busboy);
});
};
};
{
"name": "express-fileupload",
"version": "0.0.5",
"version": "0.0.6",
"author": "Richard Girges <richardgirges@gmail.com>",

@@ -5,0 +5,0 @@ "description": "Simple express file upload middleware that wraps around connect-busboy",

@@ -1,17 +0,33 @@

express-fileupload
===========
Simple express file upload middleware that wraps around [connect-busboy](https://github.com/mscdex/connect-busboy).
# Description
Simple express middleware for uploading files.
# Install
```bash
# With NPM
npm install --save express-fileupload
Install
=======
# With Yarn
yarn add express-fileupload
```
npm install express-fileupload
# Usage
When you upload a file, the file will be accessible from `req.files`.
### Example Scenario
* You're uploading a file called **car.jpg**
* Your input's name field is **foo**: `<input name="foo" type="file" />`
* In your express server request, you can access your uploaded file from `req.files.foo`:
```javascript
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
```
The **req.files.foo** object will contain the following:
* `req.files.foo.name`: "car.jpg"
* `req.files.foo.mv`: A function to move the file elsewhere on your server
* `req.files.mimetype`: The mimetype of your file
* `req.files.data`: A buffer representation of your file
Example
=======
### JavaScript
### Full Example
**Your node.js code:**
```javascript

@@ -26,39 +42,84 @@ var express = require('express');

app.post('/upload', function(req, res) {
var sampleFile;
var sampleFile;
if (!req.files) {
res.send('No files were uploaded.');
return;
}
if (!req.files) {
res.send('No files were uploaded.');
return;
}
sampleFile = req.files.sampleFile;
sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
if (err) {
res.status(500).send(err);
}
else {
res.send('File uploaded!');
}
});
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
sampleFile = req.files.sampleFile;
// Use the mv() method to place the file somewhere on your server
sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
if (err) {
res.status(500).send(err);
}
else {
res.send('File uploaded!');
}
});
});
```
### Form
**Your HTML file upload form:**
```html
<html>
<body>
<form ref='uploadForm'
id='uploadForm'
action='http://localhost:8000/upload'
method='post'
encType="multipart/form-data">
<input type="file" name="sampleFile" />
<input type='submit' value='Upload!' />
</form>
</body>
<body>
<form ref='uploadForm'
id='uploadForm'
action='http://localhost:8000/upload'
method='post'
encType="multipart/form-data">
<input type="file" name="sampleFile" />
<input type='submit' value='Upload!' />
</form>
</body>
</html>
```
## Thanks & Credit
### Uploading Multiple Files
express-fileupload supports multiple file uploads at the same time.
* [Brian White](https://github.com/mscdex) for his stellar work on the [Busboy Package](https://github.com/mscdex/busboy) and the [connect-busboy Package](https://github.com/mscdex/connect-busboy)
Let's say you have three files in your form, each of the inputs with the name `my_profile_pic`, `my_pet`, and `my_cover_photo`:
```html
<input type="file" name="my_profile_pic" />
<input type="file" name="my_pet" />
<input type="file" name="my_cover_photo" />
```
These uploaded files would be accessible like so:
```javascript
app.post('/upload', function(req, res) {
// Uploaded files:
console.log(req.files.my_profile_pic.name);
console.log(req.files.my_pet.name);
console.log(req.files.my_cover_photo.name);
});
```
### Using Busboy Options
Pass in Busboy options directly to the express-fileupload middleware. [Check out the Busboy documentation here.](https://github.com/mscdex/busboy#api)
```javascript
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
```
### Available Options
Pass in non-Busboy options directly to the middleware. These are express-fileupload specific options.
Option | Acceptable&nbsp;Values | Details
--- | --- | ---
safeFileNames | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li>regex</li></ul> | Strips characters from the upload's filename. You can use custom regex to determine what to strip. If set to `true`, non-alphanumeric characters _except_ dashes and underscores will be stripped. This option is off by default.<br /><br />**Example #1 (strip slashes from file names):** `app.use(fileUpload({ safeFileNames: /\\/g }))`<br />**Example #2:** `app.use(fileUpload({ safeFileNames: true }))`
# Known Bugs
##### If you're using bodyParser middleware
Add `app.use(fileUpload())` *AFTER* `app.use(bodyParser.json)` and any other `bodyParser` middlewares! This limitation will be investigated in an upcoming release.
# Help Wanted
Pull Requests are welcomed!
# Thanks & Credit
[Brian White](https://github.com/mscdex) for his stellar work on the [Busboy Package](https://github.com/mscdex/busboy) and the [connect-busboy Package](https://github.com/mscdex/connect-busboy)
var express = require('express'),
fileUpload = require('../lib/index.js'),
app = express();
fileUpload = require('../lib/index.js'),
app = express();

@@ -11,29 +11,29 @@ app.use('/form', express.static(__dirname + '/upload.test.html'));

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');
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc