376 lines
14 KiB
JavaScript
376 lines
14 KiB
JavaScript
'use strict';
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
let config = null;
|
|
|
|
exports.load_config = function () {
|
|
console.log('*** Electron framework: load_config() ***');
|
|
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;
|
|
|
|
// Set the config path for macOS or Linux
|
|
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);
|
|
}
|
|
|
|
// Look for the config file and copy the default if not found.
|
|
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);
|
|
}
|
|
|
|
// Attempt to open the config file. The preferred location is based on the OS's 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();
|
|
}
|
|
|
|
let import_config_to_ipc_result = ipcRenderer.invoke('import_config', config).then((result) => {
|
|
console.log('IPC import config finished');
|
|
console.log(result);
|
|
return true;
|
|
})
|
|
|
|
//console.log(config);
|
|
return config;
|
|
}
|
|
|
|
|
|
// Check for local file
|
|
// Updated 2022-03-10
|
|
exports.check_local_file = async function ({local_file_path, filename}) {
|
|
console.log('*** Electron framework export: check_local_file() ***');
|
|
// console.log('Check for local file');
|
|
console.log(`Local File Path: ${local_file_path}; Filename: ${filename}`);
|
|
|
|
let full_local_file_path = path.join(local_file_path, filename);
|
|
console.log(full_local_file_path);
|
|
|
|
if (fs.existsSync(full_local_file_path)) {
|
|
console.log(`Local file exists: ${full_local_file_path}`);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// Check local hash file cache
|
|
// Updated 2022-03-09
|
|
exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
|
|
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}`);
|
|
|
|
let hash_filename = `${hash}.file`;
|
|
|
|
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
|
|
// console.log(hash_file_cache_path);
|
|
|
|
if (fs.existsSync(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}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// Download hash file to cache
|
|
// Updated 2022-03-09
|
|
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() ***');
|
|
// 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}`);
|
|
|
|
let endpoint = `/event/file/${event_file_id}/download`;
|
|
|
|
let hash_filename = `${hash}.file`;
|
|
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
|
|
// console.log(hash_file_cache_path);
|
|
|
|
let download_file_result = await ipcRenderer.invoke('download_file', api_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;
|
|
}
|
|
}
|
|
|
|
|
|
// Open cached hash file after copying to temp directory
|
|
// Updated 2022-03-09
|
|
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() ***');
|
|
// console.log('Open cached hash file after copying to temp directory');
|
|
console.log(`Host File Cache Path: ${host_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`);
|
|
|
|
let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', host_file_cache_path, hash, host_file_temp_path, filename).then((result) => {
|
|
console.log('IPC open hash file to temp finished');
|
|
console.log(result);
|
|
return true;
|
|
})
|
|
|
|
// let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename);
|
|
// console.log(result);
|
|
|
|
console.log(open_hash_file_to_temp_result);
|
|
console.log('End: open_hash_file_to_temp()');
|
|
if (open_hash_file_to_temp_result) {
|
|
console.log('File opened successfully');
|
|
return true;
|
|
} else {
|
|
console.log('File was not opened successfully');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// Open local file
|
|
// Updated 2022-03-10
|
|
exports.open_local_file = async function ({local_file_path, filename}) {
|
|
console.log('*** Electron framework export: open_local_file() ***');
|
|
// console.log('Open local file');
|
|
console.log(`Local File Path: ${local_file_path}; Filename: ${filename}`);
|
|
|
|
// let full_local_file_path = path.join(local_file_path, filename);
|
|
// console.log(full_local_file_path);
|
|
|
|
// if (fs.existsSync(full_local_file_path)) {
|
|
// console.log(`Local file exists: ${full_local_file_path}`);
|
|
// // return true;
|
|
// } else {
|
|
// return false;
|
|
// }
|
|
|
|
let open_local_file_result = await ipcRenderer.invoke('open_local_file', local_file_path, filename).then((result) => {
|
|
console.log('IPC open local file finished');
|
|
console.log(result);
|
|
return true;
|
|
})
|
|
|
|
console.log(open_local_file_result);
|
|
console.log('End: open_local_file()');
|
|
if (open_local_file_result) {
|
|
console.log('File opened successfully');
|
|
return true;
|
|
} else {
|
|
console.log('File was not opened successfully');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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}`);
|
|
|
|
// 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);
|
|
|
|
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);
|
|
});
|
|
|
|
// 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;
|
|
// });
|
|
|
|
// return result;
|
|
|
|
// 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}) {
|
|
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}`);
|
|
|
|
// 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);
|
|
|
|
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);
|
|
});
|
|
|
|
// 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;
|
|
// });
|
|
|
|
// return result;
|
|
|
|
// console.log(result);
|
|
// if (result) {
|
|
// return true;
|
|
// } else {
|
|
// return false;
|
|
// }
|
|
}
|
|
}
|
|
|
|
// IPC to Main: Open local file cache if available. Copy to temp directory with given filename first.
|
|
// Updated 2022-03-09
|
|
async function open_local_file({host_file_cache_path, hash, host_file_temp_path, filename}) {
|
|
console.log('*** Electron framework: open_local_file() ***');
|
|
// console.log('Open local file cache if available. Copy to temp directory with given filename first.');
|
|
console.log(`Host File Cache Path: ${host_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`);
|
|
|
|
console.log(host_file_cache_path);
|
|
console.log(hash);
|
|
console.log(filename);
|
|
|
|
let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename);
|
|
console.log(result);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
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.');
|
|
|
|
let check_file_cache_result = check_file_cache({host_file_cache_path: host_file_cache_path, event_file_id: event_file_id, hash: hash});
|
|
console.log(check_file_cache_result);
|
|
|
|
if (check_file_cache_result) {
|
|
let open_local_file_result = open_local_file({host_file_cache_path: host_file_cache_path, hash: hash, host_file_temp_path: host_file_temp_path, filename: filename});
|
|
console.log(open_local_file_result);
|
|
|
|
return open_local_file_result;
|
|
}
|
|
|
|
ipcRenderer.once('download_file_reply', function(event, response){
|
|
console.log(response);
|
|
|
|
let open_local_file_result = open_local_file({host_file_cache_path: host_file_cache_path, hash: hash, host_file_temp_path: host_file_temp_path, filename: filename});
|
|
console.log(open_local_file_result);
|
|
|
|
return open_local_file_result;
|
|
})
|
|
|
|
|
|
|
|
} |