Working on rewrite version 3. Focus on caching and opening after download.

This commit is contained in:
Scott Idem
2022-03-08 18:30:49 -05:00
parent 1d7c14dedb
commit 552be26831
7 changed files with 357 additions and 49 deletions

View File

@@ -1,20 +1,225 @@
'use strict';
const os = require('os');
const path = require('path');
const fs = require('fs');
const { ipcRenderer } = require('electron');
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;
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}) {
exports.check_file_cache = async function ({host_file_cache_path, event_file_id, hash}) {
console.log('*** Electron framework: check_file_cache() ***');
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);
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;
// }
}
}
/* Updated 2022-03-07 */
// function open_local_file({hash, host_file_cache_path, filename}) {
function open_local_file({host_file_cache_path, hash, filename}) {
exports.open_local_file = async function ({host_file_cache_path, hash, host_file_temp_path, filename}) {
console.log('*** Electron framework: open_local_file() ***');
console.log('*** open_local_file() ***');
// hash = this.getAttribute('data-hash_sha256');
// let file_path = path.join(host_file_cache_path, hash);
// filename = this.getAttribute('data-filename');
console.log(host_file_cache_path);
console.log(hash);
// console.log(file_path);
console.log(filename);
// ipcRenderer.send('open_local_file', file_path, filename);
ipcRenderer.send('open_local_file', host_file_cache_path, hash, filename);
let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename);
console.log(result);
return true;
}
async function check_file_cache({host_file_cache_path, event_file_id, hash}) {
console.log('*** Electron framework: check_file_cache() ***');
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);
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;
// }
}
}
async function open_local_file({host_file_cache_path, hash, host_file_temp_path, filename}) {
console.log('*** Electron framework: open_local_file() ***');
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});
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});
return open_local_file_result;
})
}