feat(FileDiscovery): Add dynamic image extension support with enhanced pattern matching

- Added support for multiple image formats (.png, .jpg, .jpeg, .webp, .gif, .bmp, .tiff)
- Replaced hardcoded PNG-only filtering with dynamic extension detection
- Enhanced pattern matching with 5 progressive strategies:
  1. Exact filename match
  2. Base name match with any supported extension
  3. Sanitized pattern match (removes special characters)
  4. Fuzzy match for pdf2img patterns (handles -1, -page1 suffixes)
  5. Fallback to first available image file
- Updated generateExpectedFilename with format validation and warnings
- Improved error messages to show supported formats and available files

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-25 16:06:00 +02:00
1 parent 24a169d490
commit 0cc96e9345
1 file changed
+66 -27
+66 -27
View File
@@ -17,6 +17,8 @@ export interface FileDiscoveryResult {
} }
export class FileDiscovery { export class FileDiscovery {
/** Supported image extensions for PDF conversion output */
private static readonly SUPPORTED_IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff'];
/** /**
* Find the generated image file in the output directory. * Find the generated image file in the output directory.
* Handles cases where pdf2img sanitizes filenames differently than expected. * Handles cases where pdf2img sanitizes filenames differently than expected.
@@ -84,19 +86,21 @@ export class FileDiscovery {
const files = dirList.files.map(f => path.basename(f)); const files = dirList.files.map(f => path.basename(f));
const pathUtils = new PathUtils(plugin.app); const pathUtils = new PathUtils(plugin.app);
// Look for any PNG files that might match // Look for any image files that might match
const pngFiles = files.filter(f => f.endsWith('.png')); const imageFiles = files.filter(f =>
if (pngFiles.length === 0) { this.SUPPORTED_IMAGE_EXTENSIONS.some(ext => f.toLowerCase().endsWith(ext))
);
if (imageFiles.length === 0) {
return { return {
filePath: '', filePath: '',
found: false, found: false,
error: `No PNG files found in output directory: ${outputDir}` error: `No image files found in output directory: ${outputDir}. Supported formats: ${this.SUPPORTED_IMAGE_EXTENSIONS.join(', ')}`
}; };
} }
// Try to find a file that matches the pattern (with or without exact name) // Try to find a file that matches the pattern (with or without exact name)
// pdf2img might sanitize filenames differently // pdf2img might sanitize filenames differently
const matchingFile = this.matchFilePattern(pngFiles, expectedFileName); const matchingFile = this.matchFilePattern(imageFiles, expectedFileName);
if (matchingFile) { if (matchingFile) {
const actualOutputPath = pathUtils.joinPath(outputDir, matchingFile); const actualOutputPath = pathUtils.joinPath(outputDir, matchingFile);
@@ -118,7 +122,7 @@ export class FileDiscovery {
return { return {
filePath: '', filePath: '',
found: false, found: false,
error: `Generated image file not found: ${expectedOutputPath}. Available files: ${pngFiles.join(', ')}` error: `Generated image file not found: ${expectedOutputPath}. Available files: ${imageFiles.join(', ')}`
}; };
} }
@@ -150,19 +154,21 @@ export class FileDiscovery {
// Debug: List what files are actually in the output directory // Debug: List what files are actually in the output directory
const files = await fs.readdir(outputDir); const files = await fs.readdir(outputDir);
// Look for any PNG files that might match // Look for any image files that might match
const pngFiles = files.filter((f: string) => f.endsWith('.png')); const imageFiles = files.filter((f: string) =>
if (pngFiles.length === 0) { this.SUPPORTED_IMAGE_EXTENSIONS.some(ext => f.toLowerCase().endsWith(ext))
);
if (imageFiles.length === 0) {
return { return {
filePath: '', filePath: '',
found: false, found: false,
error: `No PNG files found in output directory: ${outputDir}` error: `No image files found in output directory: ${outputDir}. Supported formats: ${this.SUPPORTED_IMAGE_EXTENSIONS.join(', ')}`
}; };
} }
// Try to find a file that matches the pattern (with or without exact name) // Try to find a file that matches the pattern (with or without exact name)
// pdf2img might sanitize filenames differently // pdf2img might sanitize filenames differently
const matchingFile = this.matchFilePattern(pngFiles, expectedFileName); const matchingFile = this.matchFilePattern(imageFiles, expectedFileName);
if (matchingFile) { if (matchingFile) {
const actualOutputPath = path.join(outputDir, matchingFile); const actualOutputPath = path.join(outputDir, matchingFile);
@@ -185,7 +191,7 @@ export class FileDiscovery {
return { return {
filePath: '', filePath: '',
found: false, found: false,
error: `Generated image file not found: ${expectedOutputPath}. Available files: ${pngFiles.join(', ')}` error: `Generated image file not found: ${expectedOutputPath}. Available files: ${imageFiles.join(', ')}`
}; };
} }
@@ -201,31 +207,55 @@ export class FileDiscovery {
} }
/** /**
* Match files against expected filename patterns. * Match files against expected filename patterns using multiple strategies.
* Handles filename sanitization that pdf2img might apply. * Handles filename sanitization that pdf2img might apply and supports multiple image formats.
* @param files Array of available filenames * @param files Array of available filenames
* @param expectedFileName Expected filename pattern * @param expectedFileName Expected filename pattern
* @returns Matching filename or null if no match found * @returns Matching filename or null if no match found
*/ */
private static matchFilePattern(files: string[], expectedFileName: string): string | null { private static matchFilePattern(files: string[], expectedFileName: string): string | null {
// Try exact match first const expectedBase = path.basename(expectedFileName, path.extname(expectedFileName));
// Strategy 1: Exact match
const exactMatch = files.find(f => f === expectedFileName); const exactMatch = files.find(f => f === expectedFileName);
if (exactMatch) { if (exactMatch) {
return exactMatch; return exactMatch;
} }
// Try partial match by removing special characters // Strategy 2: Base name match with any supported extension
const simplifiedExpected = this.sanitizeFilename(expectedFileName); const baseNameMatch = files.find(f => {
const matchingFile = files.find(f => { const fileBase = path.basename(f, path.extname(f));
const simplifiedActual = this.sanitizeFilename(f); return fileBase === expectedBase;
return simplifiedActual === simplifiedExpected;
}); });
if (baseNameMatch) {
if (matchingFile) { return baseNameMatch;
return matchingFile;
} }
// Fallback to first PNG file if no pattern matches // Strategy 3: Sanitized pattern match (remove special characters)
const simplifiedExpected = this.sanitizeFilename(expectedBase);
const sanitizedMatch = files.find(f => {
const fileBase = path.basename(f, path.extname(f));
const simplifiedActual = this.sanitizeFilename(fileBase);
return simplifiedActual === simplifiedExpected;
});
if (sanitizedMatch) {
return sanitizedMatch;
}
// Strategy 4: Fuzzy match for pdf2img output patterns (filename-1.ext, filename-page1.ext, etc.)
const fuzzyMatch = files.find(f => {
const fileBase = path.basename(f, path.extname(f));
// Remove common pdf2img suffixes like "-1", "-page1", etc.
const cleanedFileBase = fileBase.replace(/[-_](page)?\d+$/i, '');
const cleanedExpectedBase = expectedBase.replace(/[-_](page)?\d+$/i, '');
return cleanedFileBase === cleanedExpectedBase ||
this.sanitizeFilename(cleanedFileBase) === this.sanitizeFilename(cleanedExpectedBase);
});
if (fuzzyMatch) {
return fuzzyMatch;
}
// Strategy 5: Fallback to first available image file if no pattern matches
return files.length > 0 ? files[0] : null; return files.length > 0 ? files[0] : null;
} }
@@ -240,10 +270,10 @@ export class FileDiscovery {
/** /**
* Generate expected output filename for pdf2img conversion. * Generate expected output filename for pdf2img conversion.
* pdf2img uses the pattern: "filename-1.png" for first page. * pdf2img uses the pattern: "filename-1.ext" for first page.
* @param pdfBaseName Base name of the PDF file (without extension) * @param pdfBaseName Base name of the PDF file (without extension)
* @param pageNumber Page number (1-based) * @param pageNumber Page number (1-based)
* @param format Output format ('png' or 'jpeg') * @param format Output format (defaults to 'png', supports any image format)
* @returns Expected output filename * @returns Expected output filename
*/ */
public static generateExpectedFilename( public static generateExpectedFilename(
@@ -251,6 +281,15 @@ export class FileDiscovery {
pageNumber: number = 1, pageNumber: number = 1,
format: string = 'png' format: string = 'png'
): string { ): string {
return `${pdfBaseName}-${pageNumber}.${format}`; // Validate format is supported
const formatExtension = format.toLowerCase().startsWith('.') ? format.substring(1) : format.toLowerCase();
const supportedFormats = this.SUPPORTED_IMAGE_EXTENSIONS.map(ext => ext.substring(1));
if (!supportedFormats.includes(formatExtension)) {
console.warn(`Unsupported image format '${format}', falling back to PNG`);
return `${pdfBaseName}-${pageNumber}.png`;
}
return `${pdfBaseName}-${pageNumber}.${formatExtension}`;
} }
} }