From fefd58918702135556bc27d009b1b930f6146971 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 7 Dec 2025 20:02:01 -0600 Subject: [PATCH] Don't completely remove the tmpDir; ensure it exists before saving a generated image to it --- src/App.tsx | 28 +++++++++++++++++++++++----- src/lib/tmpCleanup.ts | 18 +++++++++++------- src/lib/tools/GenerateImageTool.ts | 7 +++++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a85f19c1..1992a68d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -146,15 +146,33 @@ const fs = globalThis.electron ? new ElectronFSAdapter() : new LightningFSAdapter(new LightningFS('shakespeare-fs').promises); -// Component to handle filesystem cleanup on startup -// Automatically removes files older than 1 hour from tmp directory +// Component to handle filesystem initialization and cleanup on startup +// Ensures tmp directory exists and removes files older than 1 hour function FSCleanupHandler() { const { config } = useAppContext(); useEffect(() => { - // Run cleanup on application startup to remove stale temporary files - // This helps prevent the VFS from accumulating old files over time - cleanupTmpDirectory(fs, config.fsPathTmp).catch(console.error); + // Ensure tmp directory exists before cleanup + const initializeAndCleanup = async () => { + try { + // Create tmp directory if it doesn't exist + try { + await fs.stat(config.fsPathTmp); + } catch { + // Directory doesn't exist, create it + await fs.mkdir(config.fsPathTmp); + console.log(`Created ${config.fsPathTmp} directory`); + } + + // Run cleanup to remove stale temporary files + // This helps prevent the VFS from accumulating old files over time + await cleanupTmpDirectory(fs, config.fsPathTmp); + } catch (error) { + console.error(`Error during filesystem initialization:`, error); + } + }; + + initializeAndCleanup(); }, [config.fsPathTmp]); return null; diff --git a/src/lib/tmpCleanup.ts b/src/lib/tmpCleanup.ts index d5de3a27..a2c94b3a 100644 --- a/src/lib/tmpCleanup.ts +++ b/src/lib/tmpCleanup.ts @@ -5,8 +5,9 @@ import type { JSRuntimeFS } from './JSRuntime'; * @param fs - The filesystem instance * @param dirPath - The directory path to clean * @param maxAgeMs - Maximum age in milliseconds (files older than this will be removed) + * @param rootPath - The root directory being cleaned (should never be deleted) */ -async function cleanupDirectory(fs: JSRuntimeFS, dirPath: string, maxAgeMs: number): Promise { +async function cleanupDirectory(fs: JSRuntimeFS, dirPath: string, maxAgeMs: number, rootPath: string): Promise { const now = Date.now(); try { @@ -34,11 +35,14 @@ async function cleanupDirectory(fs: JSRuntimeFS, dirPath: string, maxAgeMs: numb if (fileAge > maxAgeMs) { if (entry.isDirectory()) { // Recursively clean subdirectory first, then remove it - await cleanupDirectory(fs, fullPath, maxAgeMs); + await cleanupDirectory(fs, fullPath, maxAgeMs, rootPath); try { - // Try to remove the directory (will only succeed if empty) - await fs.rmdir(fullPath); - console.log(`Removed old directory: ${fullPath}`); + // Never delete the root cleanup directory itself, only its contents + if (fullPath !== rootPath) { + // Try to remove the directory (will only succeed if empty) + await fs.rmdir(fullPath); + console.log(`Removed old directory: ${fullPath}`); + } } catch { // Directory might not be empty, that's okay } @@ -49,7 +53,7 @@ async function cleanupDirectory(fs: JSRuntimeFS, dirPath: string, maxAgeMs: numb } } else if (entry.isDirectory()) { // Directory is not old enough to remove, but clean its contents - await cleanupDirectory(fs, fullPath, maxAgeMs); + await cleanupDirectory(fs, fullPath, maxAgeMs, rootPath); } } catch (error) { // Log error but continue with other files @@ -72,7 +76,7 @@ export async function cleanupTmpDirectory(fs: JSRuntimeFS, tmpPath = '/tmp'): Pr console.log(`Starting ${tmpPath} directory cleanup...`); try { - await cleanupDirectory(fs, tmpPath, ONE_HOUR_MS); + await cleanupDirectory(fs, tmpPath, ONE_HOUR_MS, tmpPath); console.log(`Completed ${tmpPath} directory cleanup`); } catch (error) { console.error(`Error during ${tmpPath} cleanup:`, error); diff --git a/src/lib/tools/GenerateImageTool.ts b/src/lib/tools/GenerateImageTool.ts index ad1dd56f..25132fd9 100644 --- a/src/lib/tools/GenerateImageTool.ts +++ b/src/lib/tools/GenerateImageTool.ts @@ -142,6 +142,13 @@ export class GenerateImageTool implements Tool { const filename = `generated-${timestamp}.${extension}`; const filepath = `${this.tmpPath}/${filename}`; + // Ensure tmp directory exists + try { + await this.fs.stat(this.tmpPath); + } catch { + await this.fs.mkdir(this.tmpPath, { recursive: true }); + } + // Write image to VFS await this.fs.writeFile(filepath, imageData);