From f0bf184a4ad957f7cf608520a5161863313fe778 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Wed, 9 Mar 2022 19:05:08 -0500 Subject: [PATCH] More work on major rewrite version 3. Focus on caching and opening after download. --- app/js/app_v3.js | 21 ++++++++++++----- index.js | 60 ++++++++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/app/js/app_v3.js b/app/js/app_v3.js index 4c9ccc6..82b32e8 100644 --- a/app/js/app_v3.js +++ b/app/js/app_v3.js @@ -81,7 +81,7 @@ exports.check_hash_file_cache = async function ({host_file_cache_path, 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); + // console.log(hash_file_cache_path); if (fs.existsSync(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_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) => { console.log('IPC download file process finished'); - console.log(result); + // console.log(result); 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(`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(result); - return false; + return true; }) // 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(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; + } } + + // 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}) { diff --git a/index.js b/index.js index aeba27d..010d475 100644 --- a/index.js +++ b/index.js @@ -126,7 +126,7 @@ async function download_file(api_base_url, api_endpoint, full_save_path) { const url = api_endpoint; - let result = await axios({ + let download_result = await axios({ method: 'get', url: url, 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)); // return true; - response.data.pipe(fs.createWriteStream(full_save_path)); - return true; + // response.data.pipe(fs.createWriteStream(full_save_path)); + // 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) => { - // response.data.pipe(writer); - // let error = null; - // writer.on('error', err => { - // console.log('Writer error!'); - // error = err; - // console.log(error); - // writer.close(); - // reject(err); - // }); - // writer.on('close', () => { - // console.log('Writer close!'); - // if (!error) { - // resolve(true); - // } - // //no need to call the reject here, as it will have been called in the - // //'error' stream; - // }); - // }); + return new Promise((resolve, reject) => { + response.data.pipe(writer); + let error = null; + writer.on('error', err => { + console.log('Writer error!'); + error = err; + console.log(error); + writer.close(); + reject(err); + }); + writer.on('close', () => { + console.log('Writer close!'); + if (!error) { + resolve(true); + } + //no need to call the reject here, as it will have been called in the + //'error' stream; + }); + }); }) .catch(function (error) { 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. }); - console.log(result); + console.log(download_result); console.log('End: 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('Copying file to temp: '+open_temp_file_path); try { - fs.copyFileSync(full_cache_file_path, open_temp_file_path); + await fs.copyFileSync(full_cache_file_path, open_temp_file_path); } catch (error) { console.error(error); return false; @@ -219,7 +219,7 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has } try { - shell.openPath(open_temp_file_path); + await shell.openPath(open_temp_file_path); } catch (error) { console.error(error); 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('End: Electron IPC Main: open_hash_file_to_temp()'); - return 'Return from Electron IPC Main open_hash_file_to_temp()'; - // return result; + // return 'Return from Electron IPC Main open_hash_file_to_temp()'; + return true; });