docparse-upload-process
Version:
process upload api request for the docparse server
267 lines (235 loc) • 7.21 kB
JavaScript
var inspect = require('eyespect').inspector();
var fs = require('fs');
var restify = require('restify');
var pathhash = require('pathhash');
var async = require('async');
var rk = require('required-keys');
var Supplier = require('docparse-supplier');
var User = require('docparse-user');
var Upload = require('docparse-upload');
var add = require('./add');
var save = require('./save');
var fetch = require('./fetch');
var performOCR = require('./performOCR');
var uploadExists = require('./exists');
var userInUpload = require('./userInUpload');
function buildError(param, msg, value) {
return { param: param, msg: msg, value: value };
}
function keysExist(data, cb) {
data.logger.debug('checking required keys are set for upload create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
var keys = [
'supplierCode',
'filePath',
'filename',
'userID',
'config',
'logger'
];
rk.nonNull(data, keys, function (err, reply) {
if (err) { return cb(err); }
cb();
});
}
function checkSupplier(data, cb) {
data.logger.debug('checking supplier for upload create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
Supplier.findOne({supplier_code: data.supplierCode}, function (err, reply) {
if (err) { return cb(err); }
if (!reply) {
return cb('supplier not found with given supplierCode: ' +data.supplierCode);
}
data.supplierID = reply._id.toString();
data.supplierName = reply.supplier_name;
return cb();
});
}
function checkUser(data, cb) {
data.logger.debug('checking user upload create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
User.findById(data.userID, function (err, reply) {
if (err || ! reply) { return cb('user not found with id: ' + data.userID); }
data.user = reply;
return cb();
});
}
function decodeAuth(req) {
var auth = req.headers['authorization'];
if (!auth) { return null; }
var tmp = auth.split(' '); // Split on a space, the original auth looks like "Basic Y2hhcmxlczoxMjM0NQ==" and we need the 2nd part
var buf = new Buffer(tmp[1], 'base64'); // create a buffer and tell it the data coming in is base64
var plainAuth = buf.toString(); // read it back out as a string
// At this point plainAuth = "username:password"
var creds = plainAuth.split(':'); // split on a ':'
var username = creds[0];
var password = creds[1];
return {
username: username,
password: password
};
}
function checkFileExists(data, cb) {
data.logger.debug('checking if file exists on disk for create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
fs.exists(data.filePath, function (exists) {
if (!exists) {
return cb('file not found at path: ' + data.filePath);
}
cb();
});
}
function hashFile(data, cb) {
data.logger.debug('hashing file for upload create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
pathhash(data.filePath, function (err, reply) {
if (err) {
return cb(err);
}
data.hash = reply;
cb();
});
}
module.exports = function(data, callback) {
if (!callback) {
callback = data;
return callback('no data parameter supplied');
}
console.log('create request received');
var keys = ['config', 'logger'];
rk.truthy(data, keys, function (err, reply) {
if (err) { return callback(err); }
var logger = data.logger;
var upload;
var output;
var config = data.config;
async.series([
function (cb) { keysExist(data, cb) },
function (cb) { checkFileExists(data, cb) },
function (cb) { checkSupplier(data, cb) },
function (cb) { checkUser(data, cb) },
function (cb) { hashFile(data, cb) },
function (cb) {
logger.debug('checking if upload exists already for create request', {
type: 'api', hash: data.hash,
filePath: data.filePath
});
uploadExists(data, function (err, reply) {
if (err) { return cb(err); }
upload = reply;
cb();
});
},
function(cb) {
if (!upload) {
logger.debug('upload does not yet exist for create request', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
return cb();
}
data.upload = upload;
logger.debug('upload does already exist for create request, checking user is in the upload', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
userInUpload(data, function (err, inUpload) {
if (err) { return cb(err); }
if (inUpload) {
logger.debug('upload does already exist for create request and user is in the upload, returning', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
return cb();
}
logger.debug('upload does already exist for create request and user IS NOT in the upload, adding them now', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
upload = null;
add(data, function (err, addReply) {
if (err) { return cb(err); }
upload = addReply;
cb();
});
});
},
function(cb) {
if (upload) { return cb(); }
logger.debug('upload does yet exist, saving now', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
save(data, function (err, reply) {
if (err) {
logger.error('error saving upload in create module', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
return cb(err);
}
logger.debug('upload saved successfully', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
data.upload = reply;
cb();
});
}
], function(err) {
if (err) {
return callback(err);
}
if (upload) {
logger.info('upload create request completed successfully, returning upload now', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
return callback(null, upload);
}
logger.debug('performing ocr for newly create upload', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
performOCR(data, function (err, reply) {
if (err) { return callback(err); }
var upload = reply.toObject();
var fetchData = {
userID: data.userID,
hash: reply.hash,
config: config
};
logger.debug('upload create performOCR callback fired, about to fetch the latest status for the uplaod', {
type: 'api',
hash: data.hash,
filePath: data.filePath
});
fetch(fetchData, callback);
});
});
});
};