'use strict'; const os = require('os'); const path = require('path'); const fs = require('fs'); const { ipcRenderer } = require('electron'); exports.load_config = function () { console.log('CWD: '+process.cwd()); let home_directory = require('os').homedir(); console.log('Home: '+home_directory); let tmp_directory = require('os').tmpdir(); console.log('Temporary: '+tmp_directory); let config = null; let config_directory = null; let default_config_path = path.join(process.cwd(),'config.json.default'); let config_path = null; if (os.platform == 'darwin') { config_directory = path.join(home_directory, 'Library/Application Support/OSIT'); console.log('macOS config directory: '+config_directory); } else if (os.platform == 'linux') { config_directory = path.join(home_directory, '.config/OSIT'); console.log('Linux config directory: '+config_directory); } if (fs.existsSync(config_directory)) { console.log('Config: '+config_directory); config_path = path.join(config_directory, 'config.json'); } else { fs.mkdirSync(config_directory); console.log('Config directory created: '+config_directory); //default_config_path = path.join(process.cwd(),'config.json.default'); config_path = path.join(config_directory, 'config.json'); fs.copyFileSync(default_config_path, config_path); console.log('Default config file copied: '+config_directory); } if (fs.existsSync(config_path)) { console.log('Config path: '+config_path); console.log('Config file (config.json) found under '+config_directory+'.'); config = JSON.parse(fs.readFileSync(config_path)); console.log('Config file read.'); } else if (!fs.existsSync(config_path)) { fs.copyFileSync(default_config_path, config_path); console.log('Default config file copied: '+config_directory); config = JSON.parse(fs.readFileSync(config_path)); console.log('Config file read.'); } else if (fs.existsSync('config.json')) { //fs.copyFileSync(default_config_path, config_path); //console.log('Default config file copied: '+config_directory); config = JSON.parse(fs.readFileSync('config.json')); console.log('Config file (config.json) not found under '+config_directory+'. Using config in CWD.'); console.log('Config file read.'); //console.log('Config file (config.json) not found under '+config_directory+'. Using config in CWD.'); //config = JSON.parse(fs.readFileSync('config.json')); } else { //close(); } //console.log(config); return config; } exports.check_file_cache = async function (host_file_cache_path, event_file_id, hash) { console.log('**** *** ** * FUNCTION: check_file_cache (v3) * ** *** ****'); console.log('Checking the local file cache against the remote server.'); event_file_id; // NOTE: This 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); if (fs.existsSync(save_path)) { console.log('Hashed file cache already exists: '+save_path); } else { console.log('Hashed file not found in local cache. Downloading file: '+save_path); let endpoint = `/event/file/${event_file_id}/download`; ipcRenderer.send('download_file', api_base_url, endpoint, save_path); // Must download file using main node.js thread. } return true; } exports.check_file_cache_v1 = async function () { console.log('**** *** ** * FUNCTION: check_file_cache * ** *** ****'); console.log('Checking the local file cache against the remote server.'); if (api_base_url && api_temporary_token) { tbl_event_file.iterate(function(file_value, key, iteration) { //if (file_value.event_location_id == event_location_id) { //console.log('f: ('+file_value.event_file_id+') '+file_value.event_file_filename+' ***') let file_id = file_value.id; // NOTE: This is the event_file.id or event_file_id. let filename = file_value.hosted_file_hash_sha256+'.file'; let save_path = path.join(host_file_cache_path, filename); //console.log(save_path); if (fs.existsSync(save_path)) { //console.log('Local file already exists: '+save_path); } else { console.log('File not found locally. Downloading file: '+save_path); let api_endpoint = '/event/file/'+file_id+'/download'; ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread. } }); } else { console.log('The api_base_url or api_temporary_token has not been set.'); console.log(api_base_url); console.log(api_temporary_token); return false; } return true; } exports.currently_online = function() { //alert('You are currently online'); console.log('Currently online'); app_online = true; //document.getElementById('app_network_status').classList.remove('alert-info'); //document.getElementById('app_network_status').classList.remove('warning'); //document.getElementById('app_network_status').classList.remove('alert-warning'); //document.getElementById('app_network_status').classList.add('success'); //document.getElementById('app_network_status').classList.add('alert-success'); document.getElementById('app_network_status').classList.remove('app_warning'); //document.getElementById('app_network_status').classList.add('d-none'); document.getElementById('app_network_status').innerHTML = ' Currently Online'; //document.getElementById('app_network_status').innerHTML('Currently Online'); } exports.currently_offline = function() { //alert('You are currently offline'); console.log('Currently offline'); app_online = false; //document.getElementById('app_network_status').classList.remove('alert-info'); //document.getElementById('app_network_status').classList.remove('success'); //document.getElementById('app_network_status').classList.remove('alert-success'); //document.getElementById('app_network_status').classList.add('warning'); //document.getElementById('app_network_status').classList.add('alert-warning'); document.getElementById('app_network_status').classList.add('app_warning'); //document.getElementById('app_network_status').classList.remove('d-none'); document.getElementById('app_network_status').innerHTML = ' Currently Offline'; //document.getElementById('app_network_status').innerHTML('Currently Offline'); } //window.addEventListener('online', currently_online); //window.addEventListener('offline', currently_offline);