-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
445 lines (383 loc) · 14.5 KB
/
Copy pathserver.js
File metadata and controls
445 lines (383 loc) · 14.5 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const express = require('express');
const { exec, spawn } = require('child_process');
const { promisify } = require('util');
const fs = require('fs').promises;
const path = require('path');
const os = require('os');
const fetch = require('node-fetch');
const app = express();
app.use(express.json());
const execAsync = promisify(exec);
// Request queue to prevent concurrent conversions
class ConversionQueue {
constructor() {
this.queue = [];
this.processing = false;
}
async enqueue(docxUrl, callback) {
return new Promise((resolve, reject) => {
this.queue.push({ docxUrl, callback, resolve, reject });
this.processQueue();
});
}
async processQueue() {
if (this.processing || this.queue.length === 0) return;
this.processing = true;
const request = this.queue.shift();
try {
const result = await request.callback();
request.resolve(result);
} catch (error) {
request.reject(error);
} finally {
// Small delay between conversions to prevent resource conflicts
await new Promise(resolve => setTimeout(resolve, 1000));
this.processing = false;
this.processQueue();
}
}
}
const conversionQueue = new ConversionQueue();
// Convert DOCX to PDF using LibreOffice with timeout and process monitoring
async function convertDocxToPdf(docxBuffer, outputDir, timeoutMs = 60000) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
const inputFile = path.join(os.tmpdir(), `input_${Date.now()}_${Math.random().toString(36).substr(2, 9)}.docx`);
// Write input file
fs.writeFile(inputFile, docxBuffer)
.then(() => {
console.log(`📝 Input file written: ${inputFile} (${docxBuffer.length} bytes)`);
// Spawn LibreOffice process
const child = spawn('libreoffice', [
'--headless',
'--convert-to', 'pdf',
'--outdir', outputDir,
inputFile
], {
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
});
let stdout = '';
let stderr = '';
let timeoutId = null;
let processKilled = false;
// Capture stdout
child.stdout.on('data', (data) => {
stdout += data.toString();
console.log(`📄 LibreOffice stdout: ${data.toString().trim()}`);
});
// Capture stderr
child.stderr.on('data', (data) => {
stderr += data.toString();
console.log(`⚠️ LibreOffice stderr: ${data.toString().trim()}`);
});
// Set timeout to kill process
timeoutId = setTimeout(() => {
if (!child.killed && child.exitCode === null) {
processKilled = true;
console.error(`⏱️ Conversion timeout after ${timeoutMs}ms, killing process...`);
child.kill('SIGTERM');
// Force kill after 5 seconds if still running
setTimeout(() => {
if (child.exitCode === null) {
console.error('💀 Force killing LibreOffice process...');
child.kill('SIGKILL');
}
}, 5000);
}
}, timeoutMs);
// Handle process completion
child.on('close', (code, signal) => {
const duration = Date.now() - startTime;
clearTimeout(timeoutId);
// Cleanup input file
fs.unlink(inputFile).catch(() => {
console.warn('⚠️ Failed to cleanup input file');
});
console.log(`⏱️ Conversion completed in ${duration}ms (code: ${code}, signal: ${signal})`);
if (processKilled) {
reject(new Error(`LibreOffice conversion timeout after ${timeoutMs}ms. Duration: ${duration}ms. Stdout: ${stdout || 'none'}. Stderr: ${stderr || 'none'}`));
return;
}
if (code !== 0) {
console.error(`❌ LibreOffice failed with code ${code}`);
console.error(`📄 Stdout: ${stdout || '(empty)'}`);
console.error(`⚠️ Stderr: ${stderr || '(empty)'}`);
reject(new Error(`LibreOffice conversion failed: code ${code}, duration: ${duration}ms, stderr: ${stderr || 'none'}, stdout: ${stdout || 'none'}`));
return;
}
// Find generated PDF
fs.readdir(outputDir)
.then(files => {
const pdfFiles = files.filter(f => f.endsWith('.pdf'));
if (pdfFiles.length === 0) {
reject(new Error(`PDF file not generated. Output directory: ${outputDir}, files: ${files.join(', ')}`));
return;
}
const pdfFile = path.join(outputDir, pdfFiles[0]);
fs.stat(pdfFile)
.then(stat => {
console.log(`✅ PDF generated: ${pdfFile} (${stat.size} bytes)`);
resolve(pdfFile);
})
.catch(reject);
})
.catch(reject);
});
// Handle spawn errors
child.on('error', (error) => {
clearTimeout(timeoutId);
console.error('❌ LibreOffice spawn error:', error);
// Cleanup input file
fs.unlink(inputFile).catch(() => {
console.warn('⚠️ Failed to cleanup input file');
});
reject(error);
});
})
.catch(reject);
});
}
// Wrapper function for backward compatibility (downloads DOCX and converts)
async function convertDocxToPdfFromUrl(docxUrl, orderId) {
// Check if this is a test/health-check URL
if (docxUrl === 'test' || docxUrl === 'health-check' || !docxUrl.startsWith('http')) {
throw new Error('Invalid DOCX URL: Only absolute URLs are supported');
}
const tempDir = os.tmpdir();
const timestamp = Date.now();
const outputDir = path.join(tempDir, `output_${orderId}_${timestamp}`);
try {
// Download DOCX file
console.log(`📥 Downloading DOCX from: ${docxUrl}`);
const response = await fetch(docxUrl);
if (!response.ok) {
throw new Error(`Failed to download DOCX: ${response.status} ${response.statusText}`);
}
const buffer = await response.buffer();
console.log(`✅ DOCX downloaded: ${buffer.length} bytes`);
// Create output directory
await fs.mkdir(outputDir, { recursive: true });
// Convert using LibreOffice with timeout
console.log(`🔄 Running LibreOffice conversion...`);
const pdfPath = await convertDocxToPdf(buffer, outputDir, 60000);
// Read PDF
const pdfBuffer = await fs.readFile(pdfPath);
console.log(`✅ PDF generated: ${pdfBuffer.length} bytes`);
// Cleanup
try {
await fs.unlink(pdfPath);
await fs.rm(outputDir, { recursive: true, force: true });
console.log(`🧹 Cleaned up temporary files`);
} catch (cleanupError) {
console.warn(`⚠️ Cleanup warning: ${cleanupError.message}`);
}
return pdfBuffer;
} catch (error) {
// Cleanup on error
try {
await fs.rm(outputDir, { recursive: true, force: true }).catch(() => {});
} catch (cleanupError) {
console.warn(`⚠️ Cleanup error: ${cleanupError.message}`);
}
throw error;
}
}
// Keep-alive endpoint to prevent auto-stop
app.get('/keepalive', (req, res) => {
res.json({
status: 'ok',
service: 'word-pdf-service',
timestamp: new Date().toISOString()
});
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'ok',
service: 'word-pdf-service',
timestamp: new Date().toISOString()
});
});
// POST /api/convert - Receive conversion request
app.post('/api/convert', async (req, res) => {
try {
const { docxUrl, orderId, callbackUrl } = req.body;
if (!docxUrl || !orderId || !callbackUrl) {
return res.status(400).json({
success: false,
error: 'Missing required fields: docxUrl, orderId, callbackUrl'
});
}
console.log(`\n🔄 New conversion request received:`);
console.log(` Order ID: ${orderId}`);
console.log(` DOCX URL: ${docxUrl}`);
console.log(` Callback URL: ${callbackUrl}`);
// Generate job ID
const jobId = `job_${orderId}_${Date.now()}`;
// Start conversion asynchronously (don't wait)
convertDocxToPdfFromUrl(docxUrl, orderId)
.then(async (pdfBuffer) => {
// Send PDF to webhook
const base64Pdf = pdfBuffer.toString('base64');
console.log(`\n✅ Conversion completed for order ${orderId}`);
console.log(` PDF size: ${pdfBuffer.length} bytes`);
console.log(` Sending to webhook: ${callbackUrl}`);
try {
const webhookResponse = await fetch(callbackUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-render-webhook-secret': process.env.RENDER_WEBHOOK_SECRET || ''
},
body: JSON.stringify({
orderId,
jobId,
pdfBuffer: base64Pdf,
status: 'completed'
})
});
if (webhookResponse.ok) {
console.log(`✅ Webhook sent successfully for order: ${orderId}`);
} else {
const errorText = await webhookResponse.text();
console.error(`❌ Webhook failed: ${webhookResponse.status} ${errorText}`);
}
} catch (webhookError) {
console.error(`❌ Error sending webhook:`, webhookError);
}
})
.catch(async (error) => {
console.error(`\n❌ Conversion failed for order ${orderId}:`);
console.error(` Error: ${error.message}`);
console.error(` Stack: ${error.stack}`);
// Send failure notification
try {
await fetch(callbackUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-render-webhook-secret': process.env.RENDER_WEBHOOK_SECRET || ''
},
body: JSON.stringify({
orderId,
jobId,
status: 'failed',
error: error.message
})
});
console.log(`✅ Failure webhook sent for order: ${orderId}`);
} catch (webhookError) {
console.error(`❌ Failed to send failure webhook:`, webhookError);
}
});
// Return immediately with job ID
res.json({
success: true,
jobId,
message: 'Conversion job started'
});
} catch (error) {
console.error('❌ Error starting conversion:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});
// POST /api/convert-sync - Synchronous conversion (for real-time downloads)
app.post('/api/convert-sync', async (req, res) => {
try {
const { docxUrl } = req.body;
if (!docxUrl) {
return res.status(400).json({
success: false,
error: 'Missing required field: docxUrl'
});
}
console.log(`\n🔄 Synchronous conversion request received:`);
console.log(` DOCX URL: ${docxUrl}`);
// Queue the conversion
const result = await new Promise((resolve, reject) => {
conversionQueue.enqueue(docxUrl, async () => {
try {
// Download DOCX
console.log(`📥 Downloading DOCX from: ${docxUrl}`);
const docxResponse = await fetch(docxUrl);
if (!docxResponse.ok) {
throw new Error(`Failed to download DOCX: ${docxResponse.status}`);
}
const docxBuffer = await docxResponse.buffer();
console.log(`✅ DOCX downloaded: ${docxBuffer.length} bytes`);
// Create unique output directory
const outputDir = path.join(os.tmpdir(), `output_sync_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`);
await fs.mkdir(outputDir, { recursive: true });
// Convert to PDF with timeout
console.log(`🔄 Running LibreOffice conversion...`);
const pdfPath = await convertDocxToPdf(docxBuffer, outputDir, 60000); // 60 second timeout
// Read PDF
const pdfBuffer = await fs.readFile(pdfPath);
console.log(`✅ PDF generated: ${pdfBuffer.length} bytes`);
// Cleanup
try {
await fs.unlink(pdfPath);
await fs.rm(outputDir, { recursive: true, force: true });
console.log(`🧹 Cleaned up temporary files`);
} catch (e) {
console.warn('⚠️ Failed to cleanup:', e);
}
return {
success: true,
pdfBuffer: pdfBuffer.toString('base64'),
size: pdfBuffer.length
};
} catch (error) {
console.error(`❌ Error in synchronous conversion:`, error);
throw error;
}
}).then(resolve).catch(reject);
});
res.json(result);
} catch (error) {
console.error(`❌ Conversion endpoint error:`, error);
res.status(500).json({
success: false,
error: error.message || 'Conversion failed'
});
}
});
// GET /api/status/:jobId - Check conversion status (optional)
app.get('/api/status/:jobId', (req, res) => {
// This is a placeholder - implement job tracking if needed
res.json({
success: true,
status: 'processing',
message: 'Status checking not implemented. Use webhook for completion notification.'
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('❌ Unhandled error:', err);
res.status(500).json({
success: false,
error: 'Internal server error'
});
});
const PORT = process.env.PORT || 10000;
app.listen(PORT, () => {
console.log(`\n🚀 Word PDF Service started`);
console.log(` Port: ${PORT}`);
console.log(` Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(` LibreOffice: Checking...`);
// Verify LibreOffice is installed (non-blocking)
exec('libreoffice --version', { timeout: 5000 }, (error, stdout, stderr) => {
if (error) {
console.error(` ⚠️ LibreOffice not found: ${error.message}`);
} else {
console.log(` ✅ LibreOffice: ${stdout.trim()}`);
console.log(` ✅ Service ready - LibreOffice will initialize on first conversion`);
// Note: Pre-warming removed to avoid hanging. With min_machines_running=1,
// the service stays running and LibreOffice will be ready for first request.
}
});
});