A lot of clean up!!!
This commit is contained in:
152
index.js
152
index.js
@@ -2,11 +2,13 @@ const { app, BrowserWindow, ipcMain, shell, systemPreferences } = require('elect
|
||||
|
||||
|
||||
const axios = require('axios');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
//const http = require('http');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const process = require('process');
|
||||
|
||||
//const http = require('http');
|
||||
//const request = require('request');
|
||||
//const url = require('url');
|
||||
// const usb = require('usb') // Compiled with an old version of Node.js
|
||||
@@ -18,12 +20,14 @@ let tmp_directory = require('os').tmpdir();
|
||||
console.log('Temporary: '+tmp_directory);
|
||||
|
||||
let config = null;
|
||||
let host_file_cache_path = null;
|
||||
let local_file_cache_path = null;
|
||||
let host_file_temp_path = null;
|
||||
|
||||
console.log(os.type());
|
||||
console.log(process.getSystemVersion());
|
||||
|
||||
let endpoints_in_progress = [];
|
||||
|
||||
|
||||
if (os.type == 'Darwin') {
|
||||
if (systemPreferences.getMediaAccessStatus('microphone') != 'granted') {
|
||||
@@ -118,14 +122,14 @@ ipcMain.handle('import_config', async (event, config_data) => {
|
||||
|
||||
config = config_data;
|
||||
|
||||
host_file_cache_path = config.host_file_cache_path;
|
||||
local_file_cache_path = config.local_file_cache_path;
|
||||
host_file_temp_path = config.host_file_temp_path;
|
||||
|
||||
if (fs.existsSync(host_file_cache_path)) {
|
||||
console.log('Host file cache path exists: '+host_file_cache_path);
|
||||
if (fs.existsSync(local_file_cache_path)) {
|
||||
console.log('Host file cache path exists: '+local_file_cache_path);
|
||||
} else {
|
||||
fs.mkdirSync(host_file_cache_path, true);
|
||||
console.log('Host file cache path created: '+host_file_cache_path);
|
||||
fs.mkdirSync(local_file_cache_path, true);
|
||||
console.log('Host file cache path created: '+local_file_cache_path);
|
||||
}
|
||||
|
||||
if (fs.existsSync(host_file_temp_path)) {
|
||||
@@ -141,32 +145,17 @@ ipcMain.handle('import_config', async (event, config_data) => {
|
||||
|
||||
// Download file to path
|
||||
// full_save_path should be the full path that includes the filename
|
||||
// Updated 2022-03-09
|
||||
ipcMain.handle('download_file', async (event, api_base_url, api_endpoint, full_save_path) => {
|
||||
// Updated 2022-10-12
|
||||
ipcMain.handle('download_file', async (event, api_base_url, api_endpoint, full_save_path, hash=null, verify_hash=false, overwrite_existing=false) => {
|
||||
console.log('*** Electron IPC Main: download_file() ***');
|
||||
// console.log('ipcMain on download_file: api_base_url='+api_base_url+' | api_temporary_token='+api_temporary_token);
|
||||
console.log('ipcMain on download_file: api_base_url='+api_base_url);
|
||||
console.log('ipcMain download and save file: HTTP '+api_endpoint+' -> FILE '+full_save_path);
|
||||
// console.log('ipcMain on download_file: api_base_url='+api_base_url);
|
||||
console.log(`ipcMain download and save file: HTTP ${api_endpoint} -> FILE ${full_save_path}`);
|
||||
if (!api_base_url) {
|
||||
console.log('API Base URL is required. Returning false');
|
||||
return false;
|
||||
}
|
||||
|
||||
let result = await download_file(api_base_url, api_endpoint, full_save_path);
|
||||
|
||||
// console.log(result);
|
||||
console.log('End: Electron IPC Main: download_file()');
|
||||
return result;
|
||||
});
|
||||
|
||||
|
||||
// Download file to path
|
||||
// full_save_path should be the full path that includes the filename
|
||||
// Updated 2022-10-11
|
||||
async function download_file(api_base_url, api_endpoint, full_save_path) {
|
||||
console.log('*** node.js: download_file() ***');
|
||||
// console.log(api_base_url);
|
||||
|
||||
axios.defaults.baseURL = api_base_url;
|
||||
axios.defaults.headers.common['Access-Control-Allow-Origin'] = config.access_control_allow_origin; // '*'; // app_config.access_control_allow_origin;
|
||||
axios.defaults.headers.common['content-type'] = 'application/json';
|
||||
@@ -175,20 +164,71 @@ async function download_file(api_base_url, api_endpoint, full_save_path) {
|
||||
|
||||
const url = api_endpoint;
|
||||
|
||||
let tmp_full_save_path = full_save_path+'.tmp';
|
||||
const tmp_full_save_path = full_save_path+'.tmp';
|
||||
|
||||
if (fs.existsSync(tmp_full_save_path)) {
|
||||
console.log('A temp file was found!');
|
||||
return false;
|
||||
} else {
|
||||
console.log(`Downloading for real...`);
|
||||
console.log(`A temp download file was found! ${tmp_full_save_path}`);
|
||||
|
||||
let stats = null;
|
||||
try {
|
||||
stats = fs.statSync(tmp_full_save_path);
|
||||
|
||||
// console.log(`File Accessed Last: ${stats.atime}`); // File data last changed (actual contents)
|
||||
console.log(`File Data Last Modified: ${stats.mtime}`); // File data last changed (actual contents)
|
||||
console.log(`File Metadata Last Modified: ${stats.ctime}`); // File metadata last changed (filename, permissions, etc)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
let current_datetime = new Date();
|
||||
let offset_minutes = 3; // In minutes. 5 minutes of no changes to the file content seems reasonable? Trying with 3 minutes 2022-10-12
|
||||
let offset_datetime = new Date(current_datetime.getTime() - offset_minutes*60000);
|
||||
|
||||
// console.log(`Times: ${current_datetime} ${offset_datetime} | File ${stats.mtime}`);
|
||||
if (stats.mtime < offset_datetime) {
|
||||
console.log(`Marking as expired temp file based on modified datetime. Expire after: ${offset_minutes} minutes`);
|
||||
overwrite_existing = true;
|
||||
} else {
|
||||
console.log(`Temp download file has not expired yet. Expire after: ${offset_minutes} minutes`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (fs.existsSync(full_save_path)) {
|
||||
console.log(`A cached file was found! ${full_save_path}`);
|
||||
if (verify_hash) {
|
||||
const file_buffer = fs.readFileSync(full_save_path);
|
||||
const file_hash_sha256 = crypto.createHash('sha256');
|
||||
file_hash_sha256.update(file_buffer);
|
||||
|
||||
const file_hash_sha256_check = file_hash_sha256.digest('hex');
|
||||
if (file_hash_sha256_check == hash) {
|
||||
// console.log('File hash match', file_hash_sha256_check);
|
||||
} else {
|
||||
console.log('File hash does not match', file_hash_sha256_check);
|
||||
if (overwrite_existing) {
|
||||
console.log('Going to overwrite the existing file because the hash does not match.');
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// return false;
|
||||
}
|
||||
|
||||
console.log('Endpoints in Progress:', endpoints_in_progress);
|
||||
if (endpoints_in_progress.includes(api_endpoint)) {
|
||||
console.log(`Endpoint already being downloaded: ${api_endpoint}`);
|
||||
return false;
|
||||
}
|
||||
// console.log(`Done with checks. Time to download! Endpoint: ${api_endpoint}`);
|
||||
endpoints_in_progress.push(api_endpoint);
|
||||
|
||||
let download_result = await axios({
|
||||
method: 'get',
|
||||
url: url,
|
||||
responseType: 'stream' /* responseType must be stream */
|
||||
}).then(function (response) {
|
||||
console.log('Creating write stream to download file...');
|
||||
console.log(`Creating write stream for downloading endpoint: ${api_endpoint}`);
|
||||
const writer = fs.createWriteStream(tmp_full_save_path);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -202,19 +242,42 @@ async function download_file(api_base_url, api_endpoint, full_save_path) {
|
||||
reject(err);
|
||||
});
|
||||
writer.on('close', () => {
|
||||
console.log('Writer close!');
|
||||
|
||||
if (!error) {
|
||||
fs.moveFileSync(tmp_full_save_path, full_save_path);
|
||||
console.log('Temporary file moved/renamed: '+config_directory);
|
||||
// console.log(`Download complete! Writer closed.`);
|
||||
resolve(true);
|
||||
} else {
|
||||
console.log('Writer closed unexpectedly!', error);
|
||||
}
|
||||
//no need to call the reject here, as it will have been called in the
|
||||
//'error' stream;
|
||||
});
|
||||
});
|
||||
})
|
||||
.then(function (response) {
|
||||
console.log(`Download complete. Temporary file moved/renamed: ${full_save_path}`);
|
||||
fs.renameSync(tmp_full_save_path, full_save_path);
|
||||
|
||||
for( let i = 0; i < endpoints_in_progress.length; i++){
|
||||
if ( endpoints_in_progress[i] === api_endpoint) {
|
||||
endpoints_in_progress.splice(i, 1);
|
||||
// NOTE: Decrement the index variable so it does not skip the next item in the array.
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(`Error downloading! Endpoint: ${api_endpoint}`);
|
||||
|
||||
for( let i = 0; i < endpoints_in_progress.length; i++){
|
||||
if ( endpoints_in_progress[i] === api_endpoint) {
|
||||
endpoints_in_progress.splice(i, 1);
|
||||
// NOTE: Decrement the index variable so it does not skip the next item in the array.
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
if (error.response) {
|
||||
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
|
||||
} else {
|
||||
@@ -226,21 +289,20 @@ 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(`Done with download function! Endpoint: ${api_endpoint}`);
|
||||
|
||||
// console.log(download_result);
|
||||
// console.log('End: download_file()');
|
||||
return download_result;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, hash, host_file_temp_path, filename, check_hash) => {
|
||||
ipcMain.handle('open_hash_file_to_temp', async (event, local_file_cache_path, hash, host_file_temp_path, filename, verify_hash=true) => {
|
||||
console.log('*** Electron IPC Main: open_hash_file_to_temp() ***');
|
||||
console.log('ipcMain on open_hash_file_to_temp');
|
||||
console.log(`ipcMain open hash file from temp directory: ${host_file_cache_path} -> ${host_file_temp_path}/${filename}`);
|
||||
console.log(`ipcMain open hash file from temp directory: ${local_file_cache_path} -> ${host_file_temp_path}/${filename}`);
|
||||
|
||||
// NOTE: This may be needed later? Uncomment if paths are relative to working directory.
|
||||
// let cache_file_path = path.join(process.cwd(), host_file_cache_path);
|
||||
let cache_file_path = host_file_cache_path;
|
||||
// let cache_file_path = path.join(process.cwd(), local_file_cache_path);
|
||||
let cache_file_path = local_file_cache_path;
|
||||
console.log(cache_file_path);
|
||||
|
||||
let hash_filename = hash+'.file';
|
||||
@@ -260,7 +322,7 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
|
||||
}
|
||||
|
||||
if (fs.existsSync(full_cache_file_path)) {
|
||||
console.log(`Hashed file exists in cache: ${full_cache_file_path}`);
|
||||
// console.log(`Hashed file exists in cache: ${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);
|
||||
@@ -269,14 +331,14 @@ ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, has
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_hash) {
|
||||
if (verify_hash) {
|
||||
const file_buffer = fs.readFileSync(full_cache_file_path);
|
||||
const file_hash_sha256 = crypto.createHash('sha256');
|
||||
file_hash_sha256.update(file_buffer);
|
||||
|
||||
const file_hash_sha256_check = file_hash_sha256.digest('hex');
|
||||
if (file_hash_sha256_check == hash) {
|
||||
console.log('File hash match', file_hash_sha256_check);
|
||||
// console.log('File hash match', file_hash_sha256_check);
|
||||
} else {
|
||||
console.log('File hash does not match', file_hash_sha256_check);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user