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

@@ -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;
});