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

@@ -20,27 +20,27 @@
<!-- One Sky IT default custom Cascading Style Sheets (CSS) -->
<!-- One Sky IT site custom Cascading Style Sheets (CSS) -->
<link rel="stylesheet" href="https://cmsc.oneskyit.com/static/css/aether_variables.css">
<!-- <link rel="stylesheet" href="http://dev.oneskyit.local:5000/static/css/aether_variables.css"> -->
<link rel="stylesheet" href="https://cmsc.oneskyit.com/static/svelte/build/aether_utilities.css">
<!-- <link rel="stylesheet" href="http://dev.oneskyit.local:5000/static/svelte/build/aether_utilities.css"> -->
<link rel="stylesheet" href="https://cmsc.oneskyit.com/static/css/aether_system.css">
<!-- <link rel="stylesheet" href="http://dev.oneskyit.local:5000/static/css/aether_system.css"> -->
<link rel="stylesheet" href="https://cmsc.oneskyit.com/static/svelte/build/aether_modules_core.css">
<!-- <link rel="stylesheet" href="http://dev.oneskyit.local:5000/static/svelte/build/aether_modules_core.css"> -->
<!-- <link rel="stylesheet" href="https://lci.oneskyit.com/static/svelte/build/aether_css_variables.css"> -->
<link rel="stylesheet" href="http://demo.oneskyit.local:5000/static/svelte/build/aether_css_variables.css">
<!-- <link rel="stylesheet" href="https://lci.oneskyit.com/static/svelte/build/aether_utilities.css"> -->
<link rel="stylesheet" href="http://demo.oneskyit.local:5000/static/svelte/build/aether_utilities.css">
<!-- <link rel="stylesheet" href="https://lci.oneskyit.com/static/css/aether_system.css"> -->
<link rel="stylesheet" href="http://demo.oneskyit.local:5000/static/css/aether_system.css">
<!-- <link rel="stylesheet" href="https://lci.oneskyit.com/static/svelte/build/aether_modules_core.css"> -->
<link rel="stylesheet" href="http://demo.oneskyit.local:5000/static/svelte/build/aether_modules_core.css">
<link href="https://cmsc.oneskyit.com/static/svelte/build/bundle.css" rel="stylesheet">
<!-- <link href="http://dev.oneskyit.local:5000/static/svelte/build/bundle.css" rel="stylesheet"> -->
<link href="https://cmsc.oneskyit.com/static/svelte/build/reloading.css" rel="stylesheet">
<!-- <link href="http://dev.oneskyit.local:5000/static/svelte/build/reloading.css" rel="stylesheet"> -->
<!-- <link href="https://lci.oneskyit.com/static/svelte/build/bundle.css" rel="stylesheet"> -->
<link href="http://demo.oneskyit.local:5000/static/svelte/build/bundle.css" rel="stylesheet">
<!-- <link href="https://lci.oneskyit.com/static/svelte/build/reloading.css" rel="stylesheet"> -->
<link href="http://demo.oneskyit.local:5000/static/svelte/build/reloading.css" rel="stylesheet">
<link href="https://cmsc.oneskyit.com/static/svelte/build/base_style_grid_layout_v3.css" rel="stylesheet">
<!-- <link href="http://dev.oneskyit.local:5000/static/svelte/build/base_style_grid_layout_v3.css" rel="stylesheet"> -->
<link href="https://cmsc.oneskyit.com/static/svelte/build/base_style_grid_theme_v3.css" rel="stylesheet">
<!-- <link href="http://dev.oneskyit.local:5000/static/svelte/build/base_style_grid_theme_v3.css" rel="stylesheet"> -->
<!-- <link href="https://lci.oneskyit.com/static/svelte/build/base_style_grid_layout_v3.css" rel="stylesheet"> -->
<link href="http://demo.oneskyit.local:5000/static/svelte/build/base_style_grid_layout_v3.css" rel="stylesheet">
<!-- <link href="https://lci.oneskyit.com/static/svelte/build/base_style_grid_theme_v3.css" rel="stylesheet"> -->
<link href="http://demo.oneskyit.local:5000/static/svelte/build/base_style_grid_theme_v3.css" rel="stylesheet">
<link href="https://cmsc.oneskyit.com/static/svelte/build/event_launcher.css" rel="stylesheet">
<!-- <link href="http://dev.oneskyit.local:5000/static/svelte/build/event_launcher.css" rel="stylesheet"> -->
<!-- <link href="https://lci.oneskyit.com/static/svelte/build/event_launcher.css" rel="stylesheet"> -->
<link href="http://demo.oneskyit.local:5000/static/svelte/build/event_launcher.css" rel="stylesheet">
<link rel="stylesheet" href="css/aether_native_app_v3.css">
@@ -65,46 +65,88 @@
<script>
const app = require('./js/aether_native_app_v3');
let app_config = app.load_config();
console.log(app_config);
console.log('Native App Config:', app_config);
const flask_env = 'development'; // 'development', 'production'
const env = 'development'; // 'development', 'production'
const app_env = 'development_local'; // 'development_local', 'development_remote'
const api_env = 'development_local'; // 'development_local', 'development_remote'
const app_mode = 'native'; // null, 'default', 'onsite', 'native'
// BEGIN: Environment constants set by server when rendering HTML
document.aether = {
name: 'Aether Native App (Electron and Svelte)'
};
// Environment options: development_local, development_remote, production_local, production_remote,
// const api_base_url = 'http://dev-fastapi.oneskyit.local:5005';
// const fastapi_base_url = 'http://dev-fastapi.oneskyit.local:5005';
// const api_temporary_token = 'viOhiwXybrM';
// const fastapi_temporary_token = 'viOhiwXybrM';
// END: Environment constants set by server when rendering HTML
// BEGIN: Content constants set by server when rendering HTML
const page_for = {"event": app_config.event_id, "event_device": app_config.event_device_id, "event_location": app_config.event_location_id, "event_session": app_config.event_session_id}; // Simple key value like object
console.log('Page For:', page_for);
// END: Content constants set by server when rendering HTML
// BEGIN: Client constants set by server when rendering HTML
const client_account_jwt = {}; // Future use
const client_person_jwt = {}; // Future use
const client_user_jwt = {}; // Future use
let client_account_id = app_config.account_id;
console.log(client_account_id);
let event_id = app_config.event_id;
let event_device_id = app_config.event_device_id;
let event_location_id = app_config.event_location_id;
const page_for = { 'event': event_id, 'event_device': event_device_id, 'event_location': event_location_id }; // Simple key value like object
console.log(page_for);
// END: Client constants set by server when rendering HTML
const host_file_cache_path = app_config.host_file_cache_path; // 'file_cache/'
// BEGIN: Other constants set by server when rendering HTML
// END: Other constants set by server when rendering HTML
let page = {}
page['page_for'] = { 'event': app_config.event_id, 'event_device': app_config.event_device_id, 'event_location': app_config.event_location_id, "event_session": app_config.event_session_id }; // Simple key value like object;
// page['current_url_root'] = 'http://demo.oneskyit.local:5000/';
// page['current_url_full_path'] = '/event/pjrcghqwert/launcher/x7uFNCMEdOI?';
document.aether.page = page;
document.aether.client = {"account_id": app_config.account_id, "site_id": app_config.site_id, "site_domain_id": app_config.site_domain_id, "person_id": null, "user_id": null, "order_cart_id_random": null, "super_check": false, "manager_check": false, "administrator_check": false, "support_check": false, "assistant_check": false, "trusted_check": false, "verified_check": false, "provisional_check": false, "public_check": false, "user_check": false, "logged_in_check": false, "person_check": false, "authenticated_check": false, "anonymous_check": true, "person_group": null, "user_group": null, "orders_closed_count": 0, "order_count": 0, "app_mode": "native"};
document.aether.cfg = {};
document.aether.cfg.app = {"name": "One Sky IT's Aether Native App DEV", "version": "3.5 DEV", "email": null, "env": null, "mode": "native", "host_file_cache_path": app_config.host_file_cache_path, "host_file_temp_path": app_config.host_file_temp_path};
document.aether.cfg.api = {"protocol": app_config.api_protocol, "server": app_config.api_server, "port": app_config.api_port, "path": app_config.api_path, "secret_key": app_config.api_secret_key, "base_url": null, "temporary_token": {"token": null, "expire_on": null}, "protocol_backup": app_config.api_protocol_backup, "server_backup": app_config.api_server_backup, "port_backup": app_config.api_port_backup, "path_backup": app_config.api_path_backup, "secret_key_backup": app_config.api_secret_key_backup, "base_url_backup": null, "temporary_token_backup": {"token": null, "expire_on": null}};
document.aether.cfg.idb = {"name": app_config.idb_name};
// const flask_env = 'development'; // 'development', 'production'
// const env = 'development'; // 'development', 'production'
// const app_env = 'development_local'; // 'development_local', 'development_remote'
// const api_env = 'development_local'; // 'development_local', 'development_remote'
// const app_mode = 'native'; // null, 'default', 'native', 'onsite'
// let event_id = app_config.event_id;
// let event_device_id = app_config.event_device_id;
// let event_location_id = app_config.event_location_id;
// const host_file_cache_path = app_config.host_file_cache_path; // 'file_cache/'
// console.log(host_file_cache_path);
const host_file_temp_path = app_config.host_file_temp_path; // 'temp/'
// const host_file_temp_path = app_config.host_file_temp_path; // 'temp/'
// console.log(host_file_temp_path);
let idb_name = app_config.idb_name;
// let idb_name = app_config.idb_name;
// BEGIN: API section
const access_control_allow_origin = app_config.access_control_allow_origin;
const api_secret_key = app_config.api_secret_key;
console.log(api_secret_key);
let api_temporary_token = null;
// const access_control_allow_origin = app_config.access_control_allow_origin;
// const api_secret_key = app_config.api_secret_key;
// console.log(api_secret_key);
// let api_temporary_token = null;
let api_server_base_url = app_config.api_server_base_url; // null;
let api_server_base_url_bak = app_config.api_server_base_url_bak;
let app_server_base_url = app_config.app_server_base_url;
// let api_base_url = app_config.api_base_url; // null;
// let api_base_url_bak = app_config.api_base_url_bak;
// let app_server_base_url = app_config.app_server_base_url;
// let file_base_url = app_config.file_server_base_url;
// if (app_config.use_local_api) {
// api_server_base_url = app_config.api_local_base_url; // 'http://api.localhost:5001'
// api_base_url = app_config.api_local_base_url; // 'http://api.localhost:5001'
// } else {
// api_server_base_url = app_config.api_remote_base_url; // 'https://api.oneskyit.com'
// api_base_url = app_config.api_remote_base_url; // 'https://api.oneskyit.com'
// }
/* ***** **** *** ** * ### * ** *** **** ***** */
@@ -119,8 +161,8 @@
</script>
<!-- <script defer src="svelte/build/bundle.js" crossorigin></script> -->
<script defer src="https://cmsc.oneskyit.com/static/svelte/build/bundle.js" crossorigin></script>
<!-- <script defer src="http://dev.oneskyit.local:5000/static/svelte/build/bundle.js" crossorigin></script> -->
<!-- <script defer src="https://lci.oneskyit.com/static/svelte/build/bundle.js" crossorigin></script> -->
<script defer src="http://demo.oneskyit.local:5000/static/svelte/build/bundle.js" crossorigin></script>
<!-- JavaScript (JS) end -->
</head>

View File

@@ -167,11 +167,13 @@ exports.check_local_file = async function ({local_file_path, filename}) {
// Check local hash file cache
// Used by Svelte Event Launcher
// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11
// Updated 2022-05-06
exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
console.log('*** Electron framework export: check_hash_file_cache() ***');
// console.log('*** Electron framework export: check_hash_file_cache() ***');
// console.log('Check local hash file cache');
console.log(`Host File Cache Path: ${host_file_cache_path}; Hash: ${hash}`);
console.log(`*** Electron framework export: check_hash_file_cache() *** Host File Cache Path: ${host_file_cache_path}; Hash: ${hash}`);
let hash_filename = `${hash}.file`;
@@ -187,7 +189,7 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
// console.log(hash_file_cache_path);
if (fs.existsSync(hash_file_cache_path)) {
console.log(`Hashed file exists in cache: ${hash_file_cache_path}`);
// console.log(`Hashed file exists in cache: ${hash_file_cache_path}`);
return true;
} else {
console.log(`Hashed file not found in cache: ${hash_file_cache_path}`);
@@ -197,11 +199,12 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
// Download hash file to cache
// Used by Svelte Event Launcher
// Updated 2022-05-06
exports.download_hash_file_to_cache = async function ({host_file_cache_path, event_file_id=null, hash=null}) {
console.log('*** Electron framework export: download_hash_file_to_cache() ***');
exports.download_hash_file_to_cache = async function ({api_base_url, host_file_cache_path, event_file_id=null, hash=null}) {
// console.log('*** Electron framework export: download_hash_file_to_cache() ***');
// console.log('Download hash file to cache');
console.log(`Host File Cache Path: ${host_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`);
console.log(`*** Electron framework export: download_hash_file_to_cache() *** Base URL: ${api_base_url}; Host File Cache Path: ${host_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`);
let endpoint = `/event/file/${event_file_id}/download`;
@@ -216,27 +219,42 @@ exports.download_hash_file_to_cache = async function ({host_file_cache_path, eve
}
let hash_file_cache_path = path.join(subdirectory_path, hash_filename);
// console.log(hash_file_cache_path);
if (fs.existsSync(hash_file_cache_path)) {
if (check_hash) {
const file_buffer = fs.readFileSync(hash_file_cache_path);
const file_hash_sha256 = crypto.createHash('sha256');
file_hash_sha256.update(file_buffer);
let download_file_result = await ipcRenderer.invoke('download_file', api_server_base_url, endpoint, hash_file_cache_path).then((result) => {
console.log('IPC download file process finished');
console.log(result);
return true;
});
// console.log(download_file_result);
// console.log('End: download_hash_file_to_cache()');
if (download_file_result) {
console.log('File downloaded successfully');
return true;
} else {
console.log('File was not downloaded successfully');
return false;
const file_hash_sha256_check = file_hash_sha256.digest('hex');S
if (file_hash_sha256_check == hash) {
console.log('File hash match', file_hash_sha256_check);
return true;
} else {
// This should only happen if the file is actively being downloaded or it is corrupt.
console.log('File hash does not match', file_hash_sha256_check);
return false;
}
}
}
// console.log(`!!!ABOUT TO CALL DOWNLOAD FILE HANDLER!!! exports.download_hash_file_to_cache(); Base URL: ${api_base_url}`);
let download_file_result = await ipcRenderer.invoke('download_file', api_base_url, endpoint, hash_file_cache_path).then((result) => {
if (result) {
console.log('IPC download file process finished successfully');
return true;
} else {
console.log('IPC Download Result:', result);
return null;
}
});
// console.log(`!!!DONE WITH DOWNLOAD FILE HANDLER!!! exports.download_hash_file_to_cache(); Base URL: ${api_base_url}`);
return download_file_result;
}
// Open cached hash file after copying to temp directory
// Used by Svelte Event Launcher
// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11
// Updated 2022-05-06
exports.open_hash_file_to_temp = async function ({host_file_cache_path, hash, host_file_temp_path, filename}) {
console.log('*** Electron framework export: open_hash_file_to_temp() ***');
@@ -273,6 +291,8 @@ exports.open_hash_file_to_temp = async function ({host_file_cache_path, hash, ho
// Open local file
// Used by Svelte Event Launcher
// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11
// Updated 2022-03-10
exports.open_local_file = async function ({local_file_path, filename}) {
console.log('*** Electron framework export: open_local_file() ***');
@@ -307,65 +327,65 @@ exports.open_local_file = async function ({local_file_path, filename}) {
}
// Check local file cache and download from server if needed.
// Updated 2022-03-09
// exports.check_file_cache = async function ({host_file_cache_path, event_file_id, hash}) {
exports.check_file_cache = async function ({host_file_cache_path, event_file_id, hash}) {
console.log('*** Electron framework export: check_file_cache() ***');
// console.log('Check local file cache and download from server if needed.');
console.log(`Host File Cache Path: ${host_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`);
// // Check local file cache and download from server if needed.
// // Updated 2022-03-09
// // exports.check_file_cache = async function ({host_file_cache_path, event_file_id, hash}) {
// exports.check_file_cache = async function ({api_base_url, host_file_cache_path, event_file_id, hash}) {
// console.log('*** Electron framework export: check_file_cache() ***');
// // console.log('Check local file cache and download from server if needed.');
// console.log(`Host File Cache Path: ${host_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`);
// NOTE: event_file_id is the event_file.id_random or event_file.event_file_id_random
let hash_filename = hash+'.file';
// // NOTE: event_file_id is the event_file.id_random or event_file.event_file_id_random
// let hash_filename = hash+'.file';
let save_path = path.join(host_file_cache_path, hash_filename);
console.log(save_path);
// let save_path = path.join(host_file_cache_path, hash_filename);
// console.log(save_path);
if (fs.existsSync(save_path)) {
console.log('Hashed file cache already exists: '+save_path);
return true;
} else {
console.log('Hashed file not found in local cache. Downloading file: '+save_path);
let endpoint = `/event/file/${event_file_id}/download`;
let result = await ipcRenderer.send('download_file', api_server_base_url, endpoint, save_path); // Must download file using main node.js thread.
console.log(result);
// if (fs.existsSync(save_path)) {
// console.log('Hashed file cache already exists: '+save_path);
// return true;
// } else {
// console.log('Hashed file not found in local cache. Downloading file: '+save_path);
// let endpoint = `/event/file/${event_file_id}/download`;
// let result = await ipcRenderer.send('download_file', api_base_url, endpoint, save_path); // Must download file using main node.js thread.
// console.log(result);
return new Promise((resolve, reject) => {
ipcRenderer.once('download_file_reply', function(event, response){
console.log(response);
return response;
})
resolve(true);
});
// return new Promise((resolve, reject) => {
// ipcRenderer.once('download_file_reply', function(event, response){
// console.log(response);
// return response;
// })
// resolve(true);
// });
// await ipcRenderer.once('download_file_reply', function(event, response){
// console.log(response);
// return response;
// });
// // await ipcRenderer.once('download_file_reply', function(event, response){
// // console.log(response);
// // return response;
// // });
// result.then(function (response) {
// console.log('Downloaded!!!???');
// return true;
// }).catch(function (error) {
// console.log(error);
// return false;
// });
// // result.then(function (response) {
// // console.log('Downloaded!!!???');
// // return true;
// // }).catch(function (error) {
// // console.log(error);
// // return false;
// // });
// return result;
// // return result;
// console.log(result);
// if (result) {
// return true;
// } else {
// return false;
// }
}
}
// // console.log(result);
// // if (result) {
// // return true;
// // } else {
// // return false;
// // }
// }
// }
// Check local file cache and download from server if needed. Must use IPC to Main to download file. Set a Promise to wait for download_file_reply.
// Updated 2022-03-09
async function check_file_cache({host_file_cache_path, event_file_id, hash}) {
async function check_file_cache({api_base_url, host_file_cache_path, event_file_id, hash}) {
console.log('*** Electron framework: check_file_cache() ***');
// console.log('Check local file cache and download from server if needed.');
console.log(`Host File Cache Path: ${host_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`);
@@ -382,7 +402,7 @@ async function check_file_cache({host_file_cache_path, event_file_id, hash}) {
} else {
console.log('Hashed file not found in local cache. Downloading file: '+save_path);
let endpoint = `/event/file/${event_file_id}/download`;
let result = await ipcRenderer.send('download_file', api_server_base_url, endpoint, save_path); // Must download file using main node.js thread.
let result = await ipcRenderer.send('download_file', api_base_url, endpoint, save_path); // Must download file using main node.js thread.
console.log(result);
return new Promise((resolve, reject) => {
@@ -436,6 +456,7 @@ async function open_local_file({host_file_cache_path, hash, host_file_temp_path,
}
// No longer needed? Not referenced as of 2022-10-11
exports.check_file_cache_and_open_local_file = async function ({host_file_cache_path, event_file_id, hash, host_file_temp_path, filename}) {
console.log('*** Electron framework: check_file_cache_and_open_local_file() ***');
console.log('Checking the local file cache against the remote server and then opening the local file.');

View File

@@ -9,11 +9,11 @@
"api_secret_key": "dFP6J9DVj9hUgIMn-fNIqg",
"api_server_base_url": "http://dev-api.oneskyit.local:5005",
"api_base_url": "http://dev-api.oneskyit.local:5005",
"app_server_base_url": "http://dev-demo.oneskyit.local:5000",
"file_server_base_url": "",
"api_server_base_url_bak": "https://dev-api.oneskyit.com",
"api_base_url_bak": "https://dev-api.oneskyit.com",
"app_server_base_url_bak": "https://dev-demo.oneskyit.com",
"file_server_base_url_bak": "",

View File

@@ -13,11 +13,11 @@
"api_secret_key": "ABCD1234XYZ",
"api_server_base_url": "http://dev-api.oneskyit.local:5005",
"api_base_url": "http://dev-api.oneskyit.local:5005",
"app_server_base_url": "http://dev-demo.oneskyit.local:5000",
"file_server_base_url": "",
"api_server_base_url_bak": "https://dev-api.oneskyit.com",
"api_base_url_bak": "https://dev-api.oneskyit.com",
"app_server_base_url_bak": "https://dev-demo.oneskyit.com",
"file_server_base_url_bak": "",

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;
});

886
package-lock.json generated

File diff suppressed because it is too large Load Diff