UNPKG

docparse-upload-process

Version:

process upload api request for the docparse server

122 lines (114 loc) 3.06 kB
var querystring = require('querystring'); var request = require('request'); var inspect = require('eyespect').inspector(); var mongoose = require('mongoose'); var Upload = require('docparse-upload'); var User = require('docparse-user'); var async = require('async'); var userInUpload = require('./userInUpload'); var rk = require('required-keys'); function keysExist(data, cb) { var keys = [ 'userID', 'hash', 'config' ] rk.nonNull(data, keys, function (err, reply) { if (err) { return cb(err); } cb(); }); } function findUpload(data, cb) { Upload.findOne({hash: data.hash}, function (err, reply) { if (err) { return cb(err); } if (!reply) { return cb('upload not found with hash: '+ data.hash); } data.upload = reply; cb(); }); } function findUser(data, cb) { User.findById(data.userID, function (err, reply) { if (err || !reply) { return cb('user not found with id: '+ data.userID); } data.user = reply; cb(); }); } function fetchUpload(data, cb) { var upload = data.upload; if (upload.ocr_status.complete) { inspect('upload ocr status complete'); return cb(); } // start updating the upload in the background updateUpload(data, cb); } /** * Issue a fetch request the remote pdfer service for the latest information about this upload */ function updateUpload(data, cb) { var config = data.config var upload = data.upload; var username = config.get('pdfer:username'); var password = config.get('pdfer:password'); var host = config.get('pdfer:host'); var port = config.get('pdfer:port'); var url = 'http://'+username+':'+password+'@'+host + ':' + port +'/api/fetch/'+data.hash; var options = { url: url, method: 'get', json: true } request(options, function (err, response, body) { if (err) { return cb(err); } var statusCode = response.statusCode; if (statusCode !== 200) { var message = 'error updating upload status in fetch request, bad status code: ' + statusCode; inspect(body, message); return cb(message); } upload.status = body.status; if (body.parsed) { upload.ocr_status.complete = true; upload.ocr_status.running = true; upload.text_pages = body.text_pages; upload.num_page = body.text_pages.length; upload.status = 'ocr complete, waiting to parse' } upload.save(cb); }); } module.exports = function(data, callback) { var upload; var output; async.series([ function (cb) { keysExist(data, cb) }, function (cb) { findUpload(data, cb) }, function (cb) { findUser(data, cb) }, function(cb) { userInUpload(data, function (err, reply) { if (err) { return cb(err); } if (!reply) { return cb('you are not permitted to fetch this upload'); } cb(); }); }, function(cb) { fetchUpload(data, cb) } ], function (err) { if (err) { return callback(err); } return callback(null, data.upload); }); }