Working on rewrite version 3. Focus on caching and opening after download.

This commit is contained in:
Scott Idem
2022-03-08 18:30:49 -05:00
parent 1d7c14dedb
commit 552be26831
7 changed files with 357 additions and 49 deletions

160
index.js
View File

@@ -95,36 +95,124 @@ app.on('activate', () => {
// ipcMain.on('download_file', (event, api_base_url, api_endpoint, api_temporary_token, save_path) => {
ipcMain.on('download_file', (event, api_base_url, api_endpoint, save_path) => {
ipcMain.on('download_file', async (event, api_base_url, api_endpoint, save_path) => {
console.log('*** call IPC 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: '+api_endpoint+' -> '+save_path);
console.log('ipcMain download and save file: HTTP '+api_endpoint+' -> FILE '+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;
let result = await download_file(api_base_url, api_endpoint, save_path);
event.sender.send('download_file_reply', result);
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;
axios({
method: 'get',
url: url,
responseType: 'stream' /* responseType must be stream */
}).then(function (response) {
response.data.pipe(fs.createWriteStream(save_path));
});
// 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);
});
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;
const url = api_endpoint;
const writer = fs.createWriteStream(save_path);
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;
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.
});
resolve(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, filename) => {
console.log(host_file_cache_path);
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);
let cache_file_path = path.join(process.cwd(), host_file_cache_path);
@@ -134,23 +222,35 @@ ipcMain.on('open_local_file', (event, host_file_cache_path, hash, filename) => {
let full_cache_file_path = path.join(cache_file_path, hash_filename);
console.log(full_cache_file_path);
open_file_path = path.join(process.cwd(), 'temp/', filename);
console.log(open_file_path);
open_temp_file_path = path.join(process.cwd(), host_file_temp_path, filename); // 'temp/'
console.log(open_temp_file_path);
if (fs.existsSync(open_file_path)) {
//console.log('File link already exists: '+open_file_path);
if (fs.existsSync(open_temp_file_path)) {
console.log('A file with the same name already exists in the local temp directory: '+open_temp_file_path);
// NOTE: Should the file be checked to see if it has changed from the hashed cache version???
// NOTE: What if they made changes to the file locally in temp? The changed file would be used since a new copy is not being made.
// NOTE: It might make sense for this to be a configurable option depending on the group. Some do not allow changes. This helps enforce that.
console.log('File copy already exists: '+open_file_path);
} else {
//console.log('Creating file link: '+open_file_path);
//fs.linkSync(full_cache_file_path, open_file_path);
console.log('Copying file to temp: '+open_file_path);
fs.copyFileSync(full_cache_file_path, open_file_path);
}
shell.openPath(open_file_path);
//fs.open(open_file_path);
if (fs.existsSync(full_cache_file_path)) {
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);
} catch (error) {
console.error(error);
return false;
}
// console.log('Creating file link: '+open_temp_file_path);
// fs.linkSync(full_cache_file_path, open_temp_file_path);
} else {
return false;
}
shell.openPath(open_temp_file_path);
//fs.open(open_temp_file_path);
// return true;
event.sender.send('asynchronous-reply', true);
});