Fix: Standardize Electron bridge and implement robust caching
- Refactored all IPC methods and parameters to snake_case for consistency with SvelteKit. - Implemented exhaustive background caching engine with download locking. - Reverted to legacy-proven flat hash storage pattern (hash.file). - Added axios for reliable stream-based binary downloads. - Updated preload and main handlers to support recursive room data fetching.
This commit is contained in:
70
src/main/file_handlers.ts
Normal file
70
src/main/file_handlers.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { ipcMain, shell } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import axios from 'axios';
|
||||
import { expandPath } from './file_utils';
|
||||
|
||||
let endpoints_in_progress: string[] = [];
|
||||
|
||||
export function registerFileHandlers() {
|
||||
function get_legacy_hashed_path(root: string, hash: string) {
|
||||
return path.join(expandPath(root), `${hash}.file`);
|
||||
}
|
||||
|
||||
ipcMain.handle('native:check-cache', async (event, { cache_root, hash }) => {
|
||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
||||
return fs.existsSync(full_path);
|
||||
});
|
||||
|
||||
ipcMain.handle('native:download-to-cache', async (event, { url, cache_root, hash, api_key, account_id }) => {
|
||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
||||
const tmp_path = `${full_path}.tmp`;
|
||||
|
||||
if (endpoints_in_progress.includes(url)) return { success: true, status: 'in_progress' };
|
||||
if (fs.existsSync(full_path)) return { success: true, path: full_path, status: 'exists' };
|
||||
|
||||
console.log(`Native: Downloading ${hash} -> ${full_path}`);
|
||||
|
||||
try {
|
||||
endpoints_in_progress.push(url);
|
||||
const response = await axios({
|
||||
method: 'get', url, responseType: 'stream',
|
||||
headers: {
|
||||
'x-aether-api-key': api_key,
|
||||
'x-account-id': account_id || '',
|
||||
'x-no-account-id': 'Nothing to See Here'
|
||||
}
|
||||
});
|
||||
|
||||
const writer = fs.createWriteStream(tmp_path);
|
||||
response.data.pipe(writer);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writer.on('finish', () => resolve());
|
||||
writer.on('error', reject);
|
||||
});
|
||||
|
||||
fs.renameSync(tmp_path, full_path);
|
||||
return { success: true, path: full_path };
|
||||
} catch (error: any) {
|
||||
if (fs.existsSync(tmp_path)) fs.unlinkSync(tmp_path);
|
||||
return { success: false, error: error.message };
|
||||
} finally {
|
||||
endpoints_in_progress = endpoints_in_progress.filter(e => e !== url);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('native:launch-from-cache', async (event, { cache_root, hash, temp_root, filename }) => {
|
||||
try {
|
||||
const source = get_legacy_hashed_path(cache_root, hash);
|
||||
const expanded_temp = expandPath(temp_root);
|
||||
const target = path.join(expanded_temp, filename);
|
||||
if (!fs.existsSync(expanded_temp)) fs.mkdirSync(expanded_temp, { recursive: true });
|
||||
fs.copyFileSync(source, target);
|
||||
await shell.openPath(target);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user