refactor: migrate FileDiscovery.ts to use vault.adapter

- Replace fs.access/readdir with vault.adapter methods
- Replace path.join with PathUtils.joinPath for vault paths
- Add plugin parameter to methods for vault.adapter access
- Keep path module for basename operations on external files
- Add fs fallback when plugin unavailable (external tool output)
- Update PdfToImageConverter.ts to pass plugin to FileDiscovery
- Maintain dual approach for vault and external file discovery
This commit is contained in:
Jarvis committed 2025-09-25 12:30:25 +02:00
1 parent ee79ed3f74
commit 497407e288
2 files changed
+110 -18

No files matched your search

+1 -1
View File
@@ -124,7 +124,7 @@ export class PdfToImageConverter {
}
// Find the generated image file (may have different name than expected)
const fileDiscovery = await FileDiscovery.findGeneratedFile(outputDir, expectedOutputFileName);
const fileDiscovery = await FileDiscovery.findGeneratedFile(outputDir, expectedOutputFileName, this.plugin);
if (!fileDiscovery.found) {
return {
imagePath: '',
+109 -17
View File
@@ -3,8 +3,9 @@
* Handles filename pattern matching when pdf2img generates files with different names.
*/
import * as fs from 'fs/promises';
import * as path from 'path';
import { PathUtils } from '../../core/PathUtils';
import type { obsidianTypstPDFExport } from '../../../main';
export interface FileDiscoveryResult {
/** Path to the discovered file */
@@ -21,43 +22,68 @@ export class FileDiscovery {
* Handles cases where pdf2img sanitizes filenames differently than expected.
* @param outputDir Directory where files were generated
* @param expectedFileName Expected output filename
* @param plugin Plugin instance for PathUtils
* @returns Discovery result with actual file path
*/
public static async findGeneratedFile(
outputDir: string,
expectedFileName: string
expectedFileName: string,
plugin?: obsidianTypstPDFExport
): Promise<FileDiscoveryResult> {
const expectedOutputPath = path.join(outputDir, expectedFileName);
if (!plugin?.app) {
// Fallback to path for external tool output directories
const expectedOutputPath = path.join(outputDir, expectedFileName);
const fs = require('fs').promises;
try {
await fs.access(expectedOutputPath);
return {
filePath: expectedOutputPath,
found: true
};
} catch {
return await this.findAlternativeFileFs(outputDir, expectedFileName, expectedOutputPath);
}
}
const pathUtils = new PathUtils(plugin.app);
const expectedOutputPath = pathUtils.joinPath(outputDir, expectedFileName);
// Check if the expected output file exists first
try {
await fs.access(expectedOutputPath);
return {
filePath: expectedOutputPath,
found: true
};
if (await pathUtils.fileExists(expectedOutputPath)) {
return {
filePath: expectedOutputPath,
found: true
};
}
} catch {
// File not found with expected name, try to find alternatives
return await this.findAlternativeFile(outputDir, expectedFileName, expectedOutputPath);
// Continue to alternative search
}
// File not found with expected name, try to find alternatives
return await this.findAlternativeFile(outputDir, expectedFileName, expectedOutputPath, plugin);
}
/**
* Find alternative files when the expected filename doesn't match.
* Find alternative files when the expected filename doesn't match (using vault.adapter).
* @param outputDir Output directory to search
* @param expectedFileName Expected filename for pattern matching
* @param expectedOutputPath Full expected output path
* @param plugin Plugin instance for vault access
* @returns Discovery result with alternative file if found
*/
private static async findAlternativeFile(
outputDir: string,
expectedFileName: string,
expectedOutputPath: string
expectedOutputPath: string,
plugin: obsidianTypstPDFExport
): Promise<FileDiscoveryResult> {
try {
// Debug: List what files are actually in the output directory
const files = await fs.readdir(outputDir);
const dirList = await plugin.app.vault.adapter.list(outputDir);
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) {
@@ -67,14 +93,80 @@ export class FileDiscovery {
error: `No PNG 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);
if (matchingFile) {
const actualOutputPath = pathUtils.joinPath(outputDir, matchingFile);
// Final check if we found an alternative file
if (await pathUtils.fileExists(actualOutputPath)) {
return {
filePath: actualOutputPath,
found: true
};
} else {
return {
filePath: '',
found: false,
error: `Alternative file not accessible: ${actualOutputPath}`
};
}
} else {
return {
filePath: '',
found: false,
error: `Generated image file not found: ${expectedOutputPath}. Available files: ${pngFiles.join(', ')}`
};
}
} catch (listError: unknown) {
console.error(`Failed to list output directory:`, listError);
const errorMessage = listError instanceof Error ? listError.message : String(listError);
return {
filePath: '',
found: false,
error: `Failed to search output directory: ${errorMessage}`
};
}
}
/**
* Find alternative files when the expected filename doesn't match (fs fallback).
* @param outputDir Output directory to search
* @param expectedFileName Expected filename for pattern matching
* @param expectedOutputPath Full expected output path
* @returns Discovery result with alternative file if found
*/
private static async findAlternativeFileFs(
outputDir: string,
expectedFileName: string,
expectedOutputPath: string
): Promise<FileDiscoveryResult> {
try {
const fs = require('fs').promises;
// 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) {
return {
filePath: '',
found: false,
error: `No PNG 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);
if (matchingFile) {
const actualOutputPath = path.join(outputDir, matchingFile);
// Final check if we found an alternative file
try {
await fs.access(actualOutputPath);