done purchases remove, ocr scan, read image
This commit is contained in:
100
express-server/node_modules/express-fileupload/lib/fileFactory.js
generated
vendored
Normal file
100
express-server/node_modules/express-fileupload/lib/fileFactory.js
generated
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const streamifier = require('streamifier');
|
||||
const md5 = require('md5');
|
||||
|
||||
module.exports = function(options, fileUploadOptions = null) {
|
||||
const output = {
|
||||
name: options.name,
|
||||
data: options.buffer,
|
||||
encoding: options.encoding,
|
||||
tempFilePath: options.tempFilePath,
|
||||
truncated: options.truncated,
|
||||
mimetype: options.mimetype,
|
||||
md5: () => md5(options.buffer),
|
||||
mv: function(filePath, callback) {
|
||||
// Callback is passed in, use the callback API
|
||||
if (callback) {
|
||||
if (options.buffer.length && !options.tempFilePath) {
|
||||
moveFromBuffer(
|
||||
() => {
|
||||
callback(null);
|
||||
},
|
||||
error => {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
moveFromTemp(
|
||||
() => {
|
||||
callback(null);
|
||||
},
|
||||
error => {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Otherwise, return a promise
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (options.buffer) {
|
||||
moveFromBuffer(resolve, reject);
|
||||
} else {
|
||||
moveFromTemp(resolve, reject);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkAndMakeDir(){
|
||||
if (fileUploadOptions && fileUploadOptions.createParentPath) {
|
||||
const parentPath = path.dirname(filePath);
|
||||
if (!fs.existsSync(parentPath)) {
|
||||
fs.mkdirSync(parentPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local function that moves the file to a different location on the filesystem
|
||||
* Takes two function arguments to make it compatible w/ Promise or Callback APIs
|
||||
* @param {Function} successFunc
|
||||
* @param {Function} errorFunc
|
||||
*/
|
||||
function moveFromTemp(successFunc, errorFunc) {
|
||||
checkAndMakeDir();
|
||||
fs.rename(options.tempFilePath, filePath, function(err){
|
||||
if (err) {
|
||||
errorFunc(err);
|
||||
} else {
|
||||
successFunc();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function moveFromBuffer(successFunc, errorFunc) {
|
||||
checkAndMakeDir();
|
||||
|
||||
const fstream = fs.createWriteStream(filePath);
|
||||
|
||||
streamifier.createReadStream(options.buffer).pipe(fstream);
|
||||
|
||||
fstream.on('error', function(error) {
|
||||
errorFunc(error);
|
||||
});
|
||||
|
||||
fstream.on('close', function() {
|
||||
successFunc();
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (options.size) {
|
||||
output.size = options.size;
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
37
express-server/node_modules/express-fileupload/lib/index.js
generated
vendored
Normal file
37
express-server/node_modules/express-fileupload/lib/index.js
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
const fileFactory = require('./fileFactory');
|
||||
const processMultipart = require('./processMultipart');
|
||||
const isEligibleRequest = require('./isEligibleRequest');
|
||||
const processNested = require('./processNested');
|
||||
|
||||
const fileUploadOptionsDefaults = {
|
||||
safeFileNames: false,
|
||||
preserveExtension: false,
|
||||
abortOnLimit: false,
|
||||
createParentPath: false,
|
||||
parseNested: false,
|
||||
useTempFiles: false,
|
||||
tempFileDir: '/tmp'
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose the file upload middleware
|
||||
*/
|
||||
module.exports = function(fileUploadOptions) {
|
||||
fileUploadOptions = Object.assign({}, fileUploadOptionsDefaults, fileUploadOptions || {});
|
||||
|
||||
return function(req, res, next) {
|
||||
if (!isEligibleRequest(req)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
processMultipart(fileUploadOptions, req, res, next);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Quietly expose fileFactory and processNested; useful for testing
|
||||
*/
|
||||
module.exports.fileFactory = fileFactory;
|
||||
module.exports.processNested = processNested;
|
44
express-server/node_modules/express-fileupload/lib/isEligibleRequest.js
generated
vendored
Normal file
44
express-server/node_modules/express-fileupload/lib/isEligibleRequest.js
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
const ACCEPTABLE_CONTENT_TYPE = /^(?:multipart\/.+)$/i;
|
||||
const UNACCEPTABLE_METHODS = [
|
||||
'GET',
|
||||
'HEAD'
|
||||
];
|
||||
|
||||
/**
|
||||
* Ensures that the request in question is eligible for file uploads
|
||||
* @param {Object} req Express req object
|
||||
*/
|
||||
module.exports = function(req) {
|
||||
return hasBody(req) && hasAcceptableMethod(req) && hasAcceptableContentType(req);
|
||||
};
|
||||
|
||||
/**
|
||||
* Ensures the request is not using a non-compliant multipart method
|
||||
* such as GET or HEAD
|
||||
* @param {Object} req Express req object
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function hasAcceptableMethod(req) {
|
||||
return (UNACCEPTABLE_METHODS.indexOf(req.method) < 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that only multipart requests are processed by express-fileupload
|
||||
* @param {Object} req Express req object
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function hasAcceptableContentType(req) {
|
||||
let str = (req.headers['content-type'] || '').split(';')[0];
|
||||
|
||||
return ACCEPTABLE_CONTENT_TYPE.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the request contains a content body
|
||||
* @param {Object} req Express req object
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function hasBody(req) {
|
||||
return ('transfer-encoding' in req.headers) ||
|
||||
('content-length' in req.headers && req.headers['content-length'] !== '0');
|
||||
}
|
176
express-server/node_modules/express-fileupload/lib/processMultipart.js
generated
vendored
Normal file
176
express-server/node_modules/express-fileupload/lib/processMultipart.js
generated
vendored
Normal file
@ -0,0 +1,176 @@
|
||||
const Busboy = require('busboy');
|
||||
const fileFactory = require('./fileFactory');
|
||||
const {
|
||||
getTempFilePath,
|
||||
complete,
|
||||
cleanupStream,
|
||||
tempFileHandler
|
||||
} = require('./tempFileHandler');
|
||||
const processNested = require('./processNested');
|
||||
|
||||
/**
|
||||
* Processes multipart request
|
||||
* Builds a req.body object for fields
|
||||
* Builds a req.files object for files
|
||||
* @param {Object} options expressFileupload and Busboy options
|
||||
* @param {Object} req Express request object
|
||||
* @param {Object} res Express response object
|
||||
* @param {Function} next Express next method
|
||||
* @return {void}
|
||||
*/
|
||||
module.exports = function processMultipart(options, req, res, next) {
|
||||
let busboyOptions = {};
|
||||
let busboy;
|
||||
|
||||
req.files = null;
|
||||
|
||||
// Build busboy config
|
||||
for (let k in options) {
|
||||
if (Object.prototype.hasOwnProperty.call(options, k)) {
|
||||
busboyOptions[k] = options[k];
|
||||
}
|
||||
}
|
||||
|
||||
// Attach request headers to busboy config
|
||||
busboyOptions.headers = req.headers;
|
||||
|
||||
// Init busboy instance
|
||||
busboy = new Busboy(busboyOptions);
|
||||
|
||||
// Build multipart req.body fields
|
||||
busboy.on('field', function(fieldname, val) {
|
||||
req.body = req.body || {};
|
||||
|
||||
let prev = req.body[fieldname];
|
||||
|
||||
if (!prev) {
|
||||
return (req.body[fieldname] = val);
|
||||
}
|
||||
|
||||
if (Array.isArray(prev)) {
|
||||
return prev.push(val);
|
||||
}
|
||||
|
||||
req.body[fieldname] = [prev, val];
|
||||
});
|
||||
|
||||
// Build req.files fields
|
||||
busboy.on('file', function(fieldname, file, filename, encoding, mime) {
|
||||
const buffers = [];
|
||||
let safeFileNameRegex = /[^\w-]/g;
|
||||
const memHandler = function(data) {
|
||||
buffers.push(data);
|
||||
if (options.debug) {
|
||||
return console.log('Uploading %s -> %s', fieldname, filename); // eslint-disable-line
|
||||
}
|
||||
};
|
||||
const dataHandler = options.useTempFiles
|
||||
? tempFileHandler(options, fieldname, filename)
|
||||
: memHandler;
|
||||
|
||||
file.on('limit', () => {
|
||||
if (options.abortOnLimit) {
|
||||
res.writeHead(413, { Connection: 'close' });
|
||||
res.end('File size limit has been reached');
|
||||
}
|
||||
});
|
||||
|
||||
file.on('data', dataHandler);
|
||||
|
||||
file.on('end', function() {
|
||||
if (!req.files) {
|
||||
req.files = {};
|
||||
}
|
||||
if (options.useTempFiles) {
|
||||
complete(filename);
|
||||
}
|
||||
|
||||
const buffer = Buffer.concat(buffers);
|
||||
// see: https://github.com/richardgirges/express-fileupload/issues/14
|
||||
// firefox uploads empty file in case of cache miss when f5ing page.
|
||||
// resulting in unexpected behavior. if there is no file data, the file is invalid.
|
||||
if (!buffer.length && !options.useTempFiles) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.safeFileNames) {
|
||||
let maxExtensionLength = 3;
|
||||
let extension = '';
|
||||
|
||||
if (typeof options.safeFileNames === 'object') {
|
||||
safeFileNameRegex = options.safeFileNames;
|
||||
}
|
||||
|
||||
maxExtensionLength = parseInt(options.preserveExtension);
|
||||
if (options.preserveExtension || maxExtensionLength === 0) {
|
||||
if (isNaN(maxExtensionLength)) {
|
||||
maxExtensionLength = 3;
|
||||
} else {
|
||||
maxExtensionLength = Math.abs(maxExtensionLength);
|
||||
}
|
||||
|
||||
let filenameParts = filename.split('.');
|
||||
let filenamePartsLen = filenameParts.length;
|
||||
if (filenamePartsLen > 1) {
|
||||
extension = filenameParts.pop();
|
||||
|
||||
if (
|
||||
extension.length > maxExtensionLength &&
|
||||
maxExtensionLength > 0
|
||||
) {
|
||||
filenameParts[filenameParts.length - 1] +=
|
||||
'.' +
|
||||
extension.substr(0, extension.length - maxExtensionLength);
|
||||
extension = extension.substr(-maxExtensionLength);
|
||||
}
|
||||
|
||||
extension = maxExtensionLength
|
||||
? '.' + extension.replace(safeFileNameRegex, '')
|
||||
: '';
|
||||
filename = filenameParts.join('.');
|
||||
}
|
||||
}
|
||||
|
||||
filename = filename.replace(safeFileNameRegex, '').concat(extension);
|
||||
}
|
||||
|
||||
const newFile = fileFactory(
|
||||
{
|
||||
name: filename,
|
||||
buffer,
|
||||
tempFilePath: getTempFilePath(),
|
||||
encoding,
|
||||
truncated: file.truncated,
|
||||
mimetype: mime
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
// Non-array fields
|
||||
if (!req.files.hasOwnProperty(fieldname)) {
|
||||
req.files[fieldname] = newFile;
|
||||
} else {
|
||||
// Array fields
|
||||
if (req.files[fieldname] instanceof Array) {
|
||||
req.files[fieldname].push(newFile);
|
||||
} else {
|
||||
req.files[fieldname] = [req.files[fieldname], newFile];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
file.on('error', cleanupStream, next);
|
||||
});
|
||||
|
||||
busboy.on('finish', () => {
|
||||
if (options.parseNested) {
|
||||
req.body = processNested(req.body);
|
||||
req.files = processNested(req.files);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
busboy.on('error', next);
|
||||
|
||||
req.pipe(busboy);
|
||||
};
|
28
express-server/node_modules/express-fileupload/lib/processNested.js
generated
vendored
Normal file
28
express-server/node_modules/express-fileupload/lib/processNested.js
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
module.exports = function(data){
|
||||
if (!data || data.length < 1) return {};
|
||||
|
||||
let d = {},
|
||||
keys = Object.keys(data);
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i],
|
||||
value = data[key],
|
||||
current = d,
|
||||
keyParts = key
|
||||
.replace(new RegExp(/\[/g), '.')
|
||||
.replace(new RegExp(/\]/g), '')
|
||||
.split('.');
|
||||
|
||||
for (let index = 0; index < keyParts.length; index++){
|
||||
let k = keyParts[index];
|
||||
if (index >= keyParts.length - 1){
|
||||
current[k] = value;
|
||||
} else {
|
||||
if (!current[k]) current[k] = !isNaN(keyParts[index + 1]) ? [] : {};
|
||||
current = current[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return d;
|
||||
};
|
42
express-server/node_modules/express-fileupload/lib/tempFileHandler.js
generated
vendored
Normal file
42
express-server/node_modules/express-fileupload/lib/tempFileHandler.js
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
const fs = require('fs');
|
||||
let writeStream;
|
||||
let tempFilePath;
|
||||
|
||||
module.exports.getTempFilePath = function() {
|
||||
return tempFilePath;
|
||||
};
|
||||
|
||||
module.exports.cleanupStream = function() {
|
||||
writeStream.end();
|
||||
|
||||
fs.unlink(tempFilePath, function(err) {
|
||||
if (err) throw err;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.complete = function(){
|
||||
writeStream.end();
|
||||
};
|
||||
|
||||
module.exports.tempFileHandler = function(options, fieldname, filename) {
|
||||
const dir = __dirname + (options.tempFileDir || '/tmp/');
|
||||
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
tempFilePath = dir + 'tmp' + Date.now();
|
||||
writeStream = fs.createWriteStream(tempFilePath);
|
||||
let fileSize = 0; // eslint-disable-line
|
||||
|
||||
return function(data) {
|
||||
writeStream.write(data);
|
||||
fileSize += data.length;
|
||||
if (options.debug) {
|
||||
return console.log( // eslint-disable-line
|
||||
`Uploaded ${data.length} bytes for `,
|
||||
fieldname,
|
||||
filename
|
||||
);
|
||||
}
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user