"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerFileHandlers = registerFileHandlers; const electron_1 = require("electron"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const axios_1 = __importDefault(require("axios")); const file_utils_1 = require("./file_utils"); let endpoints_in_progress = []; function registerFileHandlers() { function get_legacy_hashed_path(root, hash) { return path.join((0, file_utils_1.expandPath)(root), `${hash}.file`); } electron_1.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); }); electron_1.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 (0, axios_1.default)({ 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((resolve, reject) => { writer.on('finish', () => resolve()); writer.on('error', reject); }); fs.renameSync(tmp_path, full_path); return { success: true, path: full_path }; } catch (error) { 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); } }); electron_1.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 = (0, file_utils_1.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 electron_1.shell.openPath(target); return { success: true }; } catch (error) { return { success: false, error: error.message }; } }); } //# sourceMappingURL=file_handlers.js.map