|
| 1 | +'use strict'; |
| 2 | +import log from "npmlog"; |
| 3 | +import { plugins, pre, createServer, Response } from "restify"; |
| 4 | +import { SvcContext } from './svccontext'; |
| 5 | +import { SvcResponse } from './svcresponse'; |
| 6 | +import PDFParser, { PDFDataReady, PDFFormImage, PDFParserError } from 'pdf2json'; |
| 7 | + |
| 8 | + |
| 9 | +export class PDFFORMService { |
| 10 | + // private static |
| 11 | + private static _nextId = 1; |
| 12 | + private static _name = 'PDFFORMServer'; |
| 13 | + private static _pdfPathBase = ""; |
| 14 | + |
| 15 | + get_id: () => number; |
| 16 | + get_name: () => string; |
| 17 | + get_version: () => string; |
| 18 | + |
| 19 | + // constructor |
| 20 | + constructor() { |
| 21 | + // private, only accessible within this constructor |
| 22 | + let _id = PDFFORMService._nextId++; |
| 23 | + let _name = PDFFORMService._name; |
| 24 | + const _version = "0.0.1"; |
| 25 | + |
| 26 | + // public (every instance will have their own copy of these methods, needs to be lightweight) |
| 27 | + this.get_id = function() { return _id; }; |
| 28 | + this.get_name = function() { return _name + _id; }; |
| 29 | + this.get_version = function() {return _version; }; |
| 30 | + }; |
| 31 | + |
| 32 | + // public static |
| 33 | + public static get_nextId() { |
| 34 | + return this._name + this._nextId; |
| 35 | + }; |
| 36 | + |
| 37 | + //private |
| 38 | + private _onPFBinDataReady(context: SvcContext , evtData: PDFFormImage) { |
| 39 | + log.info(this.get_name(), " completed response."); |
| 40 | + var resData = new SvcResponse(200, "OK", "data", evtData); |
| 41 | + context.completeResponse(resData); |
| 42 | + resData.destroy(); |
| 43 | + evtData = null; |
| 44 | + return context.next(); |
| 45 | + }; |
| 46 | + |
| 47 | + private _onPFBinDataError(context: SvcContext, error: PDFParserError) { |
| 48 | + log.info(this.get_name() + " 500 Error: ", JSON.stringify(error)); |
| 49 | + var resData = new SvcResponse(500, JSON.stringify(error), undefined, undefined) |
| 50 | + context.completeResponse(resData); |
| 51 | + resData.destroy(); |
| 52 | + resData = null; |
| 53 | + return context.next(); |
| 54 | + }; |
| 55 | + |
| 56 | + _customizeHeaders(res: Response) { |
| 57 | + // Resitify currently has a bug which doesn't allow you to set default headers |
| 58 | + // This headers comply with CORS and allow us to server our response to any origin |
| 59 | + res.header("Access-Control-Allow-Origin", "*"); |
| 60 | + res.header("Access-Control-Allow-Headers", "X-Requested-With"); |
| 61 | + res.header("Cache-Control", "no-cache, must-revalidate"); |
| 62 | + }; |
| 63 | + |
| 64 | + // public (every instance will share the same method, but has no access to private fields defined in constructor) |
| 65 | + start() { |
| 66 | + //private function within this public method |
| 67 | + const version = this.get_version(); |
| 68 | + const _gfilter = (svcContext: SvcContext) => { |
| 69 | + var req = svcContext.request; |
| 70 | + var folderName = req.params.folderName; |
| 71 | + var pdfId = req.params.pdfId; |
| 72 | + log.info(this.get_name(), " received request:" + req.method + ":" + folderName + "/" + pdfId); |
| 73 | + |
| 74 | + var pdfParser = new PDFParser(); |
| 75 | + |
| 76 | + this._customizeHeaders(svcContext.response); |
| 77 | + |
| 78 | + pdfParser.on("pdfParser_dataReady", (data: PDFFormImage) => this._onPFBinDataReady(svcContext, data)); |
| 79 | + pdfParser.on("pdfParser_dataError", (error: PDFParserError) => this._onPFBinDataError(svcContext, error)); |
| 80 | + |
| 81 | + pdfParser.loadPDF(PDFFORMService._pdfPathBase + folderName + "/" + pdfId + ".pdf", 0); |
| 82 | + }; |
| 83 | + |
| 84 | + const server = createServer({ |
| 85 | + name: this.get_name(), |
| 86 | + version: this.get_version() |
| 87 | + }); |
| 88 | + |
| 89 | + server.use(plugins.acceptParser(server.acceptable)); |
| 90 | + server.use(plugins.authorizationParser()); |
| 91 | + server.use(plugins.dateParser()); |
| 92 | + server.use(plugins.queryParser()); |
| 93 | + server.use(plugins.bodyParser()); |
| 94 | + server.use(plugins.jsonp()); |
| 95 | + server.use(plugins.gzipResponse()); |
| 96 | + server.pre(pre.userAgentConnection()); |
| 97 | + |
| 98 | + server.get('/p2jsvc/:folderName/:pdfId', function(req, res, next) { |
| 99 | + _gfilter(new SvcContext(req, res, next)); |
| 100 | + }); |
| 101 | + |
| 102 | + server.post('/p2jsvc', function(req, res, next) { |
| 103 | + _gfilter(new SvcContext(req, res, next)); |
| 104 | + }); |
| 105 | + |
| 106 | + server.get('/p2jsvc/status', function(req, res, next) { |
| 107 | + var jsObj = new SvcResponse(200, "OK", server.name, version); |
| 108 | + res.send(200, jsObj); |
| 109 | + return next(); |
| 110 | + }); |
| 111 | + |
| 112 | + server.listen(7799, function() { |
| 113 | + log.info(server.name, ' listening at ' + server.url); |
| 114 | + }); |
| 115 | + }; |
| 116 | +}; |
| 117 | + |
| 118 | + |
| 119 | + |
0 commit comments