A lot of work on the launcher. Moving things from Electron node.js to Svelte.

This commit is contained in:
Scott Idem
2022-10-11 20:06:13 -04:00
parent 31203648b4
commit 3a4812de4c
6 changed files with 812 additions and 454 deletions

View File

@@ -55,7 +55,8 @@ function createWindow () {
})
// win.setMinimumSize(1024, 768);
win.setMinimumSize(1280, 768);
// win.setMinimumSize(1280, 768);
win.setMinimumSize(1400, 768);
//win.setFullScreenable(false)
win.FullScreenable = false;
@@ -66,7 +67,7 @@ function createWindow () {
win.loadFile('app/index.html');
// Open the DevTools.
// win.webContents.openDevTools(); // Comment out for production
win.webContents.openDevTools(); // Comment out for production
// Emitted when the window is closed.
win.on('closed', () => {
@@ -111,7 +112,7 @@ app.on('activate', () => {
// Updated 2022-04-16
ipcMain.handle('import_config', async (event, config_data) => {
console.log('*** Electron IPC Main: import_config() ***');
// console.log('ipcMain on download_file: api_server_base_url='+api_server_base_url+' | api_temporary_token='+api_temporary_token);
// console.log('ipcMain on download_file: api_base_url='+api_base_url+' | api_temporary_token='+api_temporary_token);
console.log('ipcMain on import_config:');
console.log(config_data);
@@ -141,28 +142,32 @@ ipcMain.handle('import_config', async (event, config_data) => {
// Download file to path
// full_save_path should be the full path that includes the filename
// Updated 2022-03-09
ipcMain.handle('download_file', async (event, api_server_base_url, api_endpoint, full_save_path) => {
ipcMain.handle('download_file', async (event, api_base_url, api_endpoint, full_save_path) => {
console.log('*** Electron IPC Main: download_file() ***');
// console.log('ipcMain on download_file: api_server_base_url='+api_server_base_url+' | api_temporary_token='+api_temporary_token);
console.log('ipcMain on download_file: api_server_base_url='+api_server_base_url);
// console.log('ipcMain on download_file: api_base_url='+api_base_url+' | api_temporary_token='+api_temporary_token);
console.log('ipcMain on download_file: api_base_url='+api_base_url);
console.log('ipcMain download and save file: HTTP '+api_endpoint+' -> FILE '+full_save_path);
if (!api_base_url) {
console.log('API Base URL is required. Returning false');
return false;
}
let result = await download_file(api_server_base_url, api_endpoint, full_save_path);
let result = await download_file(api_base_url, api_endpoint, full_save_path);
console.log(result);
// console.log(result);
console.log('End: Electron IPC Main: download_file()');
// return 'Return from Electron IPC Main download_file()';
return result;
});
// Download file to path
// full_save_path should be the full path that includes the filename
// Updated 2022-03-09
async function download_file(api_server_base_url, api_endpoint, full_save_path) {
// Updated 2022-10-11
async function download_file(api_base_url, api_endpoint, full_save_path) {
console.log('*** node.js: download_file() ***');
// console.log(api_base_url);
axios.defaults.baseURL = api_server_base_url;
axios.defaults.baseURL = api_base_url;
axios.defaults.headers.common['Access-Control-Allow-Origin'] = config.access_control_allow_origin; // '*'; // app_config.access_control_allow_origin;
axios.defaults.headers.common['content-type'] = 'application/json';
axios.defaults.headers.common['x-aether-api-key'] = config.api_secret_key; // 'dFP6J9DVj9hUgIMn-fNIqg'; // api_secret_key;
@@ -170,21 +175,21 @@ async function download_file(api_server_base_url, api_endpoint, full_save_path)
const url = api_endpoint;
let tmp_full_save_path = full_save_path+'.tmp';
if (fs.existsSync(tmp_full_save_path)) {
console.log('A temp file was found!');
return false;
} else {
console.log(`Downloading for real...`);
}
let download_result = await axios({
method: 'get',
url: url,
responseType: 'stream' /* responseType must be stream */
}).then(function (response) {
console.log('Downloading...');
// response.data.pipe(fs.createWriteStream(full_save_path));
// return true;
// response.data.pipe(fs.createWriteStream(full_save_path));
// return true;
const writer = fs.createWriteStream(full_save_path);
console.log('Write stream created');
console.log('Creating write stream to download file...');
const writer = fs.createWriteStream(tmp_full_save_path);
return new Promise((resolve, reject) => {
response.data.pipe(writer);
@@ -199,6 +204,8 @@ async function download_file(api_server_base_url, api_endpoint, full_save_path)
writer.on('close', () => {
console.log('Writer close!');
if (!error) {
fs.moveFileSync(tmp_full_save_path, full_save_path);
console.log('Temporary file moved/renamed: '+config_directory);
resolve(true);
}
//no need to call the reject here, as it will have been called in the
@@ -208,23 +215,25 @@ async function download_file(api_server_base_url, api_endpoint, full_save_path)
})
.catch(function (error) {
console.log(`Error downloading! Endpoint: ${api_endpoint}`);
// console.log(error);
// console.log(error.response);
if (error.response) {
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
} else {
console.log('Error:', error);
}
if (error.response && error.response.status === 404) {
return null; // Returning null since there were no results
}
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
});
console.log(download_result);
console.log('End: download_file()');
// return 'Return from download_file()';
// console.log(download_result);
// console.log('End: download_file()');
return download_result;
}
ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, hash, host_file_temp_path, filename) => {
ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, hash, host_file_temp_path, filename, check_hash) => {
console.log('*** Electron IPC Main: open_hash_file_to_temp() ***');
console.log('ipcMain on open_hash_file_to_temp');
console.log(`ipcMain open hash file from temp directory: ${host_file_cache_path} -> ${host_file_temp_path}/${filename}`);
@@ -254,12 +263,26 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
console.log(`Hashed file exists in cache: ${full_cache_file_path}`);
console.log(`Copying file to temp: ${open_temp_file_path}`);
try {
await fs.copyFileSync(full_cache_file_path, open_temp_file_path);
fs.copyFileSync(full_cache_file_path, open_temp_file_path);
} catch (error) {
console.error(error);
return false;
}
if (check_hash) {
const file_buffer = fs.readFileSync(full_cache_file_path);
const file_hash_sha256 = crypto.createHash('sha256');
file_hash_sha256.update(file_buffer);
const file_hash_sha256_check = file_hash_sha256.digest('hex');
if (file_hash_sha256_check == hash) {
console.log('File hash match', file_hash_sha256_check);
} else {
console.log('File hash does not match', file_hash_sha256_check);
return false;
}
}
// console.log('Creating file link: '+open_temp_file_path);
// fs.linkSync(full_cache_file_path, open_temp_file_path);
} else {
@@ -274,9 +297,7 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
return false;
}
console.log(true);
console.log('End: Electron IPC Main: open_hash_file_to_temp()');
// return 'Return from Electron IPC Main open_hash_file_to_temp()';
return true;
});