refactor: extract PDF processing utilities to PdfProcessor.ts
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 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+121
-49
No files matched your search
@@ -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**
|
**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)
|
**Location:** `src/ui/modal/ModalRenderer.ts` (~150 lines)
|
||||||
**Extract:**
|
**Extract:**
|
||||||
- `createHeader()` method
|
- `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
|
**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)
|
**Location:** `src/ui/modal/ModalValidator.ts` (~100 lines)
|
||||||
**Extract:**
|
**Extract:**
|
||||||
- `validateAllSections()` method
|
- `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
|
**STOP** → Verify build → Test validation works
|
||||||
|
|
||||||
### Step 4.3: Update ExportConfigModal.ts
|
### Step 4.3: Update ExportConfigModal.ts - DONE
|
||||||
**Final size:** ~200 lines
|
**Final size:** ~200 lines
|
||||||
**Keep:**
|
**Keep:**
|
||||||
- Modal lifecycle methods
|
- 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**
|
**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)
|
**Location:** `src/converters/pdf/PdfProcessor.ts` (~150 lines)
|
||||||
**Extract:**
|
**Extract:**
|
||||||
- Core PDF processing logic
|
- 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
|
**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)
|
**Location:** `src/converters/pdf/ImageOptimizer.ts` (~150 lines)
|
||||||
**Extract:**
|
**Extract:**
|
||||||
- Image optimization logic
|
- Image optimization logic
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
import * as fs from 'fs/promises';
|
import * as fs from 'fs/promises';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { PdfProcessor } from './pdf/PdfProcessor';
|
||||||
|
|
||||||
export interface PdfConversionOptions {
|
export interface PdfConversionOptions {
|
||||||
/** Quality/scale factor for the rendered image (default: 2.0 for HiDPI) */
|
/** Quality/scale factor for the rendered image (default: 2.0 for HiDPI) */
|
||||||
@@ -91,16 +92,15 @@ export class PdfToImageConverter {
|
|||||||
quality: options.quality || 90
|
quality: options.quality || 90
|
||||||
};
|
};
|
||||||
|
|
||||||
// Ensure the PDF file exists
|
// Validate PDF file exists and is valid
|
||||||
try {
|
const pdfValidation = await PdfProcessor.validatePdfFile(pdfPath);
|
||||||
await fs.access(pdfPath);
|
if (!pdfValidation.success) {
|
||||||
} catch (error) {
|
|
||||||
return {
|
return {
|
||||||
imagePath: '',
|
imagePath: '',
|
||||||
originalDimensions: { width: 0, height: 0 },
|
originalDimensions: { width: 0, height: 0 },
|
||||||
imageDimensions: { width: 0, height: 0 },
|
imageDimensions: { width: 0, height: 0 },
|
||||||
success: false,
|
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
|
* Check if a file is a valid PDF
|
||||||
*/
|
*/
|
||||||
public async isValidPdf(filePath: string): Promise<boolean> {
|
public async isValidPdf(filePath: string): Promise<boolean> {
|
||||||
try {
|
return await PdfProcessor.isValidPdf(filePath);
|
||||||
const buffer = await fs.readFile(filePath);
|
|
||||||
|
|
||||||
// Check PDF signature
|
|
||||||
const signature = buffer.subarray(0, 4).toString();
|
|
||||||
return signature === '%PDF';
|
|
||||||
} catch (error) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -413,33 +405,6 @@ export class PdfToImageConverter {
|
|||||||
success: boolean;
|
success: boolean;
|
||||||
error?: string;
|
error?: string;
|
||||||
}> {
|
}> {
|
||||||
try {
|
return await PdfProcessor.getPdfInfo(filePath);
|
||||||
// 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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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<boolean> {
|
||||||
|
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<PdfMetadata> {
|
||||||
|
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<boolean> {
|
||||||
|
const validation = await this.validatePdfFile(filePath);
|
||||||
|
if (!validation.success) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.isValidPdf(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user