File size: 2,445 Bytes
bbfde3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const Busboy = require('busboy');
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');

let analyzePowerPoint;
try {
  const pptxAnalyzer = require('../lib/pptx-analyzer');
  analyzePowerPoint = pptxAnalyzer.analyzePowerPoint;
} catch (err) {
  console.error('Failed to load pptx-analyzer:', err);
}

// Helper function to send JSON with proper headers
function sendJson(res, status, data) {
  res.setHeader('Content-Type', 'application/json');
  res.status(status).end(JSON.stringify(data));
}

module.exports = async (req, res) => {
  if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
    return;
  }
  applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });

  if (req.method !== 'POST') {
    sendJson(res, 405, { error: 'Method not allowed' });
    return;
  }

  try {
    const busboy = Busboy({ headers: req.headers });
    let fileData = null;
    let filename = null;

    busboy.on('file', (fieldname, file, info) => {
      filename = info.filename;
      const chunks = [];
      
      file.on('data', (chunk) => {
        chunks.push(chunk);
      });
      
      file.on('end', () => {
        fileData = Buffer.concat(chunks);
      });
    });

    busboy.on('finish', async () => {
      if (!fileData || !filename) {
        sendJson(res, 400, { error: 'No file uploaded' });
        return;
      }

      // Validate PowerPoint file types
      const validExtensions = ['.pptx', '.ppt', '.pps', '.potx'];
      const isValid = validExtensions.some(ext => filename.toLowerCase().endsWith(ext));
      
      if (!isValid) {
        sendJson(res, 400, { error: 'Please upload a PowerPoint file (.pptx, .ppt, .pps, or .potx)' });
        return;
      }

      try {
        if (!analyzePowerPoint) {
          throw new Error('PowerPoint analyzer not available');
        }
        const report = await analyzePowerPoint(fileData, filename);
        sendJson(res, 200, {
          fileName: filename,
          suggestedFileName: filename,
          report: report
        });
      } catch (error) {
        console.error('PowerPoint analysis error:', error);
        sendJson(res, 500, { error: error.message });
      }
    });

    req.pipe(busboy);

  } catch (error) {
    console.error('Upload error:', error);
    sendJson(res, 500, { error: error.message });
  }
};