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:
1 file changed
+31
-14
@@ -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);
|
||||||
|
if (exists) {
|
||||||
fullImagePath = possiblePath;
|
fullImagePath = possiblePath;
|
||||||
break;
|
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');
|
||||||
@@ -303,8 +309,10 @@ export class EmbedProcessor {
|
|||||||
// 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);
|
||||||
|
if (exists) {
|
||||||
return fullPath;
|
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);
|
||||||
|
if (exists) {
|
||||||
return possiblePath;
|
return possiblePath;
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// File doesn't exist, continue to next path
|
// File doesn't exist, continue to next path
|
||||||
}
|
}
|
||||||
@@ -350,8 +360,10 @@ export class EmbedProcessor {
|
|||||||
// 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);
|
||||||
|
if (exists) {
|
||||||
return fullPath;
|
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);
|
||||||
|
if (exists) {
|
||||||
return possiblePath;
|
return possiblePath;
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// File doesn't exist, continue to next path
|
// File doesn't exist, continue to next path
|
||||||
}
|
}
|
||||||
@@ -396,8 +410,8 @@ 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
|
||||||
@@ -406,8 +420,11 @@ export class EmbedProcessor {
|
|||||||
.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);
|
||||||
|
|||||||
Reference in new issue
Block a user