More work on rewrite version 3. Focus on caching and opening after download.

This commit is contained in:
Scott Idem
2022-03-09 17:41:39 -05:00
parent 552be26831
commit c0f3f7a8ca
3 changed files with 182 additions and 130 deletions

View File

@@ -71,12 +71,87 @@ exports.load_config = function () {
}
// 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 cache already exists: ${hash_file_cache_path}`);
return true;
} else {
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-07
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}`);
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 false;
})
// let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename);
// console.log(result);
// return true;
console.log('End: open_hash_file_to_temp()');
}
// 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: check_file_cache() ***');
console.log('Checking the local file cache against the remote server.');
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}`);
event_file_id; // NOTE: This is the event_file.id_random or event_file.event_file_id_random
// 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);
@@ -123,29 +198,16 @@ exports.check_file_cache = async function ({host_file_cache_path, event_file_id,
}
}
/* Updated 2022-03-07 */
// function open_local_file({hash, host_file_cache_path, 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(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;
}
// 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('Checking the local file cache against the remote server.');
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}`);
event_file_id; // NOTE: This is the event_file.id_random or event_file.event_file_id_random
// 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);
@@ -192,8 +254,12 @@ async function check_file_cache({host_file_cache_path, event_file_id, hash}) {
}
}
// 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);
@@ -211,11 +277,20 @@ exports.check_file_cache_and_open_local_file = async function ({host_file_cache_
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;
})