Files
OSIT-AE-App-Native-Electron/app/js/module_api 2020-01-08.js.bak
2020-01-24 19:51:14 -05:00

615 lines
26 KiB
JavaScript

const os = require('os');
const path = require('path');
const fs = require('fs');
const { ipcRenderer } = require('electron');
exports.api_token_request_async = async function (axios, secret_key) {
console.log('Requesting API temporary token...');
const url = '/api_token_request';
let data = { secret_key: secret_key };
const response = await axios.post(url, data)
.then(function (response) {
//console.log(response);
const api_temporary_token = response.data.temporary_token;
//console.log('API Temporary Token: '+temporary_token);
return api_temporary_token;
})
.catch(function (error) {
console.error(error);
return error;
});
return response;
}
exports.get_event_details = async function (axios, event_id) {
console.log('Requesting event details...');
const url = '/event/'+event_id;
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.get_event_location_details = async function (axios, event_id, event_location_id) {
console.log('Requesting event location details...');
const url = '/event/'+event_id+'/location/'+event_location_id;
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.get_event_location_sessions = async function (axios, event_id, event_location_id) {
console.log('Requesting location sessions...');
const url = '/event/'+event_id+'/location/'+event_location_id+'/session';
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.get_session_presentations = async function (axios, event_id, event_session_id) {
console.log('Requesting session presentations...');
const url = '/event/'+event_id+'/session/'+event_session_id+'/presentation';
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.get_presentation_presenters = async function (axios, event_id, event_session_id, event_presentation_id) {
console.log('Requesting presentation presenters...');
const url = '/event/'+event_id+'/session/'+event_session_id+'/presentation/'+event_presentation_id+'/presenter';
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.get_files_for_type_for_id = async function (axios, for_type, for_id) {
console.log('Requesting files for '+for_type+' '+for_id);
const url = '/event/file/'+for_type+'/'+for_id;
const data = await axios.get(url)
.then(function (response) {
//console.log(response.data);
//data = response.data;
return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
return data;
}
exports.download_file_id = async function (axios, file_id, filename) {
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // in render thread
console.log('1: download file id '+file_id);
const url = '/event/file/'+file_id+'/download';
//const data = await axios.get(url, responseType: 'stream')
const data = await axios({
method: "get",
url: url,
responseType: "stream"
})
.then(function (response) {
console.log('2: downloaded file id '+file_id);
console.log(response);
//console.log(response.data);
let filename = 'default.txt';
if (typeof filename_override === 'undefined' || filename_override == null) {
let headerLine = response.data.headers['content-disposition'];
console.log(headerLine);
console.log(headerLine.indexOf('="'));
console.log(headerLine.indexOf('='));
if (headerLine.indexOf('="') != -1) {
let startFileNameIndex = headerLine.indexOf('="') + 2;
let endFileNameIndex = headerLine.lastIndexOf('"');
filename = headerLine.substring(startFileNameIndex, endFileNameIndex);
} else if (headerLine.indexOf('=') != -1) {
let startFileNameIndex = headerLine.indexOf('=') + 1;
let endFileNameIndex = headerLine.length;
filename = headerLine.substring(startFileNameIndex, endFileNameIndex);
} else {
filename = 'filename_not_found_in_header.txt';
}
} else {
filename = filename_override;
}
let directory = 'file_cache/';
directory_and_filename = path.join(directory, filename);
//console.log(directory_and_filename);
if (fs.existsSync(directory_and_filename)) {
console.log('3a: file already exists: '+directory_and_filename);
} else {
console.log('3b: saving file: '+directory_and_filename);
response.data.pipe(fs.createWriteStream(directory_and_filename));
}
//data = response.data;
console.log('4: saved file id '+file_id);
return true;
//return response.data;
})
.catch(function (error) {
console.log(error);
return error;
});
//return data;
}
exports.launcher_cache_check = async function (launcher, api_base_url, api_temporary_token, file_cache_path) {
if (typeof launcher.event === 'undefined') {
launcher.event = await api.get_event_details(axios, launcher.event_id);
console.log(launcher.event.id);
idbKeyval.set(launcher.event.id, launcher.event, db_event);
/*
.then(function (data) {
console.log(data.id);
idbKeyval.set(data.id, data, db_event);
//for (var i in response) {
//var event_record = data[i];
//idbKeyval.set(response[i].id, response[i], db_event);
//}
});
*/
}
if (typeof launcher.location === 'undefined') {
launcher.location = await api.get_event_location_details(axios, launcher.event_id, launcher.event_location_id);
}
if (typeof launcher.event_files === 'undefined') {
console.log('Check for global event files.');
let for_id = launcher.event_id;
launcher.event_files = await api.get_files_for_type_for_id(axios, 'event', for_id);
for (var i in launcher.event_files) {
console.log('f: ('+launcher.event_files[i].event_file_id+') '+launcher.event_files[i].event_file_filename+' ***')
file_id = launcher.event_files[i].event_file_id; // NOTE: the .id is the hosted_file.id!
let filename = launcher.event_files[i].hash_sha256+'.file';
save_path = path.join(file_cache_path, filename);
if (fs.existsSync(save_path)) {
console.log('Local file already exists: '+save_path);
} else {
console.log('File not found locally. Downloading file: '+save_path);
let api_endpoint = '/event/file/'+file_id+'/download';
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread.
}
}
} else {
console.log('No check for global event files');
}
if (typeof launcher.location_files === 'undefined') {
console.log('Check for location specific files.');
let for_id = launcher.event_location_id;
launcher.location_files = await api.get_files_for_type_for_id(axios, 'location', for_id);
for (var i in launcher.location_files) {
console.log('f: ('+launcher.location_files[i].event_file_id+') '+launcher.location_files[i].event_file_filename+' ***')
file_id = launcher.location_files[i].event_file_id; // NOTE: the .id is the hosted_file.id!
let filename = launcher.location_files[i].hash_sha256+'.file';
save_path = path.join(file_cache_path, filename);
if (fs.existsSync(save_path)) {
console.log('Local file already exists: '+save_path);
} else {
console.log('File not found locally. Downloading file: '+save_path);
let api_endpoint = '/event/file/'+file_id+'/download';
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread.
}
}
} else {
console.log('No check for location specific files.');
}
if (typeof launcher.sessions === 'undefined') {
console.log('Check for location sessions.');
launcher.sessions = await api.get_event_location_sessions(axios, launcher.event_id, launcher.event_location_id);
for (var i in launcher.sessions) {
console.log('*** '+launcher.sessions[i].name+' ***')
// Check for session files start
if (typeof launcher.sessions[i].files === 'undefined') {
console.log('Check for session files.');
let for_id = launcher.sessions[i].id;
launcher.sessions[i].files = await api.get_files_for_type_for_id(axios, 'session', for_id);
if (launcher.sessions[i].files) {
console.log('Session files found.');
for (var j in launcher.sessions[i].files) {
console.log('f: ('+launcher.sessions[i].files[j].event_file_id+') '+launcher.sessions[i].files[j].event_file_filename+' ***');
file_id = launcher.sessions[i].files[j].event_file_id; // NOTE: the .id is the hosted_file.id!
let filename = launcher.sessions[i].files[j].hash_sha256+'.file';
save_path = path.join(file_cache_path, filename);
if (fs.existsSync(save_path)) {
console.log('Local file already exists: '+save_path);
} else {
console.log('File not found locally. Downloading file: '+save_path);
let api_endpoint = '/event/file/'+file_id+'/download';
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread.
}
}
} else {
console.log('No session files found.');
}
} else {
console.log('No check for session files.');
}
// Check for session files end
// Get session presentations and associated files
launcher.sessions[i].presentations = await api.get_session_presentations(axios, launcher.event_id, launcher.sessions[i].id);
for (var j in launcher.sessions[i].presentations) {
console.log('* '+launcher.sessions[i].presentations[j].name);
// Check for presentation files start
if (typeof launcher.sessions[i].presentations[j].files === 'undefined') {
console.log('Check for presentation files.');
let for_id = launcher.sessions[i].presentations[j].id;
launcher.sessions[i].presentations[j].files = await api.get_files_for_type_for_id(axios, 'presentation', for_id);
if (launcher.sessions[i].presentations[j].files) {
console.log('Presentation files found.');
for (var k in launcher.sessions[i].presentations[j].files) {
console.log('f: ('+launcher.sessions[i].presentations[j].files[k].event_file_id+') '+launcher.sessions[i].presentations[j].files[k].event_file_filename+' ***');
file_id = launcher.sessions[i].presentations[j].files[k].event_file_id; // NOTE: the .id is the hosted_file.id!
let filename = launcher.sessions[i].presentations[j].files[k].hash_sha256+'.file';
save_path = path.join(file_cache_path, filename);
if (fs.existsSync(save_path)) {
console.log('Local file already exists: '+save_path);
} else {
console.log('File not found locally. Downloading file: '+save_path);
let api_endpoint = '/event/file/'+file_id+'/download';
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread.
}
}
} else {
console.log('No presentation files found.');
}
} else {
console.log('No check for presentation files.');
}
// Check for presentation files end
// Get session presentations presenters and associated files
//let event_presentation_id = launcher.sessions[i].presentations[j].id;
launcher.sessions[i].presentations[j].presenters = await api.get_presentation_presenters(axios, launcher.event_id, launcher.sessions[i].id, launcher.sessions[i].presentations[j].id);
for (var k in launcher.sessions[i].presentations[j].presenters) {
console.log('-- '+launcher.sessions[i].presentations[j].presenters[k].given_name+' '+launcher.sessions[i].presentations[j].presenters[k].family_name)
// Check for presenter files start
if (typeof launcher.sessions[i].presentations[j].presenters[k].files === 'undefined') {
console.log('Check for presenter files.');
let for_id = launcher.sessions[i].presentations[j].presenters[k].id;
launcher.sessions[i].presentations[j].presenters[k].files = await api.get_files_for_type_for_id(axios, 'presenter', for_id);
if (launcher.sessions[i].presentations[j].presenters[k].files) {
console.log('Presenter files found.');
for (var l in launcher.sessions[i].presentations[j].presenters[k].files) {
console.log('f: ('+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_id+') '+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename+' ***')
file_id = launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_id; // NOTE: the .id is the hosted_file.id!
let filename = launcher.sessions[i].presentations[j].presenters[k].files[l].hash_sha256+'.file';
save_path = path.join(file_cache_path, filename);
if (fs.existsSync(save_path)) {
console.log('Local file already exists: '+save_path);
} else {
console.log('File not found locally. Downloading file: '+save_path);
let api_endpoint = '/event/file/'+file_id+'/download';
ipcRenderer.send('download_file', api_base_url, api_endpoint, api_temporary_token, save_path); // Must download file using main node.js thread.
}
}
} else {
console.log('No presenter files found.');
}
} else {
console.log('No check for presenter files.');
}
// Check for presenter files end
}
}
}
} else {
console.log('No check for location sessions.');
}
console.log('Location cache check finished.');
return true;
}
exports.launcher_render = async function (launcher) {
console.log('Rendering launcher...');
document.getElementById('location_title').innerHTML = '<h1>Launcher '+launcher.location.name+'@'+launcher.event.name+'</h1>';
document.getElementById('event_files_menu').innerHTML = '<h2>Event Files</h2>';
document.getElementById('event_files_menu').innerHTML += '<ul class="">';
for (var i in launcher.event_files) {
document.getElementById('event_files_menu').innerHTML += '<li id="menu_event_file_id_'+launcher.event_files[i].id+'" class="btn btn-info btn_no_case open_local_file" data-hash_sha256="'+launcher.event_files[i].hash_sha256+'.file" data-filename="'+launcher.event_files[i].event_file_filename+'">'+launcher.event_files[i].event_file_filename+'</li>';
}
document.getElementById('event_files_menu').innerHTML += '</ul>';
document.getElementById('location_files_menu').innerHTML = '<h2>Location Files</h2>';
document.getElementById('location_files_menu').innerHTML += '<ul class="" role="">';
for (var i in launcher.location_files) {
document.getElementById('location_files_menu').innerHTML += '<li id="menu_event_file_id_'+launcher.location_files[i].id+'" class="btn btn-info open_local_file" data-hash_sha256="'+launcher.location_files[i].hash_sha256+'.file" data-filename="'+launcher.location_files[i].event_file_filename+'">'+launcher.location_files[i].event_file_filename+'</li>';
}
document.getElementById('location_files_menu').innerHTML += '</ul>';
document.getElementById('sessions_menu').innerHTML = '<h2>Sessions</h2>';
document.getElementById('sessions_menu').innerHTML += '<ul class="" role="">';
//let location_detail = '';
if (document.getElementById('location_detail').innerHTML.length) {
console.log('detail_session_id_xx already exists');
} else {
console.log('location_detail already populated');
document.getElementById('location_detail').innerHTML = ''; // This needs to be reviewed.
}
for (var i in launcher.sessions) {
// List sessions in menu
document.getElementById('sessions_menu').innerHTML += '<li id="menu_session_id_'+launcher.sessions[i].id+'" class="btn btn-info btn_view_session" data-session_id="'+launcher.sessions[i].id+'">'+launcher.sessions[i].name+'</li>';
// Create containers for each session
let session_detail = '';
if (typeof(document.getElementById('detail_session_id_'+launcher.sessions[i].id)) != 'undefined' && document.getElementById('detail_session_id_'+launcher.sessions[i].id) != null) {
//console.log('detail_session_id_xx already exists');
} else {
//console.log('Adding detail_session_id_xx');
session_detail += '<div id="detail_session_id_'+launcher.sessions[i].id+'" class="container d-none session_detail" data-session_id="'+launcher.sessions[i].id+'">';
}
var session_startdatetime = new Date(launcher.sessions[i].start_datetime);
session_detail += '<h2 class="session_title">'+launcher.sessions[i].name+'<span class="badge badge-pill badge-info float-right">'+dateFns.format(session_startdatetime, 'ddd h:mm A')+'</span>'+'</h2>';
//session_detail += '<div class="">'; // for card-group
session_detail += '<div class="card">';
session_detail += '<div class="card-header">Session Files:</div>';
session_detail += '<div class="card-body">';
session_detail += '<ul>';
for (var j in launcher.sessions[i].files) {
//console.log(launcher.sessions[i].files[j].event_file_filename);
session_detail += '<li class="btn btn-info open_local_file" data-hash_sha256="'+launcher.sessions[i].files[j].hash_sha256+'.file" data-filename="'+launcher.sessions[i].files[j].event_file_filename+'">'+launcher.sessions[i].files[j].event_file_filename+'</li>';
}
session_detail += '</ul>';
session_detail += '</div>'; // for card-body
session_detail += '</div> <!-- for session files card -->'; // for session files card
//session_detail += '<div class="session_presentations">';
for (var j in launcher.sessions[i].presentations) {
//session_detail += '<li>'+launcher.sessions[i].presentations[j].name;
var session_presentation_startdatetime = new Date(launcher.sessions[i].presentations[j].start_datetime);
session_detail += '<div class="card">';
session_detail += '<div class="card-header">'+launcher.sessions[i].presentations[j].name+'<span class="badge badge-pill badge-info float-right">'+dateFns.format(session_startdatetime, 'h:mm a')+'</span>'+':</div>';
session_detail += '<div class="card-body">';
session_detail += '<ul class="list-group">';
for (var k in launcher.sessions[i].presentations[j].files) {
//console.log(launcher.sessions[i].presentations[j].files[k].event_file_filename);
session_detail += '<li class="list-group-item btn btn-info open_local_file" data-hash_sha256="'+launcher.sessions[i].presentations[j].files[k].hash_sha256+'.file" data-filename="'+launcher.sessions[i].presentations[j].files[k].event_file_filename+'">'+launcher.sessions[i].presentations[j].files[k].event_file_filename+'</li>';
}
session_detail += '</ul>';
session_detail += '<ul>';
for (var k in launcher.sessions[i].presentations[j].presenters) {
//console.log(launcher.sessions[i].presentations[j].presenters[k].given_name);
session_detail += '<li>'+launcher.sessions[i].presentations[j].presenters[k].given_name+' '+launcher.sessions[i].presentations[j].presenters[k].family_name;
session_detail += '<ul class="list-group">';
for (var l in launcher.sessions[i].presentations[j].presenters[k].files) {
//console.log(launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename);
//session_detail += '<li class="list-group-item" data-hash_sha256="'+launcher.sessions[i].presentations[j].presenters[k].files[l].hash_sha256+'.file" data-filename="'+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename+'">'+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename;
session_detail += '<li class="list-group-item" data-hash_sha256="'+launcher.sessions[i].presentations[j].presenters[k].files[l].hash_sha256+'.file" data-filename="'+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename+'">';
session_detail += '<button class="btn btn-info open_local_file" data-hash_sha256="'+launcher.sessions[i].presentations[j].presenters[k].files[l].hash_sha256+'.file" data-filename="'+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename+'">'+launcher.sessions[i].presentations[j].presenters[k].files[l].event_file_filename+'</button>';
var event_file_size = launcher.sessions[i].presentations[j].presenters[k].files[l].size;
var event_file_created_on = new Date(launcher.sessions[i].presentations[j].presenters[k].files[l].created_on);
session_detail += '<span class="badge badge-pill badge-light float-right">'+format_bytes(event_file_size, 2)+'; '+dateFns.format(event_file_created_on, 'MMM M h:mm A')+'; '+launcher.sessions[i].presentations[j].presenters[k].files[l].internal_os+'</span>';
session_detail += '</li>';
}
session_detail += '</ul>';
session_detail +='</li>';
}
session_detail += '</ul>';
session_detail += '</ul>';
session_detail += '</div>'; // for card-body
session_detail += '</div> <!-- for card -->'; // for card
}
//session_detail += '</div> <!-- for session_presentations -->'; // for session_presentations
//session_detail += '</div>'; // for card-group
session_detail += '</div> <!-- End session detail for '+launcher.sessions[i].id+' -->';
//document.getElementById('location_detail').innerHTML += session_detail;
if (typeof(document.getElementById('detail_session_id_'+launcher.sessions[i].id)) != 'undefined' && document.getElementById('detail_session_id_'+launcher.sessions[i].id) != null) {
document.getElementById('detail_session_id_'+launcher.sessions[i].id).innerHTML = session_detail;
} else {
document.getElementById('location_detail').innerHTML += session_detail;
}
}
document.getElementById('sessions_menu').innerHTML += '</ul>';
index_location_detail('btn_view_session');
index_open_file_buttons('open_local_file');
//const btn_session_{{ event_presentation.id }}_name = document.querySelector('button#presentation_{{ event_presentation.id }}_name');
//btn_presentation_{{ event_presentation.id }}_name.onclick = display_hide_poster_for_type_id.bind(this, true, 'presentation', {{ event_presentation.id }});
console.log('Rendering launcher finished.');
return true;
}
/* Updated 2019-12-19 */
function index_location_detail(class_name) {
var class_elements = document.getElementsByClassName(class_name);
for (var i = 0; i < class_elements.length; i++) {
class_elements[i].addEventListener( 'click', function() {view_session( this.getAttribute('data-session_id')) } );
}
return true;
}
/* Updated 2019-12-19 */
function view_session(session_id) {
var class_elements = document.getElementsByClassName('session_detail');
console.log('*** Session ID: '+session_id);
for (var i = 0; i < class_elements.length; i++) {
console.log('*** checking: '+class_elements[i].getAttribute('data-session_id'));
if (class_elements[i].getAttribute('data-session_id') == session_id) {
console.log('show');
class_elements[i].classList.remove('d-none');
class_elements[i].classList.add('d-block');
} else {
console.log('hide');
class_elements[i].classList.remove('d-block');
class_elements[i].classList.add('d-none');
}
}
return true;
}
/* Updated 2019-12-20 */
function index_open_file_buttons(class_name) {
var class_elements = document.getElementsByClassName(class_name);
for (var i = 0; i < class_elements.length; i++) {
//class_elements[i].addEventListener( 'click', function() {open_local_file( this.getAttribute('data-filename')) } );
//let directory = 'file_cache/';
//directory_and_filename = path.join(directory, class_elements[i].getAttribute('data-filename'));
let file_path = path.join(file_cache_path, class_elements[i].getAttribute('data-hash_sha256'));
let filename = class_elements[i].getAttribute('data-filename');
class_elements[i].addEventListener( 'click', function() { ipcRenderer.send('open_local_file', file_path, filename) } );
//ipcRenderer.send('open_local_file', this.getAttribute('data-filename')); // in render thread
}
return true;
}
function format_bytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}