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

This commit is contained in:
Scott Idem
2022-03-09 19:05:08 -05:00
parent c0f3f7a8ca
commit f0bf184a4a
2 changed files with 45 additions and 36 deletions

View File

@@ -81,7 +81,7 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, hash}) {
let hash_filename = `${hash}.file`; let hash_filename = `${hash}.file`;
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename); let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
console.log(hash_file_cache_path); // console.log(hash_file_cache_path);
if (fs.existsSync(hash_file_cache_path)) { if (fs.existsSync(hash_file_cache_path)) {
console.log(`Hashed file cache already exists: ${hash_file_cache_path}`); console.log(`Hashed file cache already exists: ${hash_file_cache_path}`);
@@ -102,11 +102,11 @@ exports.download_hash_file_to_cache = async function ({host_file_cache_path, eve
let hash_filename = `${hash}.file`; let hash_filename = `${hash}.file`;
let hash_file_cache_path = path.join(host_file_cache_path, hash_filename); let hash_file_cache_path = path.join(host_file_cache_path, hash_filename);
console.log(hash_file_cache_path); // 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) => { 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('IPC download file process finished');
console.log(result); // console.log(result);
return true; return true;
}); });
@@ -128,21 +128,30 @@ exports.open_hash_file_to_temp = async function ({host_file_cache_path, hash, ho
console.log('Open cached hash file after copying to temp directory'); 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}`); 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) => { 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('IPC open hash file to temp finished');
console.log(result); console.log(result);
return false; return true;
}) })
// let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename); // let result = await ipcRenderer.send('open_local_file', host_file_cache_path, hash, host_file_temp_path, filename);
// console.log(result); // console.log(result);
// return true; console.log(open_hash_file_to_temp_result);
console.log('End: open_hash_file_to_temp()'); 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;
}
} }
// Check local file cache and download from server if needed. // Check local file cache and download from server if needed.
// Updated 2022-03-09 // 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}) {

View File

@@ -126,7 +126,7 @@ async function download_file(api_base_url, api_endpoint, full_save_path) {
const url = api_endpoint; const url = api_endpoint;
let result = await axios({ let download_result = await axios({
method: 'get', method: 'get',
url: url, url: url,
responseType: 'stream' /* responseType must be stream */ responseType: 'stream' /* responseType must be stream */
@@ -135,32 +135,32 @@ async function download_file(api_base_url, api_endpoint, full_save_path) {
// response.data.pipe(fs.createWriteStream(full_save_path)); // response.data.pipe(fs.createWriteStream(full_save_path));
// return true; // return true;
response.data.pipe(fs.createWriteStream(full_save_path)); // response.data.pipe(fs.createWriteStream(full_save_path));
return true; // return true;
// const writer = fs.createWriteStream(full_save_path); const writer = fs.createWriteStream(full_save_path);
// console.log('Write stream created'); console.log('Write stream created');
// return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// response.data.pipe(writer); response.data.pipe(writer);
// let error = null; let error = null;
// writer.on('error', err => { writer.on('error', err => {
// console.log('Writer error!'); console.log('Writer error!');
// error = err; error = err;
// console.log(error); console.log(error);
// writer.close(); writer.close();
// reject(err); reject(err);
// }); });
// writer.on('close', () => { writer.on('close', () => {
// console.log('Writer close!'); console.log('Writer close!');
// if (!error) { if (!error) {
// resolve(true); resolve(true);
// } }
// //no need to call the reject here, as it will have been called in the //no need to call the reject here, as it will have been called in the
// //'error' stream; //'error' stream;
// }); });
// }); });
}) })
.catch(function (error) { .catch(function (error) {
console.log(`Error downloading! Endpoint: ${api_endpoint}`); console.log(`Error downloading! Endpoint: ${api_endpoint}`);
@@ -173,10 +173,10 @@ async function download_file(api_base_url, api_endpoint, full_save_path) {
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
}); });
console.log(result); console.log(download_result);
console.log('End: download_file()'); console.log('End: download_file()');
// return 'Return from download_file()'; // return 'Return from download_file()';
return result; return download_result;
} }
@@ -206,7 +206,7 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
console.log('Hashed file cache exists: '+full_cache_file_path); console.log('Hashed file cache exists: '+full_cache_file_path);
console.log('Copying file to temp: '+open_temp_file_path); console.log('Copying file to temp: '+open_temp_file_path);
try { try {
fs.copyFileSync(full_cache_file_path, open_temp_file_path); await fs.copyFileSync(full_cache_file_path, open_temp_file_path);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return false; return false;
@@ -219,7 +219,7 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
} }
try { try {
shell.openPath(open_temp_file_path); await shell.openPath(open_temp_file_path);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return false; return false;
@@ -227,6 +227,6 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
console.log(true); console.log(true);
console.log('End: Electron IPC Main: open_hash_file_to_temp()'); console.log('End: Electron IPC Main: open_hash_file_to_temp()');
return 'Return from Electron IPC Main open_hash_file_to_temp()'; // return 'Return from Electron IPC Main open_hash_file_to_temp()';
// return result; return true;
}); });