express-fileupload
Advanced tools
Comparing version 1.1.2-alpha.1 to 1.1.3-alpha.1
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const streamifier = require('streamifier'); | ||
const { | ||
isFunc, | ||
checkAndMakeDir, | ||
copyFile, | ||
saveBufferToFile | ||
} = require('./utilities.js'); | ||
/** | ||
* Returns true if argument is function. | ||
*/ | ||
const isFunc = func => func && func.constructor && func.call && func.apply; | ||
/** | ||
* Creates a folder for file specified in the path variable | ||
* @param {Object} fileUploadOptions | ||
* @param {String} filePath | ||
*/ | ||
const checkAndMakeDir = function(fileUploadOptions, filePath){ | ||
if (fileUploadOptions && fileUploadOptions.createParentPath) { | ||
const parentPath = path.dirname(filePath); | ||
if (!fs.existsSync(parentPath)) { | ||
fs.mkdirSync(parentPath); | ||
} | ||
} | ||
}; | ||
/** | ||
* Returns Local function that moves the file to a different location on the filesystem | ||
@@ -36,8 +21,16 @@ * which takes two function arguments to make it compatible w/ Promise or Callback APIs | ||
errorFunc = isFunc(errorFunc) ? errorFunc : successFunc; | ||
fs.rename(options.tempFilePath, filePath, function(err){ | ||
// Copy temporary file. | ||
copyFile(options.tempFilePath, filePath, function(err){ | ||
if (err) { | ||
errorFunc(err); | ||
} else { | ||
successFunc(); | ||
return; | ||
} | ||
// Delete temporary file. | ||
fs.unlink(options.tempFilePath, (err) => { | ||
if (err) { | ||
errorFunc(err); | ||
} else { | ||
successFunc(); | ||
} | ||
}); | ||
}); | ||
@@ -57,10 +50,9 @@ }; | ||
errorFunc = isFunc(errorFunc) ? errorFunc : successFunc; | ||
const fstream = fs.createWriteStream(filePath); | ||
streamifier.createReadStream(options.buffer).pipe(fstream); | ||
fstream.on('error', function(error) { | ||
errorFunc(error); | ||
saveBufferToFile(options.buffer, filePath, function(err){ | ||
if (err) { | ||
errorFunc(err); | ||
} else { | ||
successFunc(); | ||
} | ||
}); | ||
fstream.on('close', function() { | ||
successFunc(); | ||
}); | ||
}; | ||
@@ -67,0 +59,0 @@ }; |
const fs = require('fs'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const {checkAndMakeDir} = require('./utilities.js'); | ||
module.exports = function(options, fieldname, filename) { | ||
const dir = path.normalize(options.tempFileDir || process.cwd() + '/tmp/'); | ||
const tempFilePath = path.join(dir, 'tmp' + Date.now()); | ||
checkAndMakeDir({createParentPath: true}, tempFilePath); | ||
let hash = crypto.createHash('md5'); | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
let tempFilePath = path.join(dir, 'tmp' + Date.now()); | ||
let writeStream = fs.createWriteStream(tempFilePath); | ||
@@ -14,0 +14,0 @@ let fileSize = 0; // eslint-disable-line |
{ | ||
"name": "express-fileupload", | ||
"version": "1.1.2-alpha.1", | ||
"version": "1.1.3-alpha.1", | ||
"author": "Richard Girges <richardgirges@gmail.com>", | ||
@@ -13,7 +13,6 @@ "description": "Simple express file upload middleware that wraps around Busboy", | ||
"dependencies": { | ||
"busboy": "^0.2.14", | ||
"streamifier": "^0.1.1" | ||
"busboy": "^0.2.14" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=6.0.0" | ||
}, | ||
@@ -20,0 +19,0 @@ "keywords": [ |
@@ -8,10 +8,8 @@ 'use strict'; | ||
const fileFactory = require('../lib').fileFactory; | ||
const {isFunc} = require('../lib/utilities.js'); | ||
const server = require('./server'); | ||
const mockBuffer = fs.readFileSync(path.join(server.fileDir, 'basketball.png')); | ||
/** | ||
* Returns true if argument is function. | ||
*/ | ||
const isFunc = func => (func && func.constructor && func.call && func.apply) ? true : false; | ||
const mockFile = path.join(server.fileDir, 'basketball.png'); | ||
const mockBuffer = fs.readFileSync(mockFile); | ||
@@ -30,21 +28,2 @@ describe('Test of the fileFactory factory', function() { | ||
describe('File object behavior', function() { | ||
const file = fileFactory({ | ||
name: 'basketball.png', | ||
buffer: mockBuffer | ||
}); | ||
it('move the file to the specified folder', function(done) { | ||
file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) { | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
it('reject the mv if the destination does not exists', function(done) { | ||
file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) { | ||
assert.ok(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Properties', function() { | ||
@@ -92,2 +71,45 @@ it('contains the name property', function() { | ||
}); | ||
describe('File object behavior for in memory upload', function() { | ||
const file = fileFactory({ | ||
name: 'basketball.png', | ||
buffer: mockBuffer | ||
}); | ||
it('move the file to the specified folder', function(done) { | ||
file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) { | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
it('reject the mv if the destination does not exists', function(done) { | ||
file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) { | ||
assert.ok(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('File object behavior for upload into temporary file', function() { | ||
const file = fileFactory({ | ||
name: 'basketball.png', | ||
buffer: mockBuffer, | ||
tempFilePath: mockFile | ||
}); | ||
it('move the file to the specified folder', function(done) { | ||
file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) { | ||
if (!err){ | ||
//Place back moved file | ||
fs.renameSync(path.join(server.uploadDir, 'basketball.png'), mockFile); | ||
} | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
it('reject the mv if the destination does not exists', function(done) { | ||
file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) { | ||
assert.ok(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,2 @@ 'use strict'; | ||
const server = require('./server'); | ||
const app = server.setup(); | ||
const clearUploadsDir = server.clearUploadsDir; | ||
@@ -29,5 +28,3 @@ const fileDir = server.fileDir; | ||
clearUploadsDir(); | ||
let filesFound = fs.readdirSync(uploadDir).length; | ||
done(filesFound ? `Directory not empty. Found ${filesFound} files.` : null); | ||
@@ -38,2 +35,4 @@ }); | ||
describe('Test Single File Upload', function() { | ||
const app = server.setup(); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
@@ -135,3 +134,179 @@ let fileName = mockFiles[i]; | ||
describe('Test Single File Upload w/ .mv()', function() { | ||
const app = server.setup(); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
let fileName = mockFiles[i]; | ||
it(`upload ${fileName} with POST w/ .mv()`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
it(`upload ${fileName} with PUT w/ .mv()`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
} | ||
}); | ||
describe('Test Single File Upload with useTempFiles option set to true', function() { | ||
const app = server.setup({ | ||
useTempFiles: true, | ||
tempFileDir: '/tmp/' | ||
}); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
let fileName = mockFiles[i]; | ||
it(`upload ${fileName} with POST`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
it(`upload ${fileName} with PUT`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
} | ||
it('fail when no files were attached', function(done) { | ||
request(app) | ||
.post('/upload/single') | ||
.expect(400) | ||
.end(done); | ||
}); | ||
it('fail when using GET', function(done) { | ||
let filePath = path.join(fileDir, mockFiles[0]); | ||
request(app) | ||
.get('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect(400) | ||
.end(done); | ||
}); | ||
it('fail when using HEAD', function(done) { | ||
let filePath = path.join(fileDir, mockFiles[0]); | ||
request(app) | ||
.head('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect(400) | ||
.end(done); | ||
}); | ||
}); | ||
describe('Test Single File Upload w/ .mv() Promise', function() { | ||
const app = server.setup(); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
@@ -233,3 +408,108 @@ let fileName = mockFiles[i]; | ||
describe('Test Single File Upload w/ .mv() Promise and useTempFiles set to true', function() { | ||
const app = server.setup({ | ||
useTempFiles: true, | ||
tempFileDir: '/tmp/' | ||
}); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
let fileName = mockFiles[i]; | ||
it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single/promise') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) { | ||
let filePath = path.join(fileDir, fileName); | ||
let fileBuffer = fs.readFileSync(filePath); | ||
let fileHash = md5(fileBuffer); | ||
let fileStat = fs.statSync(filePath); | ||
let uploadedFilePath = path.join(uploadDir, fileName); | ||
clearUploadsDir(); | ||
request(app) | ||
.post('/upload/single/promise') | ||
.attach('testFile', filePath) | ||
.expect((res)=>{ | ||
res.body.uploadDir = ''; | ||
res.body.uploadPath = ''; | ||
}) | ||
.expect(200, { | ||
name: fileName, | ||
md5: fileHash, | ||
size: fileStat.size, | ||
uploadDir: '', | ||
uploadPath: '' | ||
}) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
} | ||
it('fail when no files were attached', function(done) { | ||
request(app) | ||
.post('/upload/single') | ||
.expect(400) | ||
.end(done); | ||
}); | ||
it('fail when using GET', function(done) { | ||
let filePath = path.join(fileDir, mockFiles[0]); | ||
request(app) | ||
.get('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect(400) | ||
.end(done); | ||
}); | ||
it('fail when using HEAD', function(done) { | ||
let filePath = path.join(fileDir, mockFiles[0]); | ||
request(app) | ||
.head('/upload/single') | ||
.attach('testFile', filePath) | ||
.expect(400) | ||
.end(done); | ||
}); | ||
}); | ||
describe('Test Multi-File Upload', function() { | ||
const app = server.setup(); | ||
it('upload multiple files with POST', function(done) { | ||
@@ -290,2 +570,4 @@ let req = request(app).post('/upload/multiple'); | ||
describe('Test File Array Upload', function() { | ||
const app = server.setup(); | ||
it('upload array of files with POST', function(done) { | ||
@@ -338,2 +620,4 @@ let req = request(app).post('/upload/array'); | ||
describe('Test Upload With Fields', function() { | ||
const app = server.setup(); | ||
for (let i = 0; i < mockFiles.length; i++) { | ||
@@ -340,0 +624,0 @@ let fileName = mockFiles[i]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1197098
1
33
2087
10
- Removedstreamifier@^0.1.1
- Removedstreamifier@0.1.1(transitive)