-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (89 loc) · 2.91 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
const express = require('express');
const fs = require('fs');
const http = require('http');
const bodyParser = require('body-parser');
const atob = require('atob');
const mammoth = require("mammoth");
const app = express();
const fileName = 'tranFile.docx'
const fileNameHtml = 'tranFile.html';
var bytesDataArr = [];
app.get('/GetDoc_App.html', async (req, res) => {
try {
await fs.readFile('static/GetDoc_App.html', 'utf-8', function(err, result){
if(err){
return res.status(500).json({Error: err});
}
return res.header('content-type', 'text/html').end(result);
});
}
catch(err) {
console.error(`error rendering GetDoc_App.xml file: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
app.get('/GetDoc_App.js', async (req, res) => {
try {
await fs.readFile('static/GetDoc_App.js', 'utf-8', function(err, result){
if(err){
return res.status(500).json({Error: err});
}
return res.header('content-type', 'text/html').end(result);
});
}
catch(err) {
console.error(`error rendering GetDoc_App.xml file: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
app.use(bodyParser.json());
function b64DecodeUnicode(str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
app.post('/submit', function (req,res) {
if(!req.body){
return res.end(`body is empty`);
}
var sliceNumber = req.headers["slice-number"];
if(req.headers["is-first"]==="true"){
bytesDataArr = [];
}
bytesDataArr[sliceNumber] = b64DecodeUnicode(req.body.data);
if(req.headers["is-last"]==="true"){
var allBytesArray = (bytesDataArr.join(",")).split(",");
fs.writeFile(fileName, new Buffer(allBytesArray), (err) => {
if(err) {
console.error(err.message);
}
convertToHtml(fileName);
});
return res.end(`finish`);
}
return res.end(`ok`);
});
function convertToHtml(docxFile){
mammoth.convertToHtml({path: fileName})
.then(function(result){
var html = result.value; // The generated HTML
var messages = result.messages; // Any messages, such as warnings during conversion
fs.writeFile(fileNameHtml, html, (err) => {
if(err) console.error(err);
})
})
.done();
}
app.get('/', async (req,res) => {
return res.end(`server is on`);
});
http.createServer(app).listen(8080, err => {
if (err) return console.error(err);
console.info(`server is listening on port 8080`);
});
process.on('uncaughtException', err => {
console.error(`uncaught exception: ${err.message}`);
setTimeout(() => process.exit(1), 1000);
});