refactor: complete PdfToImageConverter.ts modularization
Extract complex PDF-to-image conversion logic into specialized modules: - BinaryLocator.ts: pdf2img binary discovery and path resolution - EnvironmentUtils.ts: PATH setup and environment management - PdfCliExecutor.ts: CLI command execution and error handling - FileDiscovery.ts: output file discovery and pattern matching Reduces PdfToImageConverter.ts from 308 to 193 lines (37% reduction) while preserving all functionality and error handling. Improves maintainability by separating concerns: - Binary location strategy isolated from conversion logic - Environment setup centralized and reusable - CLI execution abstracted with proper error handling - File discovery logic handles pdf2img filename variations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
5 files changed
+537
-136
No files matched your search
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* PDF CLI Executor for handling pdf2img command line operations.
|
||||
* Executes external pdf2img binary to convert PDF pages to images.
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
import { BinaryLocator } from './BinaryLocator';
|
||||
import { EnvironmentUtils } from './EnvironmentUtils';
|
||||
|
||||
export interface PdfCliOptions {
|
||||
/** Scale factor for the rendered image */
|
||||
scale: number;
|
||||
/** Page number to convert (1-based) */
|
||||
pages: string;
|
||||
/** Output directory for generated images */
|
||||
outputDir: string;
|
||||
}
|
||||
|
||||
export interface PdfCliResult {
|
||||
/** Whether the CLI execution succeeded */
|
||||
success: boolean;
|
||||
/** Standard output from the command */
|
||||
stdout?: string;
|
||||
/** Standard error from the command */
|
||||
stderr?: string;
|
||||
/** Error message if execution failed */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export class PdfCliExecutor {
|
||||
/**
|
||||
* Execute pdf2img CLI command to convert PDF to images.
|
||||
* @param pdfPath Path to the input PDF file
|
||||
* @param options CLI execution options
|
||||
* @param plugin Plugin instance for accessing settings
|
||||
* @returns CLI execution result
|
||||
*/
|
||||
public static async executePdf2Img(
|
||||
pdfPath: string,
|
||||
options: PdfCliOptions,
|
||||
plugin?: any
|
||||
): Promise<PdfCliResult> {
|
||||
try {
|
||||
// Find the pdf2img binary
|
||||
const binaryLocation = await BinaryLocator.findPdf2ImgBinary(plugin);
|
||||
if (!binaryLocation.exists) {
|
||||
return {
|
||||
success: false,
|
||||
error: binaryLocation.error
|
||||
};
|
||||
}
|
||||
|
||||
// Calculate relative path from vault to temp directory for pdf2img
|
||||
// pdf2img doesn't handle absolute paths correctly, needs relative paths
|
||||
const vaultPath = process.cwd(); // This should be the vault directory
|
||||
const relativeOutputDir = path.relative(vaultPath, options.outputDir);
|
||||
|
||||
// Build the CLI command
|
||||
const cliCommand = this.buildCommand(
|
||||
binaryLocation.binaryPath,
|
||||
pdfPath,
|
||||
relativeOutputDir,
|
||||
options
|
||||
);
|
||||
|
||||
// Execute the command
|
||||
return await this.executeCommand(cliCommand, plugin);
|
||||
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: `PDF CLI execution failed: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the pdf2img CLI command string.
|
||||
* @param binaryPath Full path to pdf2img binary
|
||||
* @param pdfPath Path to input PDF file
|
||||
* @param outputDir Output directory (relative path)
|
||||
* @param options CLI options
|
||||
* @returns Command string ready for execution
|
||||
*/
|
||||
private static buildCommand(
|
||||
binaryPath: string,
|
||||
pdfPath: string,
|
||||
outputDir: string,
|
||||
options: PdfCliOptions
|
||||
): string {
|
||||
// Build the CLI command - the pdf2img binary is already executable
|
||||
return [
|
||||
`"${binaryPath}"`,
|
||||
`"${pdfPath}"`,
|
||||
'--scale', options.scale.toString(),
|
||||
'--output', `"${outputDir}"`,
|
||||
'--pages', options.pages // Only convert the specified page(s)
|
||||
].join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a CLI command with proper environment setup.
|
||||
* @param command Command string to execute
|
||||
* @param plugin Plugin instance for accessing settings
|
||||
* @returns Command execution result
|
||||
*/
|
||||
private static async executeCommand(
|
||||
command: string,
|
||||
plugin?: any
|
||||
): Promise<PdfCliResult> {
|
||||
try {
|
||||
const { exec } = require('child_process');
|
||||
const { promisify } = require('util');
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
// Get augmented environment for CLI execution
|
||||
const env = EnvironmentUtils.getAugmentedEnvironment(plugin?.settings);
|
||||
|
||||
const result = await execAsync(command, { env });
|
||||
|
||||
// Log stderr as warning if present, but don't treat as error
|
||||
if (result.stderr) {
|
||||
console.warn(`PDF conversion stderr: ${result.stderr}`);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr
|
||||
};
|
||||
|
||||
} catch (cliError: any) {
|
||||
console.error(`PDF conversion error: ${cliError.message}`);
|
||||
return {
|
||||
success: false,
|
||||
error: `PDF CLI conversion failed: ${cliError.message}`,
|
||||
stderr: cliError.stderr
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that pdf2img binary is available and working.
|
||||
* @param plugin Plugin instance for accessing settings
|
||||
* @returns Validation result
|
||||
*/
|
||||
public static async validatePdf2ImgAvailable(plugin?: any): Promise<{
|
||||
available: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
const binaryLocation = await BinaryLocator.findPdf2ImgBinary(plugin);
|
||||
return {
|
||||
available: binaryLocation.exists,
|
||||
error: binaryLocation.error
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
available: false,
|
||||
error: `Failed to validate pdf2img: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user