Enhances path handling and file resolution #5
@@ -17,6 +17,8 @@ export interface FileDiscoveryResult {
|
||||
}
|
||||
|
|
||||
|
||||
export class FileDiscovery {
|
||||
/** Supported image extensions for PDF conversion output */
|
||||
private static readonly SUPPORTED_IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff'];
|
||||
/**
|
||||
|
⚠️ Potential issue | 🟠 Major Don’t hard‑code Both vault-adapter and FS fallbacks now throw away every candidate that doesn’t end in Please derive the allowed extensions from Apply the same adjustment inside Also applies to: 154-190 🤖 Prompt for AI Agents_⚠️ Potential issue_ | _🟠 Major_
**Don’t hard‑code `.png` when searching for generated previews**
Both vault-adapter and FS fallbacks now throw away every candidate that doesn’t end in `.png`. As soon as we request JPEG output (which `ImageOptimizer` explicitly supports), `candidates` becomes empty and we report “file not found” even though a perfectly good `.jpeg/.jpg` file is sitting in the directory. That’s a regression for anyone relying on JPEG previews.
Please derive the allowed extensions from `expectedFileName` (and treat `.jpeg`/`.jpg` as interchangeable) before filtering, in both branches.
```diff
- // Look for any PNG files that might match
- const pngFiles = files.filter(f => f.endsWith('.png'));
- if (pngFiles.length === 0) {
+ const expectedExt = (path.extname(expectedFileName) || '.png').toLowerCase();
+ const allowedExts = expectedExt === '.jpeg' ? ['.jpeg', '.jpg'] : [expectedExt];
+ const candidateFiles = files.filter(f => {
+ const lower = f.toLowerCase();
+ return allowedExts.some(ext => lower.endsWith(ext));
+ });
+ if (candidateFiles.length === 0) {
return {
filePath: '',
found: false,
- error: `No PNG files found in output directory: ${outputDir}`
+ error: `No ${allowedExts.join('/')} files found in output directory: ${outputDir}`
};
}
// Try to find a file that matches the pattern (with or without exact name)
// pdf2img might sanitize filenames differently
- const matchingFile = this.matchFilePattern(pngFiles, expectedFileName);
+ const matchingFile = this.matchFilePattern(candidateFiles, expectedFileName);
@@
- error: `Generated image file not found: ${expectedOutputPath}. Available files: ${pngFiles.join(', ')}`
+ error: `Generated image file not found: ${expectedOutputPath}. Available files: ${candidateFiles.join(', ')}`
};
}
```
Apply the same adjustment inside `findAlternativeFileFs`.
```diff
- const pngFiles = files.filter((f: string) => f.endsWith('.png'));
- if (pngFiles.length === 0) {
+ const expectedExt = (path.extname(expectedFileName) || '.png').toLowerCase();
+ const allowedExts = expectedExt === '.jpeg' ? ['.jpeg', '.jpg'] : [expectedExt];
+ const candidateFiles = files.filter((f: string) => {
+ const lower = f.toLowerCase();
+ return allowedExts.some(ext => lower.endsWith(ext));
+ });
+ if (candidateFiles.length === 0) {
return {
filePath: '',
found: false,
- error: `No PNG files found in output directory: ${outputDir}`
+ error: `No ${allowedExts.join('/')} files found in output directory: ${outputDir}`
};
}
@@
- const matchingFile = this.matchFilePattern(pngFiles, expectedFileName);
+ const matchingFile = this.matchFilePattern(candidateFiles, expectedFileName);
@@
- error: `Generated image file not found: ${expectedOutputPath}. Available files: ${pngFiles.join(', ')}`
+ error: `Generated image file not found: ${expectedOutputPath}. Available files: ${candidateFiles.join(', ')}`
};
```
Also applies to: 154-190
<details>
<summary>🤖 Prompt for AI Agents</summary>
```
In src/converters/pdf/FileDiscovery.ts around lines 88-123 (and similarly update
the logic in findAlternativeFileFs at lines ~154-190), the code currently
hard-codes filtering for ".png" files; change it to derive the allowed
extensions from expectedFileName (e.g., if expectedFileName ends with .jpg or
.jpeg treat both .jpg and .jpeg as allowed), build a set of allowed extensions
(include the explicit extension plus normalized jpeg variants), filter files by
those extensions instead of ".png", then use that filtered list for
matchFilePattern and subsequent checks; replicate the same extension-derivation
+ filtering change in the findAlternativeFileFs branch so JPEG output is
discovered correctly.
```
</details>
<!-- fingerprinting:phantom:medusa:sabertoothed -->
<!-- This is an auto-generated comment by CodeRabbit -->
✅ Addressed in commits 24a169d to 2cdc9c3
|
||||
* Find the generated image file in the output directory.
|
||||
* 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 pathUtils = new PathUtils(plugin.app);
|
||||
|
||||
// Look for any PNG files that might match
|
||||
const pngFiles = files.filter(f => f.endsWith('.png'));
|
||||
if (pngFiles.length === 0) {
|
||||
// Look for any image files that might match
|
||||
const imageFiles = files.filter(f =>
|
||||
this.SUPPORTED_IMAGE_EXTENSIONS.some(ext => f.toLowerCase().endsWith(ext))
|
||||
);
|
||||
if (imageFiles.length === 0) {
|
||||
return {
|
||||
filePath: '',
|
||||
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)
|
||||
// pdf2img might sanitize filenames differently
|
||||
const matchingFile = this.matchFilePattern(pngFiles, expectedFileName);
|
||||
const matchingFile = this.matchFilePattern(imageFiles, expectedFileName);
|
||||
|
||||
if (matchingFile) {
|
||||
const actualOutputPath = pathUtils.joinPath(outputDir, matchingFile);
|
||||
@@ -118,7 +122,7 @@ export class FileDiscovery {
|
||||
return {
|
||||
filePath: '',
|
||||
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
|
||||
const files = await fs.readdir(outputDir);
|
||||
|
||||
// Look for any PNG files that might match
|
||||
const pngFiles = files.filter((f: string) => f.endsWith('.png'));
|
||||
if (pngFiles.length === 0) {
|
||||
// Look for any image files that might match
|
||||
const imageFiles = files.filter((f: string) =>
|
||||
this.SUPPORTED_IMAGE_EXTENSIONS.some(ext => f.toLowerCase().endsWith(ext))
|
||||
);
|
||||
if (imageFiles.length === 0) {
|
||||
return {
|
||||
filePath: '',
|
||||
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)
|
||||
// pdf2img might sanitize filenames differently
|
||||
const matchingFile = this.matchFilePattern(pngFiles, expectedFileName);
|
||||
const matchingFile = this.matchFilePattern(imageFiles, expectedFileName);
|
||||
|
||||
if (matchingFile) {
|
||||
const actualOutputPath = path.join(outputDir, matchingFile);
|
||||
@@ -185,7 +191,7 @@ export class FileDiscovery {
|
||||
return {
|
||||
filePath: '',
|
||||
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.
|
||||
* Handles filename sanitization that pdf2img might apply.
|
||||
* Match files against expected filename patterns using multiple strategies.
|
||||
* Handles filename sanitization that pdf2img might apply and supports multiple image formats.
|
||||
* @param files Array of available filenames
|
||||
* @param expectedFileName Expected filename pattern
|
||||
* @returns Matching filename or null if no match found
|
||||
*/
|
||||
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);
|
||||
if (exactMatch) {
|
||||
return exactMatch;
|
||||
}
|
||||
|
||||
// Try partial match by removing special characters
|
||||
const simplifiedExpected = this.sanitizeFilename(expectedFileName);
|
||||
const matchingFile = files.find(f => {
|
||||
const simplifiedActual = this.sanitizeFilename(f);
|
||||
return simplifiedActual === simplifiedExpected;
|
||||
// Strategy 2: Base name match with any supported extension
|
||||
const baseNameMatch = files.find(f => {
|
||||
const fileBase = path.basename(f, path.extname(f));
|
||||
return fileBase === expectedBase;
|
||||
});
|
||||
|
||||
if (matchingFile) {
|
||||
return matchingFile;
|
||||
if (baseNameMatch) {
|
||||
return baseNameMatch;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -240,10 +270,10 @@ export class FileDiscovery {
|
||||
|
||||
/**
|
||||
* 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 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
|
||||
*/
|
||||
public static generateExpectedFilename(
|
||||
@@ -251,6 +281,15 @@ export class FileDiscovery {
|
||||
pageNumber: number = 1,
|
||||
format: string = 'png'
|
||||
): 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}`;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user
⚠️ Potential issue | 🔴 Critical
Vault adapter requires vault-relative paths; branch to FS for absolute outputDir
adapter.list(outputDir)will throw for absolute paths (typical for temp dirs). Convert to vault-relative when inside the vault; otherwise delegate to the FS fallback.📝 Committable suggestion
🤖 Prompt for AI Agents
✅ Addressed in commits
a129380to731a76e