uniquefilename
A module to get a filename - that doesn't already exist on the filesystem - using incremental values.
Install
npm install uniquefilename
Usage
uniquefilename.get(filepath, [options], [callback])
Basic example
var uniquefilename = ;options = {};uniquefilename;
Example with promises
If a callback is not supplied, a Promise is returned (using bluebird).
var uniquefilename = ;uniquefilename;
Advanced example
var uniquefilename = ;options = separator: '~' paddingCharacter: '0' paddingSize: 4 mode: 'alphanumeric';uniquefilename;
Options
separator
If the specified filename exists, the separator will be added before the incremental value such as: file{separator}2.jpg
The default value is '-'
.
mode
The mode allows you to specify which characters to use to generate the incremental value (the string after the separator)
The default value is 'numeric'
.
'numeric'
Using the following characters:1234567890
'alpha'
Using the following characters:abcdefghijklmnopqrstuvwxyz
'ALPHA'
Using the following characters:ABCDEFGHIJKLMNOPQRSTUVWXYZ
'alphanumeric'
Using the following characters:0123456789abcdefghijklmnopqrstuvwxyz
'ALPHANUMERIC'
Using the following characters:0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
'charset'
You must specify the characters you wish to use in thecharset
option
paddingCharacter && paddingSize
If you wish to left-pad the incremental values with a character, use this option. Here's an example :
var uniquefilename = ;options = mode: 'alpha' paddingCharacter: '0' paddingSize: 3;uniquefilename;
Using it with multer
Multer is a node.js middleware for handling multipart/form-data
, which is primarily used for uploading files.
The following example shows you how to use multer along with this module to move an uploaded file to a unique filename :
var multer = ;var path = ;var uniquefilename = ; router;