dev
James Seppi 5 years ago
parent aa31b2d006
commit 25be06d5d9
  1. 9
      lib/utilities.js
  2. 8
      package-lock.json
  3. 3
      package.json
  4. 12
      test/numFilesLimitHandler.spec.js
  5. 5
      test/utilities.spec.js

@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const Readable = require('stream').Readable;
// Parameters for safe file name parsing.
@ -108,9 +109,11 @@ const checkAndMakeDir = (fileUploadOptions, filePath) => {
// Check whether folder for the file exists.
if (!filePath) return false;
const parentPath = path.dirname(filePath);
// Create folder if it is not exists.
if (!fs.existsSync(parentPath)) fs.mkdirSync(parentPath, { recursive: true });
// Checks folder again and return a results.
// Create folder if it does not exist.
if (!fs.existsSync(parentPath)) {
mkdirp.sync(filePath);
}
// Check folder again and return the result.
return fs.existsSync(parentPath);
};

8
package-lock.json generated

@ -1439,18 +1439,16 @@
},
"mkdirp": {
"version": "0.5.1",
"resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true,
"requires": {
"minimist": "0.0.8"
},
"dependencies": {
"minimist": {
"version": "0.0.8",
"resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
}
}
},

@ -10,7 +10,8 @@
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"dependencies": {
"busboy": "^0.2.14"
"busboy": "^0.2.14",
"mkdirp": "^0.5.1"
},
"engines": {
"node": ">=6.0.0"

@ -18,7 +18,7 @@ describe('Test Multiple File Upload With Files Limit Handler', function() {
describe('Run numFilesLimitHandler on limit reached.', function(){
before(function() {
app = server.setup({
limits: { files: 2 }, // set limit of 2 files
limits: { files: 3 }, // set limit of 2 files
numFilesLimitHandler: (req, res) => { // set limit handler
res.writeHead(500, { Connection: 'close', 'Content-Type': 'application/json'});
res.end(JSON.stringify({response: 'Too many files!'}));
@ -27,12 +27,11 @@ describe('Test Multiple File Upload With Files Limit Handler', function() {
});
});
it('Run files limit handler when too many files', (done) => {
it('Runs handler when too many files', (done) => {
const req = request(app).post('/upload/multiple');
['car.png', 'tree.png', 'basketball.png'].forEach((fileName, index) => {
['car.png', 'tree.png', 'basketball.png', 'car.png'].forEach((fileName, index) => {
let filePath = path.join(fileDir, fileName);
uploadedFilesPath.push(path.join(uploadDir, fileName));
req.attach(`testFile${index+1}`, filePath);
});
@ -44,12 +43,11 @@ describe('Test Multiple File Upload With Files Limit Handler', function() {
});
});
it('Does not run limit handler when number of files is below limit', (done) => {
it('Does not run handler when number of files is below limit', (done) => {
const req = request(app).post('/upload/multiple');
['car.png', 'tree.png'].forEach((fileName, index) => {
['car.png', 'tree.png', 'basketball.png'].forEach((fileName, index) => {
let filePath = path.join(fileDir, fileName);
uploadedFilesPath.push(path.join(uploadDir, fileName));
req.attach(`testFile${index+1}`, filePath);
});

@ -18,7 +18,8 @@ const {
deleteFile,
copyFile,
saveBufferToFile,
parseFileName
parseFileName,
uriDecodeFileName
} = require('../lib/utilities');
const mockFile = 'basketball.png';
@ -229,7 +230,7 @@ describe('Test of the utilities functions', function() {
//
it('checkAndMakeDir creates a dir recursively if path to the file not exists', ()=>{
let dir = path.join(uploadDir, 'testfolder', 'testsubfolder', 'testfile');
assert.equal(checkAndMakeDir({createParentPath: true}, dir), true);
assert.equal(checkAndMakeDir({createParentPath: true }, dir), true);
});
});
//saveBufferToFile tests

Loading…
Cancel
Save