Files

201 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

const { app, BrowserWindow, ipcMain, clipboard, Menu, desktopCapturer, screen } = require('electron');
const path = require('path');
const fs = require('fs');
const Database = require('./src/db/database');
// Force userData path to project root/data folder
// Prevents Electron cache/localStorage/logs from writing to C drive
app.setPath('userData', path.join(process.cwd(), 'data'));
let mainWindow;
let db;
let backupInterval;
// Auto backup feature
function startAutoBackup() {
const backupDir = path.join(process.cwd(), 'DB');
const dbPath = path.join(process.cwd(), 'accounts.db');
// Ensure backup directory exists
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
// Backup every 30 minutes
backupInterval = setInterval(() => {
try {
if (fs.existsSync(dbPath)) {
const now = new Date();
const timestamp = now.getFullYear().toString() +
(now.getMonth() + 1).toString().padStart(2, '0') +
now.getDate().toString().padStart(2, '0') + '_' +
now.getHours().toString().padStart(2, '0') +
now.getMinutes().toString().padStart(2, '0') +
now.getSeconds().toString().padStart(2, '0');
const backupPath = path.join(backupDir, `accounts_${timestamp}.db`);
fs.copyFileSync(dbPath, backupPath);
console.log(`[Backup] Auto backup success: ${backupPath}`);
// Clean old backups, keep last 10
cleanOldBackups(backupDir, 10);
}
} catch (e) {
console.error('[Backup] Backup failed:', e);
}
}, 30 * 60 * 1000); // 30 minutes
console.log('[Backup] Auto backup started, interval 30 minutes');
}
// Clean old backups
function cleanOldBackups(backupDir, keepCount) {
try {
const files = fs.readdirSync(backupDir)
.filter(f => f.startsWith('accounts_') && f.endsWith('.db'))
.map(f => ({ name: f, path: path.join(backupDir, f), time: fs.statSync(path.join(backupDir, f)).mtime }))
.sort((a, b) => b.time - a.time);
if (files.length > keepCount) {
files.slice(keepCount).forEach(f => {
fs.unlinkSync(f.path);
console.log(`[Backup] Deleted old backup: ${f.name}`);
});
}
} catch (e) {
console.error('[Backup] Clean old backups failed:', e);
}
}
function createWindow() {
// Remove application menu bar
Menu.setApplicationMenu(null);
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 900,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: false
},
backgroundColor: '#0a0a0b',
titleBarStyle: 'default',
show: false
});
mainWindow.loadFile('src/index.html');
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// DevTools: opt-in only (prevents auto-opening on startup)
if (process.env.ELECTRON_DEVTOOLS === '1') {
mainWindow.webContents.openDevTools();
}
}
app.whenReady().then(() => {
db = new Database();
createWindow();
startAutoBackup();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (backupInterval) {
clearInterval(backupInterval);
}
if (process.platform !== 'darwin') {
app.quit();
}
});
// ==================== Vault IPC Handlers ====================
ipcMain.handle('db:getVaults', async () => {
return db.getVaults();
});
ipcMain.handle('db:addVault', async (event, vault) => {
return db.addVault(vault);
});
ipcMain.handle('db:updateVault', async (event, id, vault) => {
return db.updateVault(id, vault);
});
ipcMain.handle('db:deleteVault', async (event, id) => {
return db.deleteVault(id);
});
// ==================== Account IPC Handlers ====================
ipcMain.handle('db:getAccounts', async (event, page, limit, vaultId) => {
return db.getAccounts(page, limit, vaultId);
});
ipcMain.handle('db:getAccountCount', async (event, vaultId) => {
return db.getAccountCount(vaultId);
});
ipcMain.handle('db:addAccount', async (event, account) => {
return db.addAccount(account);
});
ipcMain.handle('db:updateAccount', async (event, id, account) => {
return db.updateAccount(id, account);
});
ipcMain.handle('db:deleteAccount', async (event, id) => {
return db.deleteAccount(id);
});
ipcMain.handle('db:moveAccountToVault', async (event, accountId, vaultId) => {
return db.moveAccountToVault(accountId, vaultId);
});
ipcMain.handle('db:searchAccounts', async (event, query, vaultId) => {
return db.searchAccounts(query, vaultId);
});
ipcMain.handle('clipboard:write', async (event, text) => {
clipboard.writeText(text);
return true;
});
// Screen capture feature
ipcMain.handle('screen:capture', async () => {
try {
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.size;
const scaleFactor = primaryDisplay.scaleFactor;
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: { width: width * scaleFactor, height: height * scaleFactor }
});
if (sources.length > 0) {
const thumbnail = sources[0].thumbnail;
return {
success: true,
image: thumbnail.toDataURL(),
width: thumbnail.getSize().width,
height: thumbnail.getSize().height
};
}
return { success: false, error: 'Failed to capture screen' };
} catch (e) {
console.error('Screen capture failed:', e);
return { success: false, error: e.message };
}
});