From fa44c50dc6837081dcadf1e4e24048c6210fde47 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Wed, 10 Sep 2025 12:56:12 +0200 Subject: [PATCH] refactor: extract PDF processing utilities to PdfProcessor.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract PDF validation and metadata operations from PdfToImageConverter to improve modularity and separation of concerns. Changes: - Create src/converters/pdf/PdfProcessor.ts with PDF utilities - Move isValidPdf() and getPdfInfo() methods with full implementations - Add validatePdfFile() and isValidPdfFile() helper methods - Update PdfToImageConverter.ts to delegate to PdfProcessor - Maintain backward compatibility for all existing method signatures 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- plan.md | 14 ++-- src/converters/PdfToImageConverter.ts | 49 ++---------- src/converters/pdf/PdfProcessor.ts | 107 ++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 src/converters/pdf/PdfProcessor.ts diff --git a/plan.md b/plan.md index fafa8a9..dce6c77 100644 --- a/plan.md +++ b/plan.md @@ -205,9 +205,9 @@ The main.ts file is the plugin entry point and most critical to get right. **CHECKPOINT: Test PDF generation with various settings** -## PHASE 4: Refactor ExportConfigModal.ts - IN PROGRESS +## PHASE 4: Refactor ExportConfigModal.ts - DONE -### Step 4.1: Create ModalRenderer.ts - IN PROGRESS +### Step 4.1: Create ModalRenderer.ts - DONE **Location:** `src/ui/modal/ModalRenderer.ts` (~150 lines) **Extract:** - `createHeader()` method @@ -217,7 +217,7 @@ The main.ts file is the plugin entry point and most critical to get right. **STOP** → Verify build → Test modal displays correctly -### Step 4.2: Create ModalValidator.ts +### Step 4.2: Create ModalValidator.ts - DONE **Location:** `src/ui/modal/ModalValidator.ts` (~100 lines) **Extract:** - `validateAllSections()` method @@ -227,7 +227,7 @@ The main.ts file is the plugin entry point and most critical to get right. **STOP** → Verify build → Test validation works -### Step 4.3: Update ExportConfigModal.ts +### Step 4.3: Update ExportConfigModal.ts - DONE **Final size:** ~200 lines **Keep:** - Modal lifecycle methods @@ -238,9 +238,9 @@ The main.ts file is the plugin entry point and most critical to get right. **CHECKPOINT: Test all modal interactions** -## PHASE 5: Refactor PdfToImageConverter.ts +## PHASE 5: Refactor PdfToImageConverter.ts - IN PROGRESS -### Step 5.1: Create PdfProcessor.ts +### Step 5.1: Create PdfProcessor.ts - DONE **Location:** `src/converters/pdf/PdfProcessor.ts` (~150 lines) **Extract:** - Core PDF processing logic @@ -248,7 +248,7 @@ The main.ts file is the plugin entry point and most critical to get right. **STOP** → Verify build → Test PDF processing -### Step 5.2: Create ImageOptimizer.ts +### Step 5.2: Create ImageOptimizer.ts - IN PROGRESS **Location:** `src/converters/pdf/ImageOptimizer.ts` (~150 lines) **Extract:** - Image optimization logic diff --git a/src/converters/PdfToImageConverter.ts b/src/converters/PdfToImageConverter.ts index aeb10e3..0e10ff8 100644 --- a/src/converters/PdfToImageConverter.ts +++ b/src/converters/PdfToImageConverter.ts @@ -5,6 +5,7 @@ import * as fs from 'fs/promises'; import * as path from 'path'; +import { PdfProcessor } from './pdf/PdfProcessor'; export interface PdfConversionOptions { /** Quality/scale factor for the rendered image (default: 2.0 for HiDPI) */ @@ -91,16 +92,15 @@ export class PdfToImageConverter { quality: options.quality || 90 }; - // Ensure the PDF file exists - try { - await fs.access(pdfPath); - } catch (error) { + // Validate PDF file exists and is valid + const pdfValidation = await PdfProcessor.validatePdfFile(pdfPath); + if (!pdfValidation.success) { return { imagePath: '', originalDimensions: { width: 0, height: 0 }, imageDimensions: { width: 0, height: 0 }, success: false, - error: `PDF file not found: ${pdfPath}` + error: pdfValidation.error }; } @@ -393,15 +393,7 @@ export class PdfToImageConverter { * Check if a file is a valid PDF */ public async isValidPdf(filePath: string): Promise { - try { - const buffer = await fs.readFile(filePath); - - // Check PDF signature - const signature = buffer.subarray(0, 4).toString(); - return signature === '%PDF'; - } catch (error) { - return false; - } + return await PdfProcessor.isValidPdf(filePath); } /** @@ -413,33 +405,6 @@ export class PdfToImageConverter { success: boolean; error?: string; }> { - try { - // pdf-to-img doesn't provide metadata directly - // We'll use a basic approach with the PDF signature check - const isValid = await this.isValidPdf(filePath); - if (!isValid) { - return { - pageCount: 0, - dimensions: { width: 0, height: 0 }, - success: false, - error: 'Invalid PDF file' - }; - } - - // For now, return basic info since pdf-to-img focuses on conversion - // In a real implementation, you might want to use pdf-parse or similar for metadata - return { - pageCount: 1, // We only handle first page anyway - dimensions: { width: 612, height: 792 }, // Standard letter size estimate - success: true - }; - } catch (error) { - return { - pageCount: 0, - dimensions: { width: 0, height: 0 }, - success: false, - error: error.message - }; - } + return await PdfProcessor.getPdfInfo(filePath); } } \ No newline at end of file diff --git a/src/converters/pdf/PdfProcessor.ts b/src/converters/pdf/PdfProcessor.ts new file mode 100644 index 0000000..d74ba18 --- /dev/null +++ b/src/converters/pdf/PdfProcessor.ts @@ -0,0 +1,107 @@ +/** + * PDF Processing utilities for validation and metadata extraction. + * Handles PDF-specific operations separate from image conversion. + */ + +import * as fs from 'fs/promises'; + +export interface PdfMetadata { + /** Number of pages in the PDF */ + pageCount: number; + /** PDF dimensions (width x height) */ + dimensions: { width: number; height: number }; + /** Operation success status */ + success: boolean; + /** Error message if operation failed */ + error?: string; +} + +export class PdfProcessor { + /** + * Check if a file is a valid PDF by examining its signature. + * @param filePath Path to the file to validate + * @returns True if the file is a valid PDF, false otherwise + */ + public static async isValidPdf(filePath: string): Promise { + try { + const buffer = await fs.readFile(filePath); + + // Check PDF signature - all valid PDFs must start with %PDF + const signature = buffer.subarray(0, 4).toString(); + return signature === '%PDF'; + } catch (error) { + return false; + } + } + + /** + * Get PDF metadata including page count and dimensions. + * @param filePath Path to the PDF file + * @returns PDF metadata with page count, dimensions, and status + */ + public static async getPdfInfo(filePath: string): Promise { + try { + // First validate that this is a proper PDF file + const isValid = await this.isValidPdf(filePath); + if (!isValid) { + return { + pageCount: 0, + dimensions: { width: 0, height: 0 }, + success: false, + error: 'Invalid PDF file' + }; + } + + // For now, return basic info since we use pdf-to-img for conversion + // In a real implementation, you might want to use pdf-parse or similar for metadata + // We only handle first page anyway in the conversion process + return { + pageCount: 1, // We only handle first page anyway + dimensions: { width: 612, height: 792 }, // Standard letter size estimate + success: true + }; + } catch (error: any) { + return { + pageCount: 0, + dimensions: { width: 0, height: 0 }, + success: false, + error: error.message + }; + } + } + + /** + * Validate that a PDF file exists and is accessible. + * @param filePath Path to the PDF file + * @returns Object with success status and error message if applicable + */ + public static async validatePdfFile(filePath: string): Promise<{ + success: boolean; + error?: string; + }> { + try { + await fs.access(filePath); + return { success: true }; + } catch (error: any) { + return { + success: false, + error: `PDF file not found: ${filePath}` + }; + } + } + + /** + * Check if a file exists and is a valid PDF. + * Combines file existence check with PDF validation. + * @param filePath Path to the file to check + * @returns True if file exists and is a valid PDF, false otherwise + */ + public static async isValidPdfFile(filePath: string): Promise { + const validation = await this.validatePdfFile(filePath); + if (!validation.success) { + return false; + } + + return await this.isValidPdf(filePath); + } +} \ No newline at end of file