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:
2 files changed
+105
-13
No files matched your search
@@ -124,7 +124,7 @@ export class PdfToImageConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the generated image file (may have different name than expected)
|
// 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) {
|
if (!fileDiscovery.found) {
|
||||||
return {
|
return {
|
||||||
imagePath: '',
|
imagePath: '',
|
||||||
|
|||||||
@@ -3,8 +3,9 @@
|
|||||||
* Handles filename pattern matching when pdf2img generates files with different names.
|
* Handles filename pattern matching when pdf2img generates files with different names.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as fs from 'fs/promises';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { PathUtils } from '../../core/PathUtils';
|
||||||
|
import type { obsidianTypstPDFExport } from '../../../main';
|
||||||
|
|
||||||
export interface FileDiscoveryResult {
|
export interface FileDiscoveryResult {
|
||||||
/** Path to the discovered file */
|
/** Path to the discovered file */
|
||||||
@@ -21,45 +22,136 @@ export class FileDiscovery {
|
|||||||
* Handles cases where pdf2img sanitizes filenames differently than expected.
|
* Handles cases where pdf2img sanitizes filenames differently than expected.
|
||||||
* @param outputDir Directory where files were generated
|
* @param outputDir Directory where files were generated
|
||||||
* @param expectedFileName Expected output filename
|
* @param expectedFileName Expected output filename
|
||||||
|
* @param plugin Plugin instance for PathUtils
|
||||||
* @returns Discovery result with actual file path
|
* @returns Discovery result with actual file path
|
||||||
*/
|
*/
|
||||||
public static async findGeneratedFile(
|
public static async findGeneratedFile(
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
expectedFileName: string
|
expectedFileName: string,
|
||||||
|
plugin?: obsidianTypstPDFExport
|
||||||
): Promise<FileDiscoveryResult> {
|
): 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
|
// Check if the expected output file exists first
|
||||||
try {
|
try {
|
||||||
await fs.access(expectedOutputPath);
|
if (await pathUtils.fileExists(expectedOutputPath)) {
|
||||||
return {
|
return {
|
||||||
filePath: expectedOutputPath,
|
filePath: expectedOutputPath,
|
||||||
found: true
|
found: true
|
||||||
};
|
};
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// File not found with expected name, try to find alternatives
|
// Continue to alternative search
|
||||||
return await this.findAlternativeFile(outputDir, expectedFileName, expectedOutputPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 outputDir Output directory to search
|
||||||
* @param expectedFileName Expected filename for pattern matching
|
* @param expectedFileName Expected filename for pattern matching
|
||||||
* @param expectedOutputPath Full expected output path
|
* @param expectedOutputPath Full expected output path
|
||||||
|
* @param plugin Plugin instance for vault access
|
||||||
* @returns Discovery result with alternative file if found
|
* @returns Discovery result with alternative file if found
|
||||||
*/
|
*/
|
||||||
private static async findAlternativeFile(
|
private static async findAlternativeFile(
|
||||||
|
outputDir: string,
|
||||||
|
expectedFileName: string,
|
||||||
|
expectedOutputPath: string,
|
||||||
|
plugin: obsidianTypstPDFExport
|
||||||
|
): Promise<FileDiscoveryResult> {
|
||||||
|
try {
|
||||||
|
// Debug: List what files are actually in the output directory
|
||||||
|
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) {
|
||||||
|
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 = 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,
|
outputDir: string,
|
||||||
expectedFileName: string,
|
expectedFileName: string,
|
||||||
expectedOutputPath: string
|
expectedOutputPath: string
|
||||||
): Promise<FileDiscoveryResult> {
|
): Promise<FileDiscoveryResult> {
|
||||||
try {
|
try {
|
||||||
|
const fs = require('fs').promises;
|
||||||
// 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 PNG files that might match
|
||||||
const pngFiles = files.filter(f => f.endsWith('.png'));
|
const pngFiles = files.filter((f: string) => f.endsWith('.png'));
|
||||||
if (pngFiles.length === 0) {
|
if (pngFiles.length === 0) {
|
||||||
return {
|
return {
|
||||||
filePath: '',
|
filePath: '',
|
||||||
|
|||||||
Reference in new issue
Block a user