1.0.3 • Published 8 years ago
bolto-file-upload v1.0.3
File upload plugin for bolto framework. Usage is simple: install it, load into app and define a route. Also it is possible to use it to just parse uploads form and save files into specified folder, if you need to add custom logic after the files have been uploaded.
Install
To install it, just run this command in your project directory:
npm install bolto-file-upload --save
Settings
It's a good practice to keep your upload settings (as well as others) in environment-dependent configs:
// app/config/env.js
module.exports = {
all: {
host: 'localhost',
port: 8080,
// ...
fileUpload: {
images: {
field: 'image',
folder: '/storage/blog/uploads/images',
rootUrl: '/uploads/images'
}
}
},
production: {
host: 'XXX.XXX.XXX.XXX'
}
};
Available options
folder
String - Path to uploads folder, where uploaded files should be saved. Optional. Default value is"/tmp"
. NOTE: if path is not absolute, it calculates relative to current working directory.rootUrl
String - Will be added before file name when building the url. Optional. Default value is"/uploads"
.uploader
Object - Options for multer constructor. Optional. Default value is{}
.field
String - Name of form field that contains file(s). Optional. Default value is"upload"
.multiple
Boolean - Will parse form field with multiple files. Optional. Default value isfalse
. If enabled, information about uploaded files will be stored infiles
array, otherwise info about single file will be stored infile
attribute as a single object.limit
Number - Used ifmultiple
is set totrue
to limit number of files users can upload. Optional. Default value is10
.fields
Array - Parse specified file fields. Optional. Default value isundefined
. If you need to parse form with multiple file fields, specify them in format:
[
{ name: 'avatar', maxCount: 1 },
{ name: 'gallery', maxCount: 8 }
]
any
Boolean - Will parse all file fields in the form. Optional. Default value isfalse
. If enabled, information about uploaded files will be stored infiles
array.makeFileName
Function - Function that returns name for uploaded file. Function prototype isfunction(req, file, callback) { ... }
. Optional. By default uploaded file name will be generated automatically with pattern<16_alphanumeric_chars>-<Date.now()>.<extension>
.
Load into app
Add this plugin into your app on pre-configure
stage:
// index.js
var app = require('bolto');
app.pre('configure', ['$app', '$env', '$session', '$asset', '$settings'], function ($app, $env, $session, $asset, $settings) {
$env.load(require('./app/config/env'));
$settings.set('staticFolders', [
path.join(__dirname, 'public'),
path.join(__dirname, 'uploads')
]);
$asset.init({
timestamps: true
});
var redis = require('redis');
var redisCache = redis.createClient();
var RedisStore = require('connect-redis')($session);
var sessionStore = new RedisStore({
host: 'localhost',
port: 6379,
client: redisCache
});
$app.use($session({
secret: $env.get('session.secret'),
resave: true,
saveUninitialized: true,
store: sessionStore
}));
// Add FileUpload plugin to handle file uploads
require('bolto-file-upload')(app);
});
Usage
Usage is pretty simple. Use it like middleware in your router:
// UploadsRouter.js
var app = require('bolto');
app.router('/upload', ['$env', 'FileUploadPlugin', 'UserService'], function ($env, FileUpload, UserService) {
// Get environment-dependent file upload settings
var config = $env.get('fileUpload');
// Define router for images upload
this.post('/image', UserService.ensureAdmin, FileUpload(config.images).middleware);
});
API
Plugin instance exports two methods:
middleware(req, res, next)
- Used as middleware that parses form, saves uploaded files into specified folder and sends back JSON object withfile
orfiles
attributes.parse(req, res, callback)
- Used to parse form, save uploaded files into specified folder and callcallback
function with(err, result)
arguments, whereresult
is object withfile
orfiles
attributes. Also such attributes added tores.locals
.
Author
Created by Alex Chugaev (drop me a line)
License
MIT