Introduces temp directory management
Centralizes temporary directory creation and cleanup using a dedicated `TempDirectoryManager` class. This change improves code maintainability and reduces redundancy by encapsulating temporary directory logic. It also ensures that temporary directories are properly cleaned up after export operations, preventing potential disk space issues.
This commit is contained in:
3 files changed
+116
-40
No files matched your search
@@ -24,6 +24,7 @@ import { FALLBACK_FONTS, PLUGIN_DIRS } from './src/core/constants';
|
||||
import { DependencyChecker } from './src/core/DependencyChecker';
|
||||
import { ModalSettingsHelper } from './src/core/ModalSettingsHelper';
|
||||
import { ExportErrorHandler } from './src/core/ExportErrorHandler';
|
||||
import { TempDirectoryManager } from './src/core/TempDirectoryManager';
|
||||
import { PandocTypstConverter } from './src/converters/PandocTypstConverter';
|
||||
import { ExportConfigModal } from './src/modal/ExportConfigModal';
|
||||
import { ExportConfig, ExportConfigModalSettings } from './src/modal/types';
|
||||
@@ -435,14 +436,9 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
const content = await this.app.vault.read(file);
|
||||
const pluginDir = path.join(vaultPath, '.obsidian', 'plugins', 'obsidian-typst-pdf-export');
|
||||
|
||||
// Create temp directory for processing - use plugin's temp directory
|
||||
const tempDir = path.join(pluginDir, 'temp-images');
|
||||
|
||||
// Ensure temp directory exists
|
||||
const fs = require('fs');
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
// Create temp directory manager and ensure temp directory exists
|
||||
const tempManager = TempDirectoryManager.create(vaultPath);
|
||||
const tempDir = tempManager.ensureTempDir('images');
|
||||
|
||||
// Preprocess the markdown content using MarkdownPreprocessor
|
||||
const { MarkdownPreprocessor } = await import('./src/MarkdownPreprocessor');
|
||||
@@ -530,28 +526,8 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
|
||||
// Clean up temporary directories
|
||||
try {
|
||||
const pathModule = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// Clean up temp-images directory
|
||||
const vaultTempImagesDir = pathModule.join(vaultPath, '.obsidian', 'plugins', 'obsidian-typst-pdf-export', 'temp-images');
|
||||
if (fs.existsSync(vaultTempImagesDir)) {
|
||||
const files = fs.readdirSync(vaultTempImagesDir);
|
||||
for (const file of files) {
|
||||
fs.unlinkSync(pathModule.join(vaultTempImagesDir, file));
|
||||
}
|
||||
console.log('Export: Cleaned up temp-images directory');
|
||||
}
|
||||
|
||||
// Clean up temp-pandoc directory
|
||||
const vaultTempPandocDir = pathModule.join(vaultPath, '.obsidian', 'plugins', 'obsidian-typst-pdf-export', 'temp-pandoc');
|
||||
if (fs.existsSync(vaultTempPandocDir)) {
|
||||
const files = fs.readdirSync(vaultTempPandocDir);
|
||||
for (const file of files) {
|
||||
fs.unlinkSync(pathModule.join(vaultTempPandocDir, file));
|
||||
}
|
||||
console.log('Export: Cleaned up temp-pandoc directory');
|
||||
}
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath);
|
||||
cleanupManager.cleanupAllTempDirs();
|
||||
} catch (cleanupError) {
|
||||
console.warn('Export: Failed to clean up temporary directories:', cleanupError);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import * as os from 'os';
|
||||
import { mapToTypstPaperSize } from '../utils/paperSizeMapper';
|
||||
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './types';
|
||||
import type { obsidianTypstPDFExportSettings } from '../core/settings';
|
||||
import { TempDirectoryManager } from '../core/TempDirectoryManager';
|
||||
|
||||
export class PandocTypstConverter {
|
||||
private tempDir: string | null = null;
|
||||
@@ -235,18 +236,13 @@ export class PandocTypstConverter {
|
||||
*/
|
||||
private async ensureTempDirectory(): Promise<string> {
|
||||
if (!this.tempDir) {
|
||||
// Create temp directory in plugin folder instead of system temp
|
||||
const pluginTempDir = this.pandocOptions.vaultBasePath
|
||||
? path.join(this.pandocOptions.vaultBasePath, '.obsidian', 'plugins', 'obsidian-typst-pdf-export', 'temp-pandoc')
|
||||
: path.join(os.tmpdir(), 'obsidian-typst-');
|
||||
|
||||
// Ensure the temp directory exists
|
||||
if (pluginTempDir.includes('temp-pandoc')) {
|
||||
await fsPromises.mkdir(pluginTempDir, { recursive: true });
|
||||
this.tempDir = pluginTempDir;
|
||||
if (this.pandocOptions.vaultBasePath) {
|
||||
// Use TempDirectoryManager for plugin temp directories
|
||||
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath);
|
||||
this.tempDir = tempManager.ensureTempDir('pandoc');
|
||||
} else {
|
||||
// Fallback to system temp if plugin folder not available
|
||||
this.tempDir = await fsPromises.mkdtemp(pluginTempDir);
|
||||
this.tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'obsidian-typst-'));
|
||||
}
|
||||
}
|
||||
return this.tempDir;
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Manages temporary directories for export operations
|
||||
*/
|
||||
|
||||
import { PLUGIN_DIRS } from './constants';
|
||||
|
||||
export interface TempDirectoryOptions {
|
||||
/** Base path for the vault */
|
||||
vaultPath: string;
|
||||
/** Plugin directory name (defaults to 'obsidian-typst-pdf-export') */
|
||||
pluginName?: string;
|
||||
}
|
||||
|
||||
export class TempDirectoryManager {
|
||||
private readonly fs = require('fs');
|
||||
private readonly path = require('path');
|
||||
private readonly vaultPath: string;
|
||||
private readonly pluginName: string;
|
||||
|
||||
constructor(options: TempDirectoryOptions) {
|
||||
this.vaultPath = options.vaultPath;
|
||||
this.pluginName = options.pluginName || 'obsidian-typst-pdf-export';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin directory path
|
||||
*/
|
||||
private getPluginDir(): string {
|
||||
return this.path.join(this.vaultPath, '.obsidian', 'plugins', this.pluginName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path for a specific temp directory
|
||||
*/
|
||||
public getTempDir(type: 'images' | 'pandoc'): string {
|
||||
const dirName = type === 'images' ? PLUGIN_DIRS.TEMP_IMAGES : PLUGIN_DIRS.TEMP_PANDOC;
|
||||
return this.path.join(this.getPluginDir(), dirName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a temp directory exists, creating it if necessary
|
||||
*/
|
||||
public ensureTempDir(type: 'images' | 'pandoc'): string {
|
||||
const tempDir = this.getTempDir(type);
|
||||
|
||||
if (!this.fs.existsSync(tempDir)) {
|
||||
this.fs.mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up a specific temp directory
|
||||
*/
|
||||
public cleanupTempDir(type: 'images' | 'pandoc'): boolean {
|
||||
const tempDir = this.getTempDir(type);
|
||||
|
||||
try {
|
||||
if (this.fs.existsSync(tempDir)) {
|
||||
const files = this.fs.readdirSync(tempDir);
|
||||
for (const file of files) {
|
||||
this.fs.unlinkSync(this.path.join(tempDir, file));
|
||||
}
|
||||
console.log(`Export: Cleaned up ${type === 'images' ? 'temp-images' : 'temp-pandoc'} directory`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Export: Failed to clean up ${type === 'images' ? 'temp-images' : 'temp-pandoc'} directory:`, error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up all temp directories
|
||||
*/
|
||||
public cleanupAllTempDirs(): { images: boolean; pandoc: boolean } {
|
||||
return {
|
||||
images: this.cleanupTempDir('images'),
|
||||
pandoc: this.cleanupTempDir('pandoc')
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to create manager from vault path
|
||||
*/
|
||||
public static create(vaultPath: string, pluginName?: string): TempDirectoryManager {
|
||||
return new TempDirectoryManager({ vaultPath, pluginName });
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a path is within the plugin's temp directories
|
||||
*/
|
||||
public isPluginTempDir(dirPath: string): boolean {
|
||||
const pluginDir = this.getPluginDir();
|
||||
const normalizedPath = this.path.resolve(dirPath);
|
||||
const normalizedPluginDir = this.path.resolve(pluginDir);
|
||||
|
||||
return normalizedPath.startsWith(normalizedPluginDir) &&
|
||||
(dirPath.includes(PLUGIN_DIRS.TEMP_IMAGES) || dirPath.includes(PLUGIN_DIRS.TEMP_PANDOC));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user