UNPKG

file-converter-nodejs

Version:

A powerful Node.js package for converting files between various formats, splitting and merging files, and automating complex file operations. Backed by LibreOffice and Python, this tool makes file processing seamless for developers.

87 lines (86 loc) 3.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleCleanup = exports.PythonProcessor = void 0; const child_process_1 = require("child_process"); const debug_1 = __importDefault(require("../utils/debug")); const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); var PythonProcessorHandlers; (function (PythonProcessorHandlers) { PythonProcessorHandlers["SPLIT_PDF"] = "split_pdf"; PythonProcessorHandlers["SPLIT_PPTX"] = "split_pptx"; PythonProcessorHandlers["MERGE_DOCX"] = "merge_docx"; PythonProcessorHandlers["MERGE_PPTX"] = "merge_pptx"; PythonProcessorHandlers["MERGE_PDF"] = "merge_pdf"; })(PythonProcessorHandlers || (PythonProcessorHandlers = {})); const wrapInQuotes = (str) => `"${str}"`; const PYTHON_SCRIPT_PATH = path_1.default.join(__dirname, '..', 'python', 'main.py'); class PythonProcessor { constructor(pythonPath) { this.pythonPath = pythonPath; this.invokePythonHandler = (handler, args) => { const command = `${this.pythonPath} ${PYTHON_SCRIPT_PATH} ${handler} ${args.join(' ')}`; (0, debug_1.default)(`Running python handler ${handler} with command: ${command}`); const output = (0, child_process_1.execSync)(command, { encoding: 'utf-8' }); (0, debug_1.default)(`Handler's output: ${output}`); return output.trim(); }; this.handleSplitPdf = (filePath, outdir) => { return JSON.parse(this.invokePythonHandler(PythonProcessorHandlers.SPLIT_PDF, [ wrapInQuotes(filePath), '--outputDir', wrapInQuotes(outdir), ])); }; this.handleSplitPptx = (filePath, outdir) => { return JSON.parse(this.invokePythonHandler(PythonProcessorHandlers.SPLIT_PPTX, [ wrapInQuotes(filePath), '--outputDir', wrapInQuotes(outdir), ])); }; this.handleMergePptx = (inputFiles, outputFile) => { return this.invokePythonHandler(PythonProcessorHandlers.MERGE_PPTX, [ '--inputFiles', inputFiles.map(wrapInQuotes).join(' '), '--outputFile', wrapInQuotes(outputFile), ]); }; this.handleMergePdf = (inputFiles, outputFile) => { return this.invokePythonHandler(PythonProcessorHandlers.MERGE_PDF, [ '--inputFiles', inputFiles.map(wrapInQuotes).join(' '), '--outputFile', wrapInQuotes(outputFile), ]); }; this.handleMergeDocx = (inputFiles, outputFile) => { return this.invokePythonHandler(PythonProcessorHandlers.MERGE_DOCX, [ '--inputFiles', inputFiles.map(wrapInQuotes).join(' '), '--outputFile', wrapInQuotes(outputFile), ]); }; this.pythonPath = pythonPath; } } exports.PythonProcessor = PythonProcessor; const handleCleanup = (processorOutput) => { if (!processorOutput) return; const allFiles = (Array.isArray(processorOutput) ? processorOutput : [processorOutput]).filter(Boolean); for (const filePath of allFiles) { try { (0, fs_1.unlinkSync)(filePath); } catch (error) { (0, debug_1.default)(`Error deleting file: ${filePath}`, error); } } }; exports.handleCleanup = handleCleanup;