refactor: migrate EmbedProcessor to use vault.adapter

- Replaced fs.promises.access with PathUtils.fileExists() for file existence checks
- Replaced fs.promises.mkdir with PathUtils.ensureDir() for directory creation
- Replaced fs.promises.copyFile with vault.adapter readBinary/writeBinary operations
- Replaced path.join with PathUtils.joinPath() for consistent path normalization
- Updated all file resolution methods to use proper Obsidian API methods
- Removed fs import and all direct filesystem operations
- Enhanced copyImageToVaultTemp to use vault.adapter for binary file operations

🤖 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 12:14:11 +02:00
1 parent da904e6ddd
commit 610a87d788
1 file changed
+47 -30
+47 -30
View File
@@ -7,11 +7,15 @@ import { TFile } from 'obsidian';
import type { obsidianTypstPDFExport } from '../../main'; import type { obsidianTypstPDFExport } from '../../main';
import type { PreprocessingResult } from '../converters/MarkdownPreprocessor'; import type { PreprocessingResult } from '../converters/MarkdownPreprocessor';
import { ExportErrorHandler } from '../core/ExportErrorHandler'; import { ExportErrorHandler } from '../core/ExportErrorHandler';
import { PathUtils } from '../core/PathUtils';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs';
export class EmbedProcessor { export class EmbedProcessor {
constructor(private plugin: obsidianTypstPDFExport) {} private readonly pathUtils: PathUtils;
constructor(private plugin: obsidianTypstPDFExport) {
this.pathUtils = new PathUtils(plugin.app);
}
/** /**
* Process PDF embeds - convert PDF pages to images for inclusion * Process PDF embeds - convert PDF pages to images for inclusion
@@ -126,9 +130,11 @@ export class EmbedProcessor {
// Try each possible path until we find one that exists // Try each possible path until we find one that exists
for (const possiblePath of possiblePaths) { for (const possiblePath of possiblePaths) {
try { try {
await fs.promises.access(possiblePath); const exists = await this.pathUtils.fileExists(possiblePath);
fullImagePath = possiblePath; if (exists) {
break; fullImagePath = possiblePath;
break;
}
} catch { } catch {
// File doesn't exist, continue to next path // File doesn't exist, continue to next path
} }
@@ -150,10 +156,10 @@ export class EmbedProcessor {
// Convert WebP to PNG using ImageMagick // Convert WebP to PNG using ImageMagick
const originalImageName = path.basename(fullImagePath); const originalImageName = path.basename(fullImagePath);
const pngFileName = originalImageName.replace(/\.webp$/i, '.png'); const pngFileName = originalImageName.replace(/\.webp$/i, '.png');
const vaultTempImagesDir = path.join(vaultBasePath, this.plugin.manifest.dir!, 'temp-images'); const vaultTempImagesDir = this.pathUtils.joinPath(vaultBasePath, this.pathUtils.getPluginDir(this.plugin.manifest), 'temp-images');
await fs.promises.mkdir(vaultTempImagesDir, { recursive: true }); await this.pathUtils.ensureDir(vaultTempImagesDir);
const convertedImagePath = path.join(vaultTempImagesDir, pngFileName); const convertedImagePath = this.pathUtils.joinPath(vaultTempImagesDir, pngFileName);
const { exec } = require('child_process'); const { exec } = require('child_process');
const util = require('util'); const util = require('util');
@@ -291,20 +297,22 @@ export class EmbedProcessor {
private async resolvePdfPath(sanitizedPath: string, vaultBasePath: string, currentFile?: TFile): Promise<string | null> { private async resolvePdfPath(sanitizedPath: string, vaultBasePath: string, currentFile?: TFile): Promise<string | null> {
// Decode the URL-encoded sanitized path back to normal characters // Decode the URL-encoded sanitized path back to normal characters
const decodedPath = decodeURIComponent(sanitizedPath); const decodedPath = decodeURIComponent(sanitizedPath);
// Strategy 1: Use Obsidian's link resolution API (primary strategy) // Strategy 1: Use Obsidian's link resolution API (primary strategy)
if (currentFile) { if (currentFile) {
const resolvedFile = this.plugin.app.metadataCache.getFirstLinkpathDest( const resolvedFile = this.plugin.app.metadataCache.getFirstLinkpathDest(
decodedPath, decodedPath,
currentFile.path currentFile.path
); );
if (resolvedFile && resolvedFile instanceof TFile) { if (resolvedFile && resolvedFile instanceof TFile) {
// Convert to full filesystem path // Convert to full filesystem path
const fullPath = path.resolve(vaultBasePath, resolvedFile.path); const fullPath = path.resolve(vaultBasePath, resolvedFile.path);
try { try {
await fs.promises.access(fullPath); const exists = await this.pathUtils.fileExists(fullPath);
return fullPath; if (exists) {
return fullPath;
}
} catch { } catch {
// File exists in metadata but not on filesystem, continue to fallbacks // File exists in metadata but not on filesystem, continue to fallbacks
} }
@@ -322,8 +330,10 @@ export class EmbedProcessor {
// Try each possible path until we find one that exists // Try each possible path until we find one that exists
for (const possiblePath of possiblePaths) { for (const possiblePath of possiblePaths) {
try { try {
await fs.promises.access(possiblePath); const exists = await this.pathUtils.fileExists(possiblePath);
return possiblePath; if (exists) {
return possiblePath;
}
} catch { } catch {
// File doesn't exist, continue to next path // File doesn't exist, continue to next path
} }
@@ -338,20 +348,22 @@ export class EmbedProcessor {
private async resolveFilePath(sanitizedPath: string, vaultBasePath: string, currentFile?: TFile): Promise<string | null> { private async resolveFilePath(sanitizedPath: string, vaultBasePath: string, currentFile?: TFile): Promise<string | null> {
// Decode the URL-encoded sanitized path back to normal characters // Decode the URL-encoded sanitized path back to normal characters
const decodedPath = decodeURIComponent(sanitizedPath); const decodedPath = decodeURIComponent(sanitizedPath);
// Strategy 1: Use Obsidian's link resolution API (primary strategy) // Strategy 1: Use Obsidian's link resolution API (primary strategy)
if (currentFile) { if (currentFile) {
const resolvedFile = this.plugin.app.metadataCache.getFirstLinkpathDest( const resolvedFile = this.plugin.app.metadataCache.getFirstLinkpathDest(
decodedPath, decodedPath,
currentFile.path currentFile.path
); );
if (resolvedFile && resolvedFile instanceof TFile) { if (resolvedFile && resolvedFile instanceof TFile) {
// Convert to full filesystem path // Convert to full filesystem path
const fullPath = path.resolve(vaultBasePath, resolvedFile.path); const fullPath = path.resolve(vaultBasePath, resolvedFile.path);
try { try {
await fs.promises.access(fullPath); const exists = await this.pathUtils.fileExists(fullPath);
return fullPath; if (exists) {
return fullPath;
}
} catch { } catch {
// File exists in metadata but not on filesystem, continue to fallbacks // File exists in metadata but not on filesystem, continue to fallbacks
} }
@@ -371,8 +383,10 @@ export class EmbedProcessor {
// Try each possible path until we find one that exists // Try each possible path until we find one that exists
for (const possiblePath of possiblePaths) { for (const possiblePath of possiblePaths) {
try { try {
await fs.promises.access(possiblePath); const exists = await this.pathUtils.fileExists(possiblePath);
return possiblePath; if (exists) {
return possiblePath;
}
} catch { } catch {
// File doesn't exist, continue to next path // File doesn't exist, continue to next path
} }
@@ -396,23 +410,26 @@ export class EmbedProcessor {
vaultBasePath: string vaultBasePath: string
): Promise<{ relativeImagePath: string; relativePdfPath: string }> { ): Promise<{ relativeImagePath: string; relativePdfPath: string }> {
// Copy image to vault temp directory for access // Copy image to vault temp directory for access
const vaultTempImagesDir = path.join(vaultBasePath, this.plugin.manifest.dir!, 'temp-images'); const vaultTempImagesDir = this.pathUtils.joinPath(vaultBasePath, this.pathUtils.getPluginDir(this.plugin.manifest), 'temp-images');
await fs.promises.mkdir(vaultTempImagesDir, { recursive: true }); await this.pathUtils.ensureDir(vaultTempImagesDir);
// Sanitize the basename for use in filename - replace problematic characters // Sanitize the basename for use in filename - replace problematic characters
const sanitizedBaseName = baseName const sanitizedBaseName = baseName
.replace(/[^a-zA-Z0-9\-_]/g, '_') // Replace non-alphanumeric chars with underscore .replace(/[^a-zA-Z0-9\-_]/g, '_') // Replace non-alphanumeric chars with underscore
.replace(/_{2,}/g, '_') // Collapse multiple underscores .replace(/_{2,}/g, '_') // Collapse multiple underscores
.replace(/^_+|_+$/g, ''); // Remove leading/trailing underscores .replace(/^_+|_+$/g, ''); // Remove leading/trailing underscores
const imageFileName = `${sanitizedBaseName}_preview.png`; const imageFileName = `${sanitizedBaseName}_preview.png`;
const vaultImagePath = path.join(vaultTempImagesDir, imageFileName); const vaultImagePath = this.pathUtils.joinPath(vaultTempImagesDir, imageFileName);
await fs.promises.copyFile(imagePath, vaultImagePath);
// Copy file using vault adapter
const sourceBuffer = await this.plugin.app.vault.adapter.readBinary(imagePath);
await this.plugin.app.vault.adapter.writeBinary(vaultImagePath, sourceBuffer);
// Get relative paths from vault base // Get relative paths from vault base
const relativeImagePath = path.relative(vaultBasePath, vaultImagePath); const relativeImagePath = path.relative(vaultBasePath, vaultImagePath);
const relativePdfPath = path.relative(vaultBasePath, pdfPath); const relativePdfPath = path.relative(vaultBasePath, pdfPath);
return { relativeImagePath, relativePdfPath }; return { relativeImagePath, relativePdfPath };
} }