More work on rewrite version 3. Focus on caching and opening after download.
This commit is contained in:
117
app/js/app_v3.js
117
app/js/app_v3.js
@@ -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;
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"account_id": "_XY7DXtc9MY",
|
||||
"device_id": "dbgMWS3KEHE",
|
||||
"event_id": "pjrcghqwert",
|
||||
"event_location_id": null,
|
||||
"event_session_id": null,
|
||||
|
||||
194
index.js
194
index.js
@@ -94,126 +94,96 @@ app.on('activate', () => {
|
||||
})
|
||||
|
||||
|
||||
// ipcMain.on('download_file', (event, api_base_url, api_endpoint, api_temporary_token, save_path) => {
|
||||
ipcMain.on('download_file', async (event, api_base_url, api_endpoint, save_path) => {
|
||||
console.log('*** call IPC download_file() ***');
|
||||
// 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) => {
|
||||
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(api_temporary_token);
|
||||
console.log('ipcMain download and save file: HTTP '+api_endpoint+' -> FILE '+save_path);
|
||||
console.log('ipcMain download and save file: HTTP '+api_endpoint+' -> FILE '+full_save_path);
|
||||
|
||||
let result = await download_file(api_base_url, api_endpoint, save_path);
|
||||
event.sender.send('download_file_reply', result);
|
||||
let result = await download_file(api_base_url, api_endpoint, full_save_path);
|
||||
|
||||
// axios.defaults.baseURL = api_base_url;
|
||||
// axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'; // app_config.access_control_allow_origin;
|
||||
// axios.defaults.headers.common['content-type'] = 'application/json';
|
||||
// axios.defaults.headers.common['x-aether-api-key'] = 'dFP6J9DVj9hUgIMn-fNIqg'; // api_secret_key;
|
||||
// axios.defaults.headers.common['x-account-id'] = '_XY7DXtc9MY'; // account_id;
|
||||
|
||||
// const url = api_endpoint;
|
||||
|
||||
// const writer = fs.createWriteStream(save_path);
|
||||
|
||||
// await axios({
|
||||
// method: 'get',
|
||||
// url: url,
|
||||
// responseType: 'stream' /* responseType must be stream */
|
||||
// }).then(function (response) {
|
||||
// console.log('Downloading...?');
|
||||
// // response.data.pipe(fs.createWriteStream(save_path));
|
||||
// // return true;
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// response.data.pipe(writer);
|
||||
// let error = null;
|
||||
// writer.on('error', err => {
|
||||
// console.log('Writer error!');
|
||||
// error = err;
|
||||
// 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) {
|
||||
// if (error.response && error.response.status === 404) {
|
||||
// return null; // Returning null since there were no results
|
||||
// }
|
||||
// console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
|
||||
// // console.log(error);
|
||||
// return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
|
||||
// });
|
||||
|
||||
// event.sender.send('asynchronous-reply', true);
|
||||
console.log(result);
|
||||
console.log('End: Electron IPC Main: download_file()');
|
||||
// return 'Return from Electron IPC Main download_file()';
|
||||
return result;
|
||||
});
|
||||
|
||||
async function download_file(api_base_url, api_endpoint, save_path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
axios.defaults.baseURL = api_base_url;
|
||||
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'; // app_config.access_control_allow_origin;
|
||||
axios.defaults.headers.common['content-type'] = 'application/json';
|
||||
axios.defaults.headers.common['x-aether-api-key'] = 'dFP6J9DVj9hUgIMn-fNIqg'; // api_secret_key;
|
||||
axios.defaults.headers.common['x-account-id'] = '_XY7DXtc9MY'; // account_id;
|
||||
// Download file to path
|
||||
// full_save_path should be the full path that includes the filename
|
||||
// Updated 2022-03-09
|
||||
async function download_file(api_base_url, api_endpoint, full_save_path) {
|
||||
console.log('*** node.js: download_file() ***');
|
||||
|
||||
const url = api_endpoint;
|
||||
axios.defaults.baseURL = api_base_url;
|
||||
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'; // app_config.access_control_allow_origin;
|
||||
axios.defaults.headers.common['content-type'] = 'application/json';
|
||||
axios.defaults.headers.common['x-aether-api-key'] = 'dFP6J9DVj9hUgIMn-fNIqg'; // api_secret_key;
|
||||
axios.defaults.headers.common['x-account-id'] = '_XY7DXtc9MY'; // account_id;
|
||||
|
||||
const writer = fs.createWriteStream(save_path);
|
||||
const url = api_endpoint;
|
||||
|
||||
let result = axios({
|
||||
method: 'get',
|
||||
url: url,
|
||||
responseType: 'stream' /* responseType must be stream */
|
||||
}).then(function (response) {
|
||||
console.log('Downloading...?');
|
||||
// response.data.pipe(fs.createWriteStream(save_path));
|
||||
// return true;
|
||||
let result = await axios({
|
||||
method: 'get',
|
||||
url: url,
|
||||
responseType: 'stream' /* responseType must be stream */
|
||||
}).then(function (response) {
|
||||
console.log('Downloading...');
|
||||
// response.data.pipe(fs.createWriteStream(full_save_path));
|
||||
// return true;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
response.data.pipe(writer);
|
||||
let error = null;
|
||||
writer.on('error', err => {
|
||||
console.log('Writer error!');
|
||||
error = err;
|
||||
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) {
|
||||
if (error.response && error.response.status === 404) {
|
||||
return null; // Returning null since there were no results
|
||||
}
|
||||
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
|
||||
// console.log(error);
|
||||
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
|
||||
});
|
||||
response.data.pipe(fs.createWriteStream(full_save_path));
|
||||
return true;
|
||||
|
||||
resolve(result)
|
||||
// const writer = fs.createWriteStream(full_save_path);
|
||||
|
||||
// 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;
|
||||
// });
|
||||
// });
|
||||
})
|
||||
}
|
||||
.catch(function (error) {
|
||||
console.log(`Error downloading! Endpoint: ${api_endpoint}`);
|
||||
// console.log(error);
|
||||
// console.log(error.response);
|
||||
if (error.response && error.response.status === 404) {
|
||||
return null; // Returning null since there were no results
|
||||
}
|
||||
console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`);
|
||||
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
|
||||
});
|
||||
|
||||
console.log(result);
|
||||
console.log('End: download_file()');
|
||||
// return 'Return from download_file()';
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ipcMain.on('open_local_file', (event, file_path, filename) => {
|
||||
// ipcMain.on('open_local_file', ({host_file_cache_path, hash, filename}) => {
|
||||
ipcMain.on('open_local_file', (event, host_file_cache_path, hash, host_file_temp_path, filename) => {
|
||||
console.log('ipcMain open local file: '+host_file_cache_path+' -> '+filename);
|
||||
ipcMain.handle('open_hash_file_to_temp', async (event, host_file_cache_path, hash, host_file_temp_path, filename) => {
|
||||
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}`);
|
||||
|
||||
let cache_file_path = path.join(process.cwd(), host_file_cache_path);
|
||||
console.log(cache_file_path);
|
||||
@@ -248,9 +218,15 @@ ipcMain.on('open_local_file', (event, host_file_cache_path, hash, host_file_temp
|
||||
return false;
|
||||
}
|
||||
|
||||
shell.openPath(open_temp_file_path);
|
||||
//fs.open(open_temp_file_path);
|
||||
// return true;
|
||||
try {
|
||||
shell.openPath(open_temp_file_path);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return false;
|
||||
}
|
||||
|
||||
event.sender.send('asynchronous-reply', true);
|
||||
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;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user