-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (67 loc) · 2.17 KB
/
app.js
File metadata and controls
83 lines (67 loc) · 2.17 KB
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 { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
const app = express();
const rawParser = express.raw({ type: () => true, limit: '50mb' });
const API_TOKEN = process.env.DOCUMENT_PARSER_API_TOKEN;
function authenticateToken(req, res, next) {
const token = req.headers['authorization']?.replace('Bearer ', '') || req.query.token;
if (!API_TOKEN) {
return res.status(500).json({ error: 'Server misconfigured: API_TOKEN not set' });
}
if (!token || token !== API_TOKEN) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
}
app.post('/convert', authenticateToken, rawParser, (req, res) => {
if (!req.body || !req.body.length) {
return res.status(400).json({ error: 'No file uploaded' });
}
const mime = req.headers['content-type'] || '';
let ext = '';
if (mime === 'application/pdf') {
ext = '.pdf';
} else if (mime === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
ext = '.docx';
} else {
return res.status(400).json({ error: 'Unsupported file type. Only PDF and DOCX are supported.' });
}
const tmpPath = path.join(
os.tmpdir(),
`upload-${Date.now()}-${Math.random().toString(16).slice(2)}${ext}`
);
fs.writeFileSync(tmpPath, req.body);
try {
let result;
switch (ext) {
case '.pdf':
result = convertPDF(tmpPath);
break;
case '.docx':
result = convertDocx(tmpPath);
break;
}
res.json({ content: result });
} catch (err) {
res.status(500).json({ error: err.message });
} finally {
fs.unlinkSync(tmpPath);
}
});
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
function convertPDF(filePath) {
return execSync(`python3 convert_pdf.py "${filePath}"`, { encoding: 'utf-8' });
}
function convertDocx(filePath) {
const pandocPath = process.env.PANDOC_PATH || '/opt/pandoc-3.8.2.1/bin/pandoc';
return execSync(`"${pandocPath}" -f docx -t gfm "${filePath}"`, { encoding: 'utf-8' });
}
const port = 5004;
app.listen(port, () => {
console.log(`Server starting on port ${port}`);
});