v2.9
This commit is contained in:
+67
-32
@@ -1,4 +1,4 @@
|
||||
const {app, BrowserWindow, screen, ipcMain, dialog} = require('electron');
|
||||
const {app, BrowserWindow, screen, ipcMain, dialog, shell} = require('electron');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
@@ -6,6 +6,7 @@ const {fork} = require('child_process');
|
||||
|
||||
const {encryptFileInPlace, decryptFileInPlace} = require('./aes_encrypt');
|
||||
const {lockFile, unlockFile} = require("../../helpers/lock_mechanism");
|
||||
const exec = require("nodemon/lib/config/exec");
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
let html_page = undefined;
|
||||
@@ -31,32 +32,11 @@ const createInitialKeys = () => {
|
||||
console.log(`IV saved to ${ivPath}`);
|
||||
}
|
||||
|
||||
const deleteMainComponentsAtErrorStart = () => {
|
||||
const ipConfigPath = path.join(__dirname, '..', '..', 'ipConfig.json');
|
||||
const loginDataPath = path.join(__dirname, '..', '..', 'loginData.json');
|
||||
|
||||
fs.unlink(ipConfigPath, (err) => {
|
||||
if (err) {
|
||||
console.error('Error deleting file:', err);
|
||||
return;
|
||||
}
|
||||
console.log('File deleted successfully: ' + 'ipConfig.json');
|
||||
});
|
||||
|
||||
fs.unlink(loginDataPath, (err) => {
|
||||
if (err) {
|
||||
console.error('Error deleting file:', err);
|
||||
return;
|
||||
}
|
||||
console.log('File deleted successfully: ' + 'loginData.json');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
const checkForServerConnection = async () => {
|
||||
const pathToIpConfig = path.join(__dirname, '..', '..', 'ipConfig.json');
|
||||
|
||||
try {
|
||||
console.log('Checking for server connection');
|
||||
await fs.promises.access(pathToIpConfig);
|
||||
|
||||
await lockFile(pathToIpConfig);
|
||||
@@ -71,10 +51,35 @@ const checkForServerConnection = async () => {
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error("Error:", error);
|
||||
try{
|
||||
fs.unlinkSync(pathToIpConfig);
|
||||
}
|
||||
catch{
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
async function runStartupChecks() {
|
||||
try {
|
||||
await fs.promises.access(path.join(__dirname, '..', '..', 'secret.key'), fs.constants.F_OK);
|
||||
await fs.promises.access(path.join(__dirname, '..', '..', 'iv.key'), fs.constants.F_OK);
|
||||
} catch (err) {
|
||||
console.error("Startup error detected:", err);
|
||||
|
||||
// Run the npm clean script
|
||||
await exec('npm run clean', { cwd: path.join(__dirname, '..', '..') }, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error('Error occurred while running npm run clean:', stderr);
|
||||
return;
|
||||
}
|
||||
console.log('npm run clean output:', stdout);
|
||||
createInitialKeys();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const createMainWindow = (async (title, width, height) => {
|
||||
mainWindow = new BrowserWindow({
|
||||
title: title,
|
||||
@@ -87,14 +92,7 @@ const createMainWindow = (async (title, width, height) => {
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await fs.promises.access(path.join(__dirname, '..', '..', 'secret.key'), fs.constants.F_OK);
|
||||
await fs.promises.access(path.join(__dirname, '..', '..', 'iv.key'), fs.constants.F_OK);
|
||||
} catch (err) {
|
||||
createInitialKeys();
|
||||
deleteMainComponentsAtErrorStart();
|
||||
}
|
||||
|
||||
await runStartupChecks();
|
||||
html_page = await checkForServerConnection() ? 'login.html' : 'ip_submit.html';
|
||||
|
||||
//mainWindow.setMenu(null);
|
||||
@@ -336,7 +334,7 @@ ipcMain.handle('start-main-processes', async (event, args) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (externalEndpointsProcess) {
|
||||
if (!externalEndpointsProcess) {
|
||||
externalEndpointsProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'external_endpoints.js'), args, { silent: false });
|
||||
externalEndpointsProcess.on('exit', () => {
|
||||
externalEndpointsProcess = null;
|
||||
@@ -385,3 +383,40 @@ ipcMain.handle('decrypt-backup-files', async(event, destPath) => {
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
// Handle showing a file in the system file explorer
|
||||
ipcMain.handle('show-file-in-explorer', async (event, filePath) => {
|
||||
try {
|
||||
// Ensure the file exists before attempting to show it
|
||||
await fs.promises.access(filePath, fs.constants.F_OK);
|
||||
shell.showItemInFolder(filePath); // Opens the file explorer and highlights the file
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('File does not exist:', error);
|
||||
return false
|
||||
}
|
||||
});
|
||||
|
||||
// Handle removing a path from 'filesReceived.json'
|
||||
ipcMain.handle('remove-path-from-received-files', async (event, filePath) => {
|
||||
try {
|
||||
const jsonFilePath = path.join(__dirname, '..', '..', 'filesReceived.json');
|
||||
await lockFile(jsonFilePath);
|
||||
await decryptFileInPlace(jsonFilePath);
|
||||
const data = await fs.promises.readFile(jsonFilePath, 'utf8');
|
||||
|
||||
const jsonData = JSON.parse(data);
|
||||
|
||||
// Filter out the specified file path
|
||||
jsonData.receivedFiles = jsonData.receivedFiles.filter(file => file !== filePath);
|
||||
|
||||
await fs.promises.writeFile(jsonFilePath, JSON.stringify(jsonData, null, 2), 'utf8');
|
||||
await encryptFileInPlace(jsonFilePath);
|
||||
await unlockFile(jsonFilePath);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error updating filesReceived.json:', error);
|
||||
throw new Error('Failed to update received files list.');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -14,5 +14,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
|
||||
startMainProcesses: async (args) => ipcRenderer.invoke('start-main-processes', args),
|
||||
killBeforeLogout: async() => ipcRenderer.invoke('kill-before-logout'),
|
||||
decryptFiles: async(destPath) => ipcRenderer.invoke('decrypt-backup-files', destPath)
|
||||
decryptFiles: async(destPath) => ipcRenderer.invoke('decrypt-backup-files', destPath),
|
||||
|
||||
showFileInExplorer: (filePath) => ipcRenderer.invoke('show-file-in-explorer', filePath),
|
||||
removePathFromReceivedFiles: (filePath) => ipcRenderer.invoke('remove-path-from-received-files', filePath)
|
||||
});
|
||||
Reference in New Issue
Block a user