File size: 2,223 Bytes
5fd404e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Ensure the require statement for pdf-parse is correct
const {PDFParse} = require('pdf-parse');
const mammoth = require('mammoth');
const fs = require('fs');
const path = require('path');
const os = require('os');

/**

 * Extracts text from a file buffer based on mimetype

 * @param {Buffer} buffer - The file data in memory

 * @param {String} mimetype - The type of file (pdf/docx)

 * @returns {Promise<String>} - The extracted text

 */
const extractText = async (buffer, mimetype) => {
    try {
        if (mimetype === 'application/pdf') {
            // Create a path in the system's temp directory
            const tempFilePath = path.join(os.tmpdir(), `upload_${Date.now()}.pdf`);

            // Write the buffer to the temp file
            fs.writeFileSync(tempFilePath, buffer);

            // Convert file path to a file:// URL
            const url = `file://${tempFilePath}`;
            const parser = new PDFParse({url});
            //const data = await parser.getText();
            const allImages = await parser.getScreenshot({ scale: 1.69 });; // Get images to trigger parsing and text extraction                 9
            fs.unlinkSync(tempFilePath); // Clean up temp file after processing

        
             const data = { init: true, pageImages:[]};

            for (const page of allImages.pages) {
                //const buffer = Buffer.from(page.dataUrl, 'base64');
                data.pageImages.push(page.dataUrl); // Store image data for each page
            }

           
            return data;
            
        } 
        else if (mimetype === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { // .docx
            const result = await mammoth.extractRawText({ buffer: buffer });
            return result.value;
        } 
        else if (mimetype === 'text/plain') {
            return buffer.toString('utf8');
        } 
        else {
            throw new Error('Unsupported file type');
        }
    } catch (error) {
        console.error("Text Extraction Error:", error);
        throw new Error('Failed to parse file content');
    }
};

module.exports = { extractText };