File size: 1,859 Bytes
ce37a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs-extra');
const path = require('path');
const PDFDocument = require('pdfkit');
const { Document, Packer, Paragraph, TextRun } = require('docx');

const TEMP_DIR = path.join(__dirname, '../temp_files');
fs.ensureDirSync(TEMP_DIR);

module.exports = {
    execute: async ({ filename, content, format }) => {
        const filePath = path.join(TEMP_DIR, filename);
        
        try {
            if (format === 'pdf' || filename.endsWith('.pdf')) {
                const doc = new PDFDocument();
                const stream = fs.createWriteStream(filePath);
                doc.pipe(stream);
                doc.fontSize(12).text(content, 100, 100);
                doc.end();
                
                await new Promise((resolve) => stream.on('finish', resolve));
            } else if (format === 'docx' || filename.endsWith('.docx')) {
                const doc = new Document({
                    sections: [{
                        properties: {},
                        children: content.split('\n').map(line => 
                            new Paragraph({
                                children: [new TextRun(line)],
                            })
                        ),
                    }],
                });
                
                const buffer = await Packer.toBuffer(doc);
                fs.writeFileSync(filePath, buffer);
            } else {
                // txt, code (py, js, html)
                fs.writeFileSync(filePath, content);
            }

            return { 
                success: true, 
                filePath: filePath, 
                message: `File ${filename} created successfully.` 
            };
        } catch (error) {
            console.error('File Generation Error:', error);
            return { error: 'Failed to generate file.' };
        }
    }
};