
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
uniquefilename
Advanced tools
A module to get a unique filename using incremental values.
npm install uniquefilename
var uniquefilename = require('uniquefilename');
options = {};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
// filename might be "/path/to/dir/file.jpg",
// "/path/to/dir/file-2.jpg", "/path/to/dir/file-3.jpg", etc...
// depending on the files that exist on your filesystem
});
If a callback is not supplied, a Promise is returned (using bluebird).
var uniquefilename = require('uniquefilename');
uniquefilename.get('/path/to/dir/file.jpg', {}).then(function (filename) {
console.log(filename);
});
var uniquefilename = require('uniquefilename');
options = {separator: '~', paddingCharacter: '0', paddingSize: 4, mode: 'alphanumeric'};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
// filename might be "/path/to/dir/file.jpg",
// "/path/to/dir/file~000h.jpg", "/path/to/dir/file~00h9.jpg", etc...
// depending on the files that exist on your filesystem
});
If the specified filename exists, the separator will be added before the incremental value such as: file{separator}2.jpg
The default value is '-'
.
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 the charset
optionIf you wish to left-pad the incremental values with a character, use this option. Here's an example :
var uniquefilename = require('uniquefilename');
options = {mode: 'alpha', paddingCharacter: '0', paddingSize: 3};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
// filename might be "/path/to/dir/file.jpg",
// "/path/to/dir/file-002.jpg", "/path/to/dir/file-045.jpg", etc...
// depending on the files that exist on your filesystem
});
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 = require('multer');
var path = require('path');
var uniquefilename = require('uniquefilename');
router.use(multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads/')
},
filename: function (req, file, cb) {
originalname = path.resolve('./public/uploads/' + file.originalname);
options = {};
uniquefilename.get(originalname, options, function(filename) {
cb(null, path.basename(filename));
});
}
})
}).single('thumbnail'));
FAQs
Finds a unique filename using incremental values
The npm package uniquefilename receives a total of 546 weekly downloads. As such, uniquefilename popularity was classified as not popular.
We found that uniquefilename demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.