-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiroutes.js
83 lines (67 loc) · 2.27 KB
/
apiroutes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require('express');
const path = require('path');
const winston = require('winston');
const Querystring = require('query-string');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const axios = require("axios");
const mammoth = require('mammoth');
const appconstants = require('./constants.js');
/**
* Log level
*/
winston.level = process.env.LOG_LEVEL || 'debug' ;
var router = express.Router();
router.get('/docxTohtml', (req, res, next) => {
let url = req.query.docLink;
axios.get(url,{responseType: 'arraybuffer'}).then(response => {
mammoth.convertToHtml(response.data)
.then(function(result){
res.status(200).send(result.value);
}).done();
}).catch(error => {
console.log(error);
res.send(error);
});
});
router.get('/xmlToHtml',(req, res, next) => {
let attachmentUrl = req.query.docLink;
let xmlType = "sample"; // Will be later inferred from XML structure
let configJSONPath = path.join(
appconstants.CONFIG_FOLDER,
appconstants.XML_TYPES_JSON
);
//Get XSLT config file location
let xsltConfig = '';
fs.readFile(configJSONPath, 'utf8', function (err, data) {
xsltConfig = JSON.parse(data);
//Get appropiate XSLT for xmlType
fs.readFile(xsltConfig[xmlType].path, 'utf8', function (err, data) {
xslt = data;
// Get attachment XML that has to be converted to HTML
axios.get(attachmentUrl).then( response => {
let xml = response.data;
// Send XML for conversion to service
axios({
method: 'post',
url: appconstants.XML_HTML_CONVERTER,
data: Querystring.stringify({
"input_file" : xml,
"input_xslt" : xslt,
"input_params" : ''
}),
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
}).then(response => {
res.status(200).send(response.data);
}).catch(error => {
console.error("Error in xml to html conversion using provided XSLT " + error);
});
}).catch( error => {
console.error("Error in getting XML to be converted " + error);
});
});
});
});
module.exports = router;