refactor: migrate ImageOptimizer.ts to use vault.adapter

- Replace fs.unlink/rename with vault.adapter methods when plugin available
- Replace path.join with PathUtils.joinPath for vault paths
- Add plugin parameter to optimizeImage method for vault.adapter access
- Keep path module for external tool operations
- Add fs fallbacks for temp file operations when plugin unavailable
- Use readBinary/writeBinary pattern for image file moves
- Update PdfToImageConverter.ts to pass plugin to ImageOptimizer
- Maintain dual approach for vault and external temp file handling
This commit is contained in:
Jarvis committed 2025-09-25 12:35:45 +02:00
1 parent 497407e288
commit b6f64d1cf7
2 files changed
+32 -4

No files matched your search

+2 -1
View File
@@ -143,7 +143,8 @@ export class PdfToImageConverter {
{ {
format: opts.format, format: opts.format,
quality: opts.quality quality: opts.quality
} },
this.plugin
); );
if (!optimizationResult.success) { if (!optimizationResult.success) {
+30 -3
View File
@@ -3,8 +3,9 @@
* Handles format conversion, quality management, and dimension detection. * Handles format conversion, quality management, and dimension detection.
*/ */
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 ImageOptimizationOptions { export interface ImageOptimizationOptions {
/** Output format (default: 'png') */ /** Output format (default: 'png') */
@@ -183,7 +184,8 @@ export class ImageOptimizer {
inputImagePath: string, inputImagePath: string,
outputDir: string, outputDir: string,
baseFileName: string, baseFileName: string,
options: ImageOptimizationOptions = {} options: ImageOptimizationOptions = {},
plugin?: obsidianTypstPDFExport
): Promise<ImageOptimizationResult> { ): Promise<ImageOptimizationResult> {
try { try {
const opts = { const opts = {
@@ -193,7 +195,13 @@ export class ImageOptimizer {
// Generate final output path // Generate final output path
const finalOutputFileName = `${baseFileName}_preview.${opts.format}`; const finalOutputFileName = `${baseFileName}_preview.${opts.format}`;
const finalOutputPath = path.join(outputDir, finalOutputFileName); let finalOutputPath: string;
if (plugin?.app) {
const pathUtils = new PathUtils(plugin.app);
finalOutputPath = pathUtils.joinPath(outputDir, finalOutputFileName);
} else {
finalOutputPath = path.join(outputDir, finalOutputFileName);
}
let actualImagePath = inputImagePath; let actualImagePath = inputImagePath;
@@ -209,7 +217,12 @@ export class ImageOptimizer {
if (conversionResult.success) { if (conversionResult.success) {
// Clean up the original PNG if conversion succeeded // Clean up the original PNG if conversion succeeded
try { try {
if (plugin?.app) {
await plugin.app.vault.adapter.remove(inputImagePath);
} else {
const fs = require('fs').promises;
await fs.unlink(inputImagePath); await fs.unlink(inputImagePath);
}
} catch (unlinkError) { } catch (unlinkError) {
console.warn(`Could not remove original PNG: ${unlinkError}`); console.warn(`Could not remove original PNG: ${unlinkError}`);
} }
@@ -218,12 +231,26 @@ export class ImageOptimizer {
console.warn(`ImageMagick not available for JPEG conversion, keeping PNG format: ${conversionResult.error}`); console.warn(`ImageMagick not available for JPEG conversion, keeping PNG format: ${conversionResult.error}`);
// Just rename the file to PNG format // Just rename the file to PNG format
const pngFinalPath = finalOutputPath.replace(/\.jpeg?$/, '.png'); const pngFinalPath = finalOutputPath.replace(/\.jpeg?$/, '.png');
if (plugin?.app) {
const content = await plugin.app.vault.adapter.readBinary(inputImagePath);
await plugin.app.vault.adapter.writeBinary(pngFinalPath, content);
await plugin.app.vault.adapter.remove(inputImagePath);
} else {
const fs = require('fs').promises;
await fs.rename(inputImagePath, pngFinalPath); await fs.rename(inputImagePath, pngFinalPath);
}
actualImagePath = pngFinalPath; actualImagePath = pngFinalPath;
} }
} else { } else {
// Keep as PNG, just rename to final location // Keep as PNG, just rename to final location
if (plugin?.app) {
const content = await plugin.app.vault.adapter.readBinary(inputImagePath);
await plugin.app.vault.adapter.writeBinary(finalOutputPath, content);
await plugin.app.vault.adapter.remove(inputImagePath);
} else {
const fs = require('fs').promises;
await fs.rename(inputImagePath, finalOutputPath); await fs.rename(inputImagePath, finalOutputPath);
}
actualImagePath = finalOutputPath; actualImagePath = finalOutputPath;
} }