mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Don't completely remove the tmpDir; ensure it exists before saving a generated image to it
This commit is contained in:
3 files changed
+41
-12
No files matched your search
+23
-5
@@ -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;
|
||||
|
||||
+11
-7
@@ -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<void> {
|
||||
async function cleanupDirectory(fs: JSRuntimeFS, dirPath: string, maxAgeMs: number, rootPath: string): Promise<void> {
|
||||
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);
|
||||
|
||||
@@ -142,6 +142,13 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
|
||||
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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user