pvanand commited on
Commit
c2f0090
·
verified ·
1 Parent(s): a2a4525

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +11 -65
index.js CHANGED
@@ -8,16 +8,6 @@ app.use(express.json());
8
 
9
  const tmpDir = '/tmp/marp-work';
10
 
11
- // Create tmp directory if it doesn't exist
12
- const initializeTmpDir = async () => {
13
- try {
14
- await fs.mkdir(tmpDir, { recursive: true });
15
- } catch (error) {
16
- console.error('Error creating tmp directory:', error);
17
- throw error;
18
- }
19
- };
20
-
21
  app.post('/convert', async (req, res) => {
22
  const { markdown, outputFormat, options = [] } = req.body;
23
 
@@ -25,18 +15,10 @@ app.post('/convert', async (req, res) => {
25
  return res.status(400).json({ error: 'Missing markdown or outputFormat' });
26
  }
27
 
28
- // Validate output format
29
- const allowedFormats = ['pdf', 'pptx', 'html'];
30
- if (!allowedFormats.includes(outputFormat)) {
31
- return res.status(400).json({ error: `Invalid output format. Allowed formats are: ${allowedFormats.join(', ')}` });
32
- }
33
-
34
  try {
35
- await initializeTmpDir();
36
-
37
  const inputFile = path.join(tmpDir, `input_${Date.now()}.md`);
38
  const outputFile = path.join(tmpDir, `output_${Date.now()}.${outputFormat}`);
39
-
40
  await fs.writeFile(inputFile, markdown);
41
 
42
  const cliOptions = [
@@ -45,75 +27,39 @@ app.post('/convert', async (req, res) => {
45
  outputFile,
46
  `--${outputFormat}`,
47
  '--html',
48
- '--allow-local-files',
49
  ...options
50
  ];
51
 
52
  if (outputFormat === 'pdf') {
53
  cliOptions.push(
 
54
  '--pdf-notes',
55
  '--pdf-delay', '3000'
56
  );
57
  } else if (outputFormat === 'pptx') {
58
  cliOptions.push(
 
 
59
  '--pptx-notes',
60
- '--pptx-delay', '3000',
61
- '--pptx-editable' // Enable editable PPTX
62
  );
63
  }
64
 
65
- console.log('Converting with options:', cliOptions);
66
-
67
  await marpCli(cliOptions);
68
  const output = await fs.readFile(outputFile);
69
 
70
- // Clean up temp files
71
- const cleanup = async () => {
72
- try {
73
- await fs.unlink(inputFile);
74
- await fs.unlink(outputFile);
75
- } catch (error) {
76
- console.error('Cleanup error:', error);
77
- }
78
- };
79
-
80
- cleanup(); // Non-blocking cleanup
81
 
82
  res.contentType(outputFormat);
83
  res.send(output);
84
-
85
  } catch (error) {
86
  console.error('Conversion error:', error);
87
-
88
- // More specific error messages based on error type
89
- let errorMessage = 'Conversion failed';
90
- if (error.code === 'ENOENT') {
91
- errorMessage = 'Required file or directory not found';
92
- } else if (error.code === 'EACCES') {
93
- errorMessage = 'Permission denied when accessing files';
94
- }
95
-
96
- res.status(500).json({
97
- error: errorMessage,
98
- details: error.message,
99
- code: error.code
100
- });
101
  }
102
  });
103
 
104
- // Health check endpoint
105
- app.get('/health', (req, res) => {
106
- res.json({ status: 'ok' });
107
- });
108
-
109
- // Initialize tmp directory when server starts
110
- initializeTmpDir().then(() => {
111
- const port = process.env.PORT || 7860;
112
- app.listen(port, () => {
113
- console.log(`Marp API server listening on port ${port}`);
114
- console.log(`Temporary directory: ${tmpDir}`);
115
- });
116
- }).catch(error => {
117
- console.error('Failed to initialize server:', error);
118
- process.exit(1);
119
  });
 
8
 
9
  const tmpDir = '/tmp/marp-work';
10
 
 
 
 
 
 
 
 
 
 
 
11
  app.post('/convert', async (req, res) => {
12
  const { markdown, outputFormat, options = [] } = req.body;
13
 
 
15
  return res.status(400).json({ error: 'Missing markdown or outputFormat' });
16
  }
17
 
 
 
 
 
 
 
18
  try {
 
 
19
  const inputFile = path.join(tmpDir, `input_${Date.now()}.md`);
20
  const outputFile = path.join(tmpDir, `output_${Date.now()}.${outputFormat}`);
21
+
22
  await fs.writeFile(inputFile, markdown);
23
 
24
  const cliOptions = [
 
27
  outputFile,
28
  `--${outputFormat}`,
29
  '--html',
 
30
  ...options
31
  ];
32
 
33
  if (outputFormat === 'pdf') {
34
  cliOptions.push(
35
+ '--allow-local-files',
36
  '--pdf-notes',
37
  '--pdf-delay', '3000'
38
  );
39
  } else if (outputFormat === 'pptx') {
40
  cliOptions.push(
41
+ '--pptx-editable',
42
+ '--allow-local-files',
43
  '--pptx-notes',
44
+ '--pptx-delay', '3000'
 
45
  );
46
  }
47
 
 
 
48
  await marpCli(cliOptions);
49
  const output = await fs.readFile(outputFile);
50
 
51
+ await fs.unlink(inputFile);
52
+ await fs.unlink(outputFile);
 
 
 
 
 
 
 
 
 
53
 
54
  res.contentType(outputFormat);
55
  res.send(output);
 
56
  } catch (error) {
57
  console.error('Conversion error:', error);
58
+ res.status(500).json({ error: 'Conversion failed', details: error.message });
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  }
60
  });
61
 
62
+ const port = process.env.PORT || 7860;
63
+ app.listen(port, () => {
64
+ console.log(`Marp API server listening on port ${port}`);
 
 
 
 
 
 
 
 
 
 
 
 
65
  });