diff --git a/src/lib/api.ts b/src/lib/api.ts index edc1ab40..2ee887cd 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -3,408 +3,19 @@ // This needs to be cleaned up and ideally removed the need for Axios. import type { key_val } from '$lib/ae_stores'; + + import { delete_object } from './api_delete_object'; // Exported at the end of this file import { get_object } from './api_get_object'; // Exported at the end of this file import { patch_object } from './api_patch_object'; // Exported at the end of this file import { post_object } from './api_post_object'; // Exported at the end of this file + +import { get_ae_obj_id_crud } from '$lib/api_get__crud_obj_id'; +import { get_ae_obj_li_for_obj_id_crud } from '$lib/api_get__crud_obj_li_v1'; import { get_ae_obj_li_for_obj_id_crud_v2 } from '$lib/api_get__crud_obj_li_v2'; -// Updated 2023-12-01 -export let get_ae_obj_id_crud = async function get_ae_obj_id_crud( - { - api_cfg, - no_account_id=false, - obj_type, - obj_id, - use_alt_table=false, - use_alt_base=false, - inc={}, - enabled='enabled', - hidden='not_hidden', - limit=999999, - offset=0, - data={}, - // key, - // jwt=null, - headers={}, - params={}, - timeout=25000, - return_meta=false, - log_lvl=0 - }: { - api_cfg: any, - no_account_id?: boolean, - obj_type: string, - obj_id: string, - use_alt_table?: boolean, - use_alt_base?: boolean, - inc?: any, - enabled?: string, - hidden?: string, - limit?: number, - offset?: number, - data?: any, - // key: string, - // jwt?: string, - headers?: any, - params?: any, - timeout?: number, - return_meta?: boolean, - log_lvl?: number - } - ) { - if (log_lvl) { - console.log('*** get_ae_obj_id_crud() ***'); - } - - // data = {}; - // data['super_key'] = key; - // data['jwt'] = jwt; - // NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST - - let endpoint = ''; - if (obj_type == 'account') { - endpoint = `/crud/account/${obj_id}`; - } else if (obj_type == 'address') { - endpoint = `/crud/address/${obj_id}`; - } else if (obj_type == 'archive') { - endpoint = `/crud/archive/${obj_id}`; - } else if (obj_type == 'archive_content') { - endpoint = `/crud/archive/content/${obj_id}`; - } else if (obj_type == 'contact') { - endpoint = `/crud/contact/${obj_id}`; - } else if (obj_type == 'data_store') { - endpoint = `/crud/data_store/${obj_id}`; - } else if (obj_type == 'event') { - endpoint = `/crud/event/${obj_id}`; - } else if (obj_type == 'event_abstract') { - endpoint = `/crud/event/abstract/${obj_id}`; - } else if (obj_type == 'event_badge') { - endpoint = `/crud/event/badge/${obj_id}`; - } else if (obj_type == 'event_device') { - endpoint = `/crud/event/device/${obj_id}`; - } else if (obj_type == 'event_exhibit') { - endpoint = `/crud/event/exhibit/${obj_id}`; - } else if (obj_type == 'event_exhibit_tracking') { - endpoint = `/crud/event/exhibit/tracking/${obj_id}`; - } else if (obj_type == 'event_file') { - endpoint = `/crud/event/file/${obj_id}`; - } else if (obj_type == 'event_location') { - endpoint = `/crud/event/location/${obj_id}`; - } else if (obj_type == 'event_person') { - endpoint = `/crud/event/person/${obj_id}`; - } else if (obj_type == 'event_presentation') { - endpoint = `/crud/event/presentation/${obj_id}`; - } else if (obj_type == 'event_presenter') { - endpoint = `/crud/event/presenter/${obj_id}`; - } else if (obj_type == 'event_session') { - endpoint = `/crud/event/session/${obj_id}`; - } else if (obj_type == 'event_track') { - endpoint = `/crud/event/track/${obj_id}`; - } else if (obj_type == 'grant') { - endpoint = `/crud/grant/${obj_id}`; - } else if (obj_type == 'hosted_file') { - endpoint = `/crud/hosted_file/${obj_id}`; - } else if (obj_type == 'journal') { - endpoint = `/crud/journal/${obj_id}`; - } else if (obj_type == 'journal_entry') { - endpoint = `/crud/journal/entry/${obj_id}`; - } else if (obj_type == 'order') { - endpoint = `/crud/order/${obj_id}`; - } else if (obj_type == 'order_line') { - endpoint = `/crud/order/line/${obj_id}`; - } else if (obj_type == 'page') { - endpoint = `/crud/page/${obj_id}`; - } else if (obj_type == 'person') { - endpoint = `/crud/person/${obj_id}`; - } else if (obj_type == 'post') { - endpoint = `/crud/post/${obj_id}`; - } else if (obj_type == 'post_comment') { - endpoint = `/crud/post/comment/${obj_id}`; - } else if (obj_type == 'site') { - endpoint = `/crud/site/${obj_id}`; - } else if (obj_type == 'site_domain') { - endpoint = `/crud/site/domain/${obj_id}`; - } else if (obj_type == 'sponsorship_cfg') { - endpoint = `/crud/sponsorship/cfg/${obj_id}`; - } else if (obj_type == 'sponsorship') { - endpoint = `/crud/sponsorship/${obj_id}`; - // } else if (obj_type == 'user') { - // endpoint = `/crud/user/${obj_id}`; - } else { - console.log(`Unknown object type: ${obj_type}`); - return false; - } - if (log_lvl) { - console.log('Endpoint:', endpoint); - } - - params['use_alt_table'] = use_alt_table; - params['use_alt_base'] = use_alt_base; - - if (log_lvl) { - console.log('Params:', params); - } - - if (no_account_id) { - headers['x-no-account-id'] = 'Nothing to See Here'; - delete headers['x-account-id']; - delete api_cfg['headers']['x-account-id']; - // headers['x-account-id'] = null; - // headers['x-account-id'] = '_XY7DXtc9Mxx'; - // params['x_no_account_id_token'] = 'Nothing to See Here'; - - // Remove the x-account-id header - // if (headers['x-account-id']) { - // delete headers['x-account-id']; - // } - - // headers['x-account-id'] = null; - // headers['x-no-account-id-token'] = 'Nothing to See Here'; // get_object() will fix the underscores to dashes - } - - let object_obj_get_promise = await get_object({ - api_cfg: api_cfg, - endpoint: endpoint, - headers: headers, - params: params, - timeout: timeout, - log_lvl: log_lvl - }); - - if (log_lvl > 1) { - console.log(object_obj_get_promise); - } - - return object_obj_get_promise; -} - - -// The lookup "obj_type" should broken out into a separate function. - 2024-08-07 -// Updated 2023-11-15 -export let get_ae_obj_li_for_obj_id_crud = async function get_ae_obj_li_for_obj_id_crud( - { - api_cfg, - obj_type, - for_obj_type, - for_obj_id, // NOTE: Changed 2023-12-06 to no longer required - use_alt_table=false, - use_alt_base=false, - inc={}, - enabled='enabled', - hidden='not_hidden', - order_by_li=null, - limit=999999, - offset=0, - // key, - // jwt=null, - headers={}, - params_json=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the API endpoint. Example: { "fulltext_search": { "default_qry_str": "Search string for default", "address_default_qry_str": "Search string for address", "contact_1_default_qry_str": "Search string for contact_1" } } - // json_obj=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. - params={}, - return_meta=false, - log_lvl=1 - }: { - api_cfg: any, - obj_type: string, - for_obj_type: string, - for_obj_id?: string, - use_alt_table?: boolean, - use_alt_base?: boolean, - inc?: key_val - enabled?: string, - hidden?: string, - order_by_li?: any, - limit?: number, - offset?: number, - // key: string, - // jwt?: string, - headers?: any, - params_json?: any, - // json_obj?: any, - params?: key_val, - return_meta?: boolean, - log_lvl?: number - } - ) { - if (log_lvl) { - console.log('*** get_ae_obj_li_for_obj_id_crud() ***'); - } - - // data = {}; - // data['super_key'] = key; - // data['jwt'] = jwt; - // NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST - - // const endpoint = `/crud/${obj_type}/list`; - - let endpoint = ''; - if (obj_type == 'account') { - endpoint = `/crud/account/list`; - } else if (obj_type == 'address') { - endpoint = `/crud/address/list`; - } else if (obj_type == 'archive') { - endpoint = `/crud/archive/list`; - } else if (obj_type == 'archive_content') { - endpoint = `/crud/archive/content/list`; - } else if (obj_type == 'contact') { - endpoint = `/crud/contact/list`; - } else if (obj_type == 'data_store') { - endpoint = `/crud/data_store/list`; - } else if (obj_type == 'event') { - endpoint = `/crud/event/list`; - } else if (obj_type == 'event_abstract') { - endpoint = `/crud/event/abstract/list`; - } else if (obj_type == 'event_badge') { - endpoint = `/crud/event/badge/list`; - } else if (obj_type == 'event_device') { - endpoint = `/crud/event/device/list`; - } else if (obj_type == 'event_exhibit') { - endpoint = `/crud/event/exhibit/list`; - } else if (obj_type == 'event_exhibit_tracking') { - endpoint = `/crud/event/exhibit/tracking/list`; - } else if (obj_type == 'event_file') { - endpoint = `/crud/event/file/list`; - } else if (obj_type == 'event_location') { - endpoint = `/crud/event/location/list`; - } else if (obj_type == 'event_person') { - endpoint = `/crud/event/person/list`; - } else if (obj_type == 'event_presentation') { - endpoint = `/crud/event/presentation/list`; - } else if (obj_type == 'event_presenter') { - endpoint = `/crud/event/presenter/list`; - } else if (obj_type == 'event_session') { - endpoint = `/crud/event/session/list`; - } else if (obj_type == 'event_track') { - endpoint = `/crud/event/track/list`; - } else if (obj_type == 'grant') { - endpoint = `/crud/grant/list`; - } else if (obj_type == 'hosted_file') { - endpoint = `/crud/hosted_file/list`; - } else if (obj_type == 'journal') { - endpoint = `/crud/journal/list`; - } else if (obj_type == 'journal_entry') { - endpoint = `/crud/journal/entry/list`; - } else if (obj_type == 'order') { - endpoint = `/crud/order/list`; - } else if (obj_type == 'order_line') { - endpoint = `/crud/order/line/list`; - } else if (obj_type == 'page') { - endpoint = `/crud/page/list`; - } else if (obj_type == 'person') { - endpoint = `/crud/person/list`; - } else if (obj_type == 'post') { - endpoint = `/crud/post/list`; - } else if (obj_type == 'post_comment') { - endpoint = `/crud/post/comment/list`; - } else if (obj_type == 'site') { - endpoint = `/crud/site/list`; - } else if (obj_type == 'sponsorship_cfg') { - endpoint = `/crud/sponsorship/cfg/list`; - } else if (obj_type == 'sponsorship') { - endpoint = `/crud/sponsorship/list`; - // } else if (obj_type == 'user') { - // endpoint = `/crud/user/list`; - // } else if (obj_type == 'lu' && for_obj_type == 'country_subdivision') { - // endpoint = `/crud/lu/country_subdivision/list`; - // for_obj_type = null; - // } else if (obj_type == 'lu' && for_obj_type == 'country') { - // endpoint = `/crud/lu/country/list`; - // for_obj_type = null; - // } else if (obj_type == 'lu' && for_obj_type == 'time_zone') { - // endpoint = `/crud/lu/time_zone/list`; - // for_obj_type = null; - } else { - console.log(`Unknown object type: ${obj_type}`); - return false; - } - if (log_lvl) { - console.log('Endpoint:', endpoint); - } - - if (for_obj_type) { - params['for_obj_type'] = for_obj_type; - } - if (for_obj_id) { - params['for_obj_id'] = for_obj_id; - } - - params['use_alt_table'] = use_alt_table; - params['use_alt_base'] = use_alt_base; - - /* Need to deal with inc params here */ - - let allowed_enabled_list = ['all', 'enabled', 'not_enabled'] - if (allowed_enabled_list.includes(enabled) ) { - params['enabled'] = enabled; - } - - let allowed_hidden_list = ['all', 'hidden', 'not_hidden']; - if (allowed_hidden_list.includes(hidden) ) { - params['hidden'] = hidden; - } - - // NOTE: The order_by_li variable is in the "headers" because if is a the URL GET params do not handle multiple values very well. Maybe base64 encore in the future or something? Reminder that GET requests should not have a body (no JSON). - // NOTE: The order_by_li should be a key value pair of the property/DB field to sort and how to sort (ASC or DESC) - if (order_by_li) { - headers['order_by_li'] = order_by_li; - } - - if (limit >= 0) { - params['limit'] = limit; - } - - if (offset >= 0) { - params['offset'] = offset; - } - - if (params_json) { - // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. - // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). - console.log('JSON Object:', params_json); - console.log(JSON.stringify(params_json)); - // NOTE: "jp" stands for "JSON Params" - params['jp'] = encodeURIComponent(JSON.stringify(params_json)); - if (params['jp'].length > 2083) { - console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['jp'].length} [THIS DOES NOT EXIST YET]`); - return false; - } - } - - // if (json_obj) { - // // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. - // // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). - // console.log('JSON Object:', json_obj); - // params['json_str'] = encodeURIComponent(JSON.stringify(json_obj)); - // if (params['json_str'].length > 2083) { - // console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['json_str'].length} [THIS DOES NOT EXIST YET]`); - // return false; - // } - // } - - if (log_lvl) { - console.log('Params:', params); - } - - let object_li_get_promise = await api.get_object({ - api_cfg: api_cfg, - endpoint: endpoint, - headers: headers, - params: params, - return_meta: return_meta, - log_lvl: log_lvl - }); - - if (log_lvl > 1) { - console.log(object_li_get_promise); - } - - return object_li_get_promise; -} - - // This new function has not been tested yet!!! // Updated 2024-08-07 export let get_ae_obj_li_for_lu = async function get_ae_obj_li_for_lu( @@ -1271,53 +882,15 @@ export let send_email = async function send_email( /* END: Utility: Email Related */ -// function resolved(result: any) { -// console.log('Resolved'); -// } - -// function rejected(result: any) { -// console.error(result); -// } - -// const parse_params = (params) => { -// const keys = Object.keys(params); -// let options = ''; - -// keys.forEach((key) => { -// const isParamTypeObject = typeof params[key] === 'object'; -// const isParamTypeArray = isParamTypeObject && (params[key].length >= 0); - -// if (!isParamTypeObject) { -// let encoded_value = encodeURIComponent(params[key]); -// options += `${key}=${encoded_value}&`; -// // options += `${key}=${params[key]}&`; -// } - -// if (isParamTypeObject && isParamTypeArray) { -// params[key].forEach((element) => { -// let encoded_value = encodeURIComponent(element); -// options += `${key}=${encoded_value}&`; -// // options += `${key}=${element}&`; -// }); -// } -// }); - -// return options ? options.slice(0, -1) : options; -// }; - - -// export let test = async function test() { -// console.log('Exported test function from API.'); -// } let obj = { - get_ae_obj_li_for_obj_id_crud_v2: get_ae_obj_li_for_obj_id_crud_v2, delete_object: delete_object, get_object: get_object, patch_object: patch_object, post_object: post_object, get_ae_obj_id_crud: get_ae_obj_id_crud, get_ae_obj_li_for_obj_id_crud: get_ae_obj_li_for_obj_id_crud, + get_ae_obj_li_for_obj_id_crud_v2: get_ae_obj_li_for_obj_id_crud_v2, create_ae_obj_crud: create_ae_obj_crud, update_ae_obj_id_crud: update_ae_obj_id_crud, delete_ae_obj_id_crud: delete_ae_obj_id_crud, @@ -1325,7 +898,6 @@ let obj = { delete_hosted_file: delete_hosted_file, get_data_store_obj_w_code: get_data_store_obj_w_code, send_email: send_email, - // test: test; } export let api = obj; // module.exports = api; \ No newline at end of file diff --git a/src/lib/api_get__crud_obj_id.ts b/src/lib/api_get__crud_obj_id.ts new file mode 100644 index 00000000..2a67b3c9 --- /dev/null +++ b/src/lib/api_get__crud_obj_id.ts @@ -0,0 +1,172 @@ +import type { key_val } from '$lib/ae_stores'; +import { get_object } from './api_get_object'; + +// Updated 2023-12-01 +export async function get_ae_obj_id_crud( + { + api_cfg, + no_account_id=false, + obj_type, + obj_id, + use_alt_table=false, + use_alt_base=false, + inc={}, + enabled='enabled', + hidden='not_hidden', + limit=999999, + offset=0, + data={}, + // key, + // jwt=null, + headers={}, + params={}, + timeout=25000, + return_meta=false, + log_lvl=0 + }: { + api_cfg: any, + no_account_id?: boolean, + obj_type: string, + obj_id: string, + use_alt_table?: boolean, + use_alt_base?: boolean, + inc?: any, + enabled?: string, + hidden?: string, + limit?: number, + offset?: number, + data?: any, + // key: string, + // jwt?: string, + headers?: any, + params?: key_val, + timeout?: number, + return_meta?: boolean, + log_lvl?: number + } + ) { + if (log_lvl) { + console.log('*** get_ae_obj_id_crud() ***'); + } + + // data = {}; + // data['super_key'] = key; + // data['jwt'] = jwt; + // NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST + + let endpoint = ''; + if (obj_type == 'account') { + endpoint = `/crud/account/${obj_id}`; + } else if (obj_type == 'address') { + endpoint = `/crud/address/${obj_id}`; + } else if (obj_type == 'archive') { + endpoint = `/crud/archive/${obj_id}`; + } else if (obj_type == 'archive_content') { + endpoint = `/crud/archive/content/${obj_id}`; + } else if (obj_type == 'contact') { + endpoint = `/crud/contact/${obj_id}`; + } else if (obj_type == 'data_store') { + endpoint = `/crud/data_store/${obj_id}`; + } else if (obj_type == 'event') { + endpoint = `/crud/event/${obj_id}`; + } else if (obj_type == 'event_abstract') { + endpoint = `/crud/event/abstract/${obj_id}`; + } else if (obj_type == 'event_badge') { + endpoint = `/crud/event/badge/${obj_id}`; + } else if (obj_type == 'event_device') { + endpoint = `/crud/event/device/${obj_id}`; + } else if (obj_type == 'event_exhibit') { + endpoint = `/crud/event/exhibit/${obj_id}`; + } else if (obj_type == 'event_exhibit_tracking') { + endpoint = `/crud/event/exhibit/tracking/${obj_id}`; + } else if (obj_type == 'event_file') { + endpoint = `/crud/event/file/${obj_id}`; + } else if (obj_type == 'event_location') { + endpoint = `/crud/event/location/${obj_id}`; + } else if (obj_type == 'event_person') { + endpoint = `/crud/event/person/${obj_id}`; + } else if (obj_type == 'event_presentation') { + endpoint = `/crud/event/presentation/${obj_id}`; + } else if (obj_type == 'event_presenter') { + endpoint = `/crud/event/presenter/${obj_id}`; + } else if (obj_type == 'event_session') { + endpoint = `/crud/event/session/${obj_id}`; + } else if (obj_type == 'event_track') { + endpoint = `/crud/event/track/${obj_id}`; + } else if (obj_type == 'grant') { + endpoint = `/crud/grant/${obj_id}`; + } else if (obj_type == 'hosted_file') { + endpoint = `/crud/hosted_file/${obj_id}`; + } else if (obj_type == 'journal') { + endpoint = `/crud/journal/${obj_id}`; + } else if (obj_type == 'journal_entry') { + endpoint = `/crud/journal/entry/${obj_id}`; + } else if (obj_type == 'order') { + endpoint = `/crud/order/${obj_id}`; + } else if (obj_type == 'order_line') { + endpoint = `/crud/order/line/${obj_id}`; + } else if (obj_type == 'page') { + endpoint = `/crud/page/${obj_id}`; + } else if (obj_type == 'person') { + endpoint = `/crud/person/${obj_id}`; + } else if (obj_type == 'post') { + endpoint = `/crud/post/${obj_id}`; + } else if (obj_type == 'post_comment') { + endpoint = `/crud/post/comment/${obj_id}`; + } else if (obj_type == 'site') { + endpoint = `/crud/site/${obj_id}`; + } else if (obj_type == 'site_domain') { + endpoint = `/crud/site/domain/${obj_id}`; + } else if (obj_type == 'sponsorship_cfg') { + endpoint = `/crud/sponsorship/cfg/${obj_id}`; + } else if (obj_type == 'sponsorship') { + endpoint = `/crud/sponsorship/${obj_id}`; + // } else if (obj_type == 'user') { + // endpoint = `/crud/user/${obj_id}`; + } else { + console.log(`Unknown object type: ${obj_type}`); + return false; + } + if (log_lvl) { + console.log('Endpoint:', endpoint); + } + + params['use_alt_table'] = use_alt_table; + params['use_alt_base'] = use_alt_base; + + if (log_lvl) { + console.log('Params:', params); + } + + if (no_account_id) { + headers['x-no-account-id'] = 'Nothing to See Here'; + delete headers['x-account-id']; + delete api_cfg['headers']['x-account-id']; + // headers['x-account-id'] = null; + // headers['x-account-id'] = '_XY7DXtc9Mxx'; + // params['x_no_account_id_token'] = 'Nothing to See Here'; + + // Remove the x-account-id header + // if (headers['x-account-id']) { + // delete headers['x-account-id']; + // } + + // headers['x-account-id'] = null; + // headers['x-no-account-id-token'] = 'Nothing to See Here'; // get_object() will fix the underscores to dashes + } + + let object_obj_get_promise = await get_object({ + api_cfg: api_cfg, + endpoint: endpoint, + headers: headers, + params: params, + timeout: timeout, + log_lvl: log_lvl + }); + + if (log_lvl > 1) { + console.log(object_obj_get_promise); + } + + return object_obj_get_promise; +} \ No newline at end of file diff --git a/src/lib/api_get__crud_obj_li_v1.ts b/src/lib/api_get__crud_obj_li_v1.ts new file mode 100644 index 00000000..050c3ccc --- /dev/null +++ b/src/lib/api_get__crud_obj_li_v1.ts @@ -0,0 +1,226 @@ +import type { key_val } from '$lib/ae_stores'; +import { get_object } from './api_get_object'; + +// The lookup "obj_type" should broken out into a separate function. - 2024-08-07 +// Updated 2023-11-15 +export async function get_ae_obj_li_for_obj_id_crud( + { + api_cfg, + obj_type, + for_obj_type, + for_obj_id, // NOTE: Changed 2023-12-06 to no longer required + use_alt_table=false, + use_alt_base=false, + inc={}, + enabled='enabled', + hidden='not_hidden', + order_by_li=null, + limit=999999, + offset=0, + // key, + // jwt=null, + headers={}, + params_json=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the API endpoint. Example: { "fulltext_search": { "default_qry_str": "Search string for default", "address_default_qry_str": "Search string for address", "contact_1_default_qry_str": "Search string for contact_1" } } + // json_obj=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. + params={}, + return_meta=false, + log_lvl=1 + }: { + api_cfg: any, + obj_type: string, + for_obj_type: string, + for_obj_id?: string, + use_alt_table?: boolean, + use_alt_base?: boolean, + inc?: key_val + enabled?: string, + hidden?: string, + order_by_li?: any, + limit?: number, + offset?: number, + // key: string, + // jwt?: string, + headers?: any, + params_json?: any, + // json_obj?: any, + params?: key_val, + return_meta?: boolean, + log_lvl?: number + } + ) { + if (log_lvl) { + console.log('*** get_ae_obj_li_for_obj_id_crud() ***'); + } + + // data = {}; + // data['super_key'] = key; + // data['jwt'] = jwt; + // NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST + + // const endpoint = `/crud/${obj_type}/list`; + + let endpoint = ''; + if (obj_type == 'account') { + endpoint = `/crud/account/list`; + } else if (obj_type == 'address') { + endpoint = `/crud/address/list`; + } else if (obj_type == 'archive') { + endpoint = `/crud/archive/list`; + } else if (obj_type == 'archive_content') { + endpoint = `/crud/archive/content/list`; + } else if (obj_type == 'contact') { + endpoint = `/crud/contact/list`; + } else if (obj_type == 'data_store') { + endpoint = `/crud/data_store/list`; + } else if (obj_type == 'event') { + endpoint = `/crud/event/list`; + } else if (obj_type == 'event_abstract') { + endpoint = `/crud/event/abstract/list`; + } else if (obj_type == 'event_badge') { + endpoint = `/crud/event/badge/list`; + } else if (obj_type == 'event_device') { + endpoint = `/crud/event/device/list`; + } else if (obj_type == 'event_exhibit') { + endpoint = `/crud/event/exhibit/list`; + } else if (obj_type == 'event_exhibit_tracking') { + endpoint = `/crud/event/exhibit/tracking/list`; + } else if (obj_type == 'event_file') { + endpoint = `/crud/event/file/list`; + } else if (obj_type == 'event_location') { + endpoint = `/crud/event/location/list`; + } else if (obj_type == 'event_person') { + endpoint = `/crud/event/person/list`; + } else if (obj_type == 'event_presentation') { + endpoint = `/crud/event/presentation/list`; + } else if (obj_type == 'event_presenter') { + endpoint = `/crud/event/presenter/list`; + } else if (obj_type == 'event_session') { + endpoint = `/crud/event/session/list`; + } else if (obj_type == 'event_track') { + endpoint = `/crud/event/track/list`; + } else if (obj_type == 'grant') { + endpoint = `/crud/grant/list`; + } else if (obj_type == 'hosted_file') { + endpoint = `/crud/hosted_file/list`; + } else if (obj_type == 'journal') { + endpoint = `/crud/journal/list`; + } else if (obj_type == 'journal_entry') { + endpoint = `/crud/journal/entry/list`; + } else if (obj_type == 'order') { + endpoint = `/crud/order/list`; + } else if (obj_type == 'order_line') { + endpoint = `/crud/order/line/list`; + } else if (obj_type == 'page') { + endpoint = `/crud/page/list`; + } else if (obj_type == 'person') { + endpoint = `/crud/person/list`; + } else if (obj_type == 'post') { + endpoint = `/crud/post/list`; + } else if (obj_type == 'post_comment') { + endpoint = `/crud/post/comment/list`; + } else if (obj_type == 'site') { + endpoint = `/crud/site/list`; + } else if (obj_type == 'sponsorship_cfg') { + endpoint = `/crud/sponsorship/cfg/list`; + } else if (obj_type == 'sponsorship') { + endpoint = `/crud/sponsorship/list`; + // } else if (obj_type == 'user') { + // endpoint = `/crud/user/list`; + // } else if (obj_type == 'lu' && for_obj_type == 'country_subdivision') { + // endpoint = `/crud/lu/country_subdivision/list`; + // for_obj_type = null; + // } else if (obj_type == 'lu' && for_obj_type == 'country') { + // endpoint = `/crud/lu/country/list`; + // for_obj_type = null; + // } else if (obj_type == 'lu' && for_obj_type == 'time_zone') { + // endpoint = `/crud/lu/time_zone/list`; + // for_obj_type = null; + } else { + console.log(`Unknown object type: ${obj_type}`); + return false; + } + if (log_lvl) { + console.log('Endpoint:', endpoint); + } + + if (for_obj_type) { + params['for_obj_type'] = for_obj_type; + } + if (for_obj_id) { + params['for_obj_id'] = for_obj_id; + } + + params['use_alt_table'] = use_alt_table; + params['use_alt_base'] = use_alt_base; + + /* Need to deal with inc params here */ + + let allowed_enabled_list = ['all', 'enabled', 'not_enabled'] + if (allowed_enabled_list.includes(enabled) ) { + params['enabled'] = enabled; + } + + let allowed_hidden_list = ['all', 'hidden', 'not_hidden']; + if (allowed_hidden_list.includes(hidden) ) { + params['hidden'] = hidden; + } + + // NOTE: The order_by_li variable is in the "headers" because if is a the URL GET params do not handle multiple values very well. Maybe base64 encore in the future or something? Reminder that GET requests should not have a body (no JSON). + // NOTE: The order_by_li should be a key value pair of the property/DB field to sort and how to sort (ASC or DESC) + if (order_by_li) { + headers['order_by_li'] = order_by_li; + } + + if (limit >= 0) { + params['limit'] = limit; + } + + if (offset >= 0) { + params['offset'] = offset; + } + + if (params_json) { + // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. + // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). + if (log_lvl) { + console.log('JSON Object:', params_json); + console.log(JSON.stringify(params_json)); + } + // NOTE: "jp" stands for "JSON Params" + params['jp'] = encodeURIComponent(JSON.stringify(params_json)); + if (params['jp'].length > 2083) { + console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['jp'].length} [THIS DOES NOT EXIST YET]`); + return false; + } + } + + // if (json_obj) { + // // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. + // // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). + // console.log('JSON Object:', json_obj); + // params['json_str'] = encodeURIComponent(JSON.stringify(json_obj)); + // if (params['json_str'].length > 2083) { + // console.log(`The JSON object is too large to be used as a GET parameter. The overall max URL length is 2083 characters. Please use the POST endpoint instead. Length = ${params['json_str'].length} [THIS DOES NOT EXIST YET]`); + // return false; + // } + // } + + if (log_lvl) { + console.log('Params:', params); + } + + let object_li_get_promise = await get_object({ + api_cfg: api_cfg, + endpoint: endpoint, + headers: headers, + params: params, + return_meta: return_meta, + log_lvl: log_lvl + }); + + if (log_lvl > 1) { + console.log(object_li_get_promise); + } + + return object_li_get_promise; +} \ No newline at end of file diff --git a/src/lib/api_get__crud_obj_li_v2.ts b/src/lib/api_get__crud_obj_li_v2.ts index 318af544..f8ff36d5 100644 --- a/src/lib/api_get__crud_obj_li_v2.ts +++ b/src/lib/api_get__crud_obj_li_v2.ts @@ -1,54 +1,54 @@ import type { key_val } from '$lib/ae_stores'; -import { get_object } from './api_get_object'; // Exported at the end of this file +import { get_object } from './api_get_object'; // The lookup "obj_type" should broken out into a separate function. - 2024-08-07 // Updated 2023-11-15 export async function get_ae_obj_li_for_obj_id_crud_v2( - { - api_cfg, - obj_type, - for_obj_type, - for_obj_id, // NOTE: Changed 2023-12-06 to no longer required - use_alt_tbl=false, - use_alt_mdl=false, - use_alt_exp=false, - inc={}, - enabled='enabled', - hidden='not_hidden', - order_by_li=null, - limit=999999, - offset=0, - // key, - // jwt=null, - headers={}, - params_json=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the API endpoint. Example: { "fulltext_search": { "default_qry_str": "Search string for default", "address_default_qry_str": "Search string for address", "contact_1_default_qry_str": "Search string for contact_1" } } - // json_obj=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. - params={}, - return_meta=false, - log_lvl=1 - }: { - api_cfg: any, - obj_type: string, - for_obj_type: string, - for_obj_id?: string, - use_alt_tbl?: boolean|string, - use_alt_mdl?: boolean|string, - use_alt_exp?: boolean|string, - inc?: key_val - enabled?: string, - hidden?: string, - order_by_li?: any, - limit?: number, - offset?: number, - // key: string, - // jwt?: string, - headers?: any, - params_json?: any, - // json_obj?: any, - params?: key_val, - return_meta?: boolean, - log_lvl?: number - } + { + api_cfg, + obj_type, + for_obj_type, + for_obj_id, // NOTE: Changed 2023-12-06 to no longer required + use_alt_tbl=false, + use_alt_mdl=false, + use_alt_exp=false, + inc={}, + enabled='enabled', + hidden='not_hidden', + order_by_li=null, + limit=999999, + offset=0, + // key, + // jwt=null, + headers={}, + params_json=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the API endpoint. Example: { "fulltext_search": { "default_qry_str": "Search string for default", "address_default_qry_str": "Search string for address", "contact_1_default_qry_str": "Search string for contact_1" } } + // json_obj=null, // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. + params={}, + return_meta=false, + log_lvl=1 + }: { + api_cfg: any, + obj_type: string, + for_obj_type: string, + for_obj_id?: string, + use_alt_tbl?: boolean|string, + use_alt_mdl?: boolean|string, + use_alt_exp?: boolean|string, + inc?: key_val + enabled?: string, + hidden?: string, + order_by_li?: any, + limit?: number, + offset?: number, + // key: string, + // jwt?: string, + headers?: any, + params_json?: any, + // json_obj?: any, + params?: key_val, + return_meta?: boolean, + log_lvl?: number + } ) { if (log_lvl) { console.log('*** get_ae_obj_li_for_obj_id_crud() ***'); @@ -192,8 +192,10 @@ export async function get_ae_obj_li_for_obj_id_crud_v2( if (params_json) { // NOTE: This is a JSON object that needs to be safely converted to a string for the params. This is used for the search endpoint. // Max characters for a GET request is 2083. This is a limitation of the browser (Microsoft IE and Edge). - console.log('JSON Object:', params_json); - console.log(JSON.stringify(params_json)); + if (log_lvl) { + console.log('JSON Object:', params_json); + console.log(JSON.stringify(params_json)); + } // NOTE: "jp" stands for "JSON Params" params['jp'] = encodeURIComponent(JSON.stringify(params_json)); if (params['jp'].length > 2083) { diff --git a/src/lib/electron_native.js b/src/lib/electron_native.js new file mode 100644 index 00000000..4bab8047 --- /dev/null +++ b/src/lib/electron_native.js @@ -0,0 +1,1391 @@ +'use strict'; +/* This should only contain functions that can not be pulled easily into Svelte */ +/* +Exported functions in use: +* load_config() +* check_hash_file_cache() +* download_hash_file_to_cache() +* open_hash_file_to_temp() +* open_local_file() +* kill_processes() +* run_osascript() +* run_cmd() +* get_device_info + +Local functions in use: +* open_local_file() +*/ + +const child_process = require('child_process'); +const crypto = require('crypto'); +const fs = require('fs'); +const fs_promises = require('node:fs/promises'); +const os = require('os'); +const path = require('path'); +const { ipcRenderer } = require('electron'); + +// import psList from 'ps-list'; +// const ps_list = require('ps-list'); + +let home_directory = require('os').homedir(); +console.log('Home: '+home_directory); + +let tmp_directory = require('os').tmpdir(); +console.log('Temporary: '+tmp_directory); + +let app_root_path = path.join(home_directory, 'OSIT/native_app'); // macOS default + +let config = null; + +let default_osit_sync_app_root_relative_path = 'OSIT/Speaker Ready System/Admin Share/Custom Applications'; // Mainly for macOS laptops +let default_osit_sync_app_root_path = path.join(home_directory, default_osit_sync_app_root_relative_path); // macOS default +let default_osit_sync_native_app_relative_path = 'OSIT/Speaker Ready System/Admin Share/Custom Applications/osit_binaries/osit_aether.current.app'; // Mainly for macOS laptops +let default_native_app_relative_path = 'OSIT/native_app/osit_aether.app'; // Mainly for macOS laptops +let default_current_native_app_relative_path = 'OSIT/native_app/osit_aether.current.app'; // Mainly for macOS laptops +let test_native_app_relative_path = 'OSIT/native_app/osit_aether.remote.app'; // Mainly for macOS laptops + + + +// exports.check_for_native_app_update = function () { +// console.log('*** Aether App Native export: check_for_native_app_update() ***'); +// +// let config_directory = null; +// +// if (os.platform == 'darwin') { +// config_directory = path.join(home_directory, 'OSIT/native_app'); +// console.log('macOS config directory: '+config_directory); +// +// let electron_app_path = path.join(home_directory, default_native_app_relative_path); +// let test_stats = fs.statSync(electron_app_path); +// console.log(test_stats); +// +// if (test_stats.ctimeMs < 1684182140759) { +// console.log(`This may be an old version!`); +// } +// } else if (os.platform == 'linux') { +// config_directory = path.join(home_directory, '.config/OSIT'); +// console.log('Linux config directory: '+config_directory); +// } else { +// console.log('Unknown OS... Is this Windows?'); +// return false; +// } +// +// } + + +exports.load_init_config = function () { + console.log('*** Aether App Native export: load_init_config() ***'); + + let cwd = process.cwd(); + console.log(`CWD: ${cwd}`); + + try { + if (cwd == '/') { + cwd = home_directory; + } + let directory_list = fs.readdirSync(cwd) + console.log('CWD Contents:', directory_list); + } catch (err) { + console.error(err); + } + + // let home_directory = require('os').homedir(); + // console.log('Home: '+home_directory); + + // let tmp_directory = require('os').tmpdir(); + // console.log('Temporary: '+tmp_directory); + + // let config = null; + let config_directory = null; + // let default_config_path = path.join(process.cwd(),'ae_native_app_config.current.json'); + let default_config_path = 'ae_native_app_config.current.json'; + console.log(`Default Config File (path): ${default_config_path}`); + let config_path = null; + + // Set the config path for macOS or Linux + if (os.platform == 'darwin') { + // config_directory = path.join(home_directory, 'Library/Application Support/OSIT'); + config_directory = path.join(home_directory, 'OSIT/native_app'); + console.log('macOS config directory: '+config_directory); + + let electron_app_path = path.join(home_directory, default_native_app_relative_path); + let test_stats = fs.statSync(electron_app_path); + console.log(test_stats); + } else if (os.platform == 'linux') { + config_directory = path.join(home_directory, '.config/OSIT'); + console.log('Linux config directory: '+config_directory); + } + + // Look for the config file and copy the default if not found. + if (fs.existsSync(config_directory)) { + console.log('Config directory found: '+config_directory); + } else { + fs.mkdirSync(config_directory); + console.log('Config directory created: '+config_directory); + + //default_config_path = path.join(process.cwd(),'ae_native_app_config.current.json'); + // config_path = path.join(config_directory, 'ae_native_app_config.json'); + // fs.copyFileSync(default_config_path, config_path); + // console.log('Default config file copied: '+config_directory); + } + + config_path = path.join(config_directory, 'ae_native_app_config.json'); + + // Attempt to open the config file. The preferred location is based on the OS's config directory. + if (fs.existsSync(config_path)) { + console.log(`Config file (ae_native_app_config.json) found under ${config_directory}`); + } else if (!fs.existsSync(config_path) && fs.existsSync(default_config_path)) { + fs.copyFileSync(default_config_path, config_path); + console.log('Default config file copied: '+config_directory); + + // config = JSON.parse(fs.readFileSync(config_path)); + // console.log('Config file read.'); + } else if (fs.existsSync(path.join(cwd, 'ae_native_app_config.json'))) { + //fs.copyFileSync(default_config_path, config_path); + //console.log('Default config file copied: '+config_directory); + + console.log(`Config file (config.json) not found under ${config_directory}. Using config in CWD. ${cwd}`); + config_path = path.join(cwd, 'ae_native_app_config.json'); + + console.log(`Config file (config.json) not found under ${config_directory}. Using config in CWD. ${cwd}`); + let found_config_path = path.join(cwd, 'ae_native_app_config.json'); + + fs.copyFileSync(found_config_path, config_path); + console.log(`Found config file copied: ${config_directory}`); + } else if (fs.existsSync(path.join(cwd, 'ae_native_app_config.current.json'))) { + console.log(`Config file (config.json) not found under ${config_directory} or CWD. Using default config in CWD. ${cwd}`); + default_config_path = path.join(cwd, 'ae_native_app_config.current.json'); + + fs.copyFileSync(default_config_path, config_path); + console.log(`Default config file copied: ${config_directory}`); + } else { + console.log('Can not find a config file.'); + + return false; + } + + let local_config = JSON.parse(fs.readFileSync(config_path)); + console.log('Config file read.', local_config); + config = local_config; + + // Do some quick clean up. + + config.home_directory = home_directory; // From the OS platform + config.tmp_directory = tmp_directory; // From the OS platform + + config.app_root_path = config.app_root_path.replace('[home]', home_directory); + config.app_root_path = config.app_root_path.replace('[tmp]', tmp_directory); + console.log(`App Root Path: ${config.app_root_path}`); + + config.local_file_cache_path = config.local_file_cache_path.replace('[home]', home_directory); + config.local_file_cache_path = config.local_file_cache_path.replace('[tmp]', tmp_directory); + console.log(`Local File Cache Path: ${config.local_file_cache_path}`); + if (fs.existsSync(config.local_file_cache_path)) { + } else { + fs.mkdirSync(config.local_file_cache_path); + console.log(`Host file cache directory created: ${config.local_file_cache_path}`); + } + + config.host_file_temp_path = config.host_file_temp_path.replace('[home]', home_directory); + config.host_file_temp_path = config.host_file_temp_path.replace('[tmp]', tmp_directory); + console.log(`Host file temp path: ${config.host_file_temp_path}`); + if (fs.existsSync(config.host_file_temp_path)) { + } else { + fs.mkdirSync(config.host_file_temp_path); + console.log(`Host file temp directory created: ${config.host_file_temp_path}`); + } + + // NOTE: This is not ideal... + if (!config.account_id) { + if (config.event_id == 'pjrcghqwert' || config.event_id == 'nmBfuGFeR0k') { + config.account_id = '_XY7DXtc9MY'; + } else if (config.event_id == 'r8c-rr-I4-52') { // CMSC + config.account_id = '8Gfxbxr19Nw'; + } else if (config.event_id == 'NeC-hm-MF-Q3') { // LCI + config.account_id = 'xFP7AhU8Zlc'; + } else if (config.event_id == 'Y7v-Zg-fe-gu') { // NCSD + config.account_id = 'FrodrLD7B4w'; + } else if (config.event_id == '9jW-Db-SF-wt') { // AAPOR + config.account_id = 'j5EBhRDqPuw'; + } else if (config.event_id == 'zJP-Ld-A7-I4') { // BGH + config.account_id = 'GpLf_bnywCs'; + } else { + console.log('WARNING WARNING WARNING: The account ID could not be guessed. Using the default of 1. <<< WARNING'); + config.account_id = '_XY7DXtc9MY'; + } + } + + let import_config_to_ipc_result = ipcRenderer.invoke('import_config', config).then((result) => { + console.log('IPC import config finished'); + // console.log(result); + return result; // Result should be "true" + }) + + //console.log(config); + return config; +} + + +exports.load_full_config = function (init_config) { + console.log('*** Aether App Native export: load_config() ***'); + + console.log('Init config.', init_config); + // config = init_config; + + config = get_url_cfg(init_config).then((cfg) => { + console.log('URL config file downloaded.', cfg); + + let new_config = init_config; + + // Update with remote device config settings from API. + new_config.account_id = cfg.account_id_random; + new_config.event_id = cfg.event_id_random; + new_config.event_device_id = cfg.event_device_id; + new_config.event_location_id = cfg.event_location_id; + new_config.event_session_id = cfg.event_session_id; + + new_config.check_event_device_loop_period = cfg.check_event_device_loop_period; + new_config.check_event_location_loop_period = cfg.check_event_location_loop_period; + new_config.check_event_loop_period = cfg.check_event_loop_period; + new_config.check_event_session_loop_period = cfg.check_event_session_loop_period; + + new_config.record_audio = cfg.record_audio; + new_config.record_video = cfg.record_video; + new_config.recording_path = cfg.recording_path; + new_config.recording_path = new_config.recording_path.replace('[home]', home_directory); + new_config.recording_path = new_config.recording_path.replace('[tmp]', home_directory); + + new_config.home_directory = home_directory; // From the OS platform + new_config.tmp_directory = tmp_directory; // From the OS platform + + new_config.app_root_path = new_config.app_root_path.replace('[home]', home_directory); + new_config.app_root_path = new_config.app_root_path.replace('[tmp]', tmp_directory); + console.log(`App Root Path: ${new_config.app_root_path}`); + + new_config.local_file_cache_path = new_config.local_file_cache_path.replace('[home]', home_directory); + new_config.local_file_cache_path = new_config.local_file_cache_path.replace('[tmp]', tmp_directory); + console.log(`Local File Cache Path: ${new_config.local_file_cache_path}`); + if (fs.existsSync(new_config.local_file_cache_path)) { + } else { + fs.mkdirSync(new_config.local_file_cache_path); + console.log(`Host file cache directory created: ${new_config.local_file_cache_path}`); + } + + new_config.host_file_temp_path = new_config.host_file_temp_path.replace('[home]', home_directory); + new_config.host_file_temp_path = new_config.host_file_temp_path.replace('[tmp]', tmp_directory); + console.log(`Host file temp path: ${new_config.host_file_temp_path}`); + if (fs.existsSync(new_config.host_file_temp_path)) { + } else { + fs.mkdirSync(new_config.host_file_temp_path); + console.log(`Host file temp directory created: ${new_config.host_file_temp_path}`); + } + + let import_config_to_ipc_result = ipcRenderer.invoke('import_config', new_config).then((result) => { + console.log('IPC import config finished'); + // console.log(result); + return result; // Result should be "true" + }) + + return new_config + }) + + //console.log(config); + return config; +} + + +async function get_url_cfg(cfg) { + let base_url = `${cfg.api_protocol}://${cfg.api_server}:${cfg.api_port}/${cfg.api_path}`; + + let axios_api = axios.create({ + baseURL: base_url, + timeout: 60000, // in milliseconds; 60000 = 60 seconds + /* other custom settings */ + }); + // axios_api.defaults.headers = cfg['headers']; + + // axios.defaults.baseURL = `${cfg.api_protocol}://${cfg.api_server}:${cfg.api_port}/${cfg.api_path}`; + axios_api.defaults.headers.common['Access-Control-Allow-Origin'] = cfg.access_control_allow_origin; // '*'; // app_cfg.access_control_allow_origin; + axios_api.defaults.headers.common['content-type'] = 'application/json'; + axios_api.defaults.headers.common['x-aether-api-key'] = cfg.api_secret_key; + // axios_api.defaults.headers.common['x-account-id'] = cfg.account_id; + + let event_device_id = 'dbgMWS3KEHE'; + let endpoint = `/event/device/${event_device_id}`; + + let params = {'event_device_code': 'asdf'}; + + let response_data_promise = await axios_api.get( + endpoint, + { + params: params, + onDownloadProgress: (progressEvent) => { + let percent_completed = Math.round( + (progressEvent.loaded * 100) / progressEvent.total + ); + console.log('GET Data Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed); + + // temp_get_object_percent_completed = percent_completed; + } + } + ) + .then(function (response) { + console.log(`Response: ${response}`); + + let return_data = response.data['data']; + if (Array.isArray(return_data)) { + console.log(`Data result is an array/list. Array length: ${return_data.length}`); + } else { + console.log(`Data result is a dictionary/object, not an array/list.`); + } + return return_data; + }) + .catch(function (error) { + console.log(`Base URL: ${base_url} | Endpoint: ${endpoint}`); + + console.log('Error Message:', error.message); // Is this needed here or below in the in the else portion??? + if (error.response) { + console.log(`Response Status: ${error.response.status}; Status Text: ${error.response.statusText}`); + } else { + console.log('Error:', error); + } + + if (error.response && error.response.status === 404) { + return null; // Returning null since there were no results + } + return false; // Returning false since something may have gone wrong. Also more in line with what the API returns. + }); + + return response_data_promise; +} + + + + + + + + +// Check for local file +// Updated 2022-05-06 +exports.check_local_file = async function ({local_file_path, filename}) { + console.log('*** Electron framework export: check_local_file() ***'); + // console.log('Check for local file'); + console.log(`Local File Path: ${local_file_path}; Filename: ${filename}`); + + let full_local_file_path = path.join(local_file_path, filename); + console.log(full_local_file_path); + + if (fs.existsSync(full_local_file_path)) { + console.log(`Local file exists: ${full_local_file_path}`); + return true; + } else { + return false; + } +} + + +// Check local hash file cache +// Used by Svelte Event Launcher +// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11 +// Updated 2022-05-06 +exports.check_hash_file_cache = async function ({local_file_cache_path, hash}) { + // console.log('*** Electron framework export: check_hash_file_cache() ***'); + // console.log('Check local hash file cache'); + console.log(`*** Electron framework export: check_hash_file_cache() *** Host File Cache Path: ${local_file_cache_path}; Hash: ${hash}`); + + let hash_filename = `${hash}.file`; + + let subdirectory = hash_filename.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, subdirectory); + if (fs.existsSync(subdirectory_path)) { + } else { + console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`); + return false; + } + + let hash_file_cache_path = path.join(subdirectory_path, hash_filename); + // console.log(hash_file_cache_path); + + if (fs.existsSync(hash_file_cache_path)) { + // console.log(`Hashed file exists in cache: ${hash_file_cache_path}`); + return true; + } else { + console.log(`Hashed file not found in cache: ${hash_file_cache_path}`); + return false; + } +} + + +// Check local hash file cache +// Used by Svelte Event Launcher +// Updated 2022-10-12 +exports.check_hash_file_cache_v2 = async function ({local_file_cache_path, hash, verify_hash=false}) { + console.log('*** Aether App Native export: check_hash_file_cache_v2() ***'); + console.log(`Local File Cache Path: ${local_file_cache_path}; Hash: ${hash}`); + + if (fs.existsSync(local_file_cache_path)) { + } else { + // This should not happen. The directory needs to be created. + console.log(`Cache directory for hashed files was not found: ${local_file_cache_path}`); + return false; + } + + let hash_filename = `${hash}.file`; + + let hash_subdirectory = hash_filename.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, hash_subdirectory); + + if (fs.existsSync(subdirectory_path)) { + } else { + // This should not happen. The subdirectory needs to be created. + console.log(`Hashed file subdirectory not found in cache directory: ${subdirectory_path}`); + return false; + } + + let full_cached_hash_path = path.join(subdirectory_path, hash_filename); + + if (fs.existsSync(full_cached_hash_path)) { + // console.log(`Hashed file exists in cache: ${full_cached_hash_path}`); + + if (verify_hash) { + const file_buffer = fs.readFileSync(full_cached_hash_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 { + // This should only happen if the file is actively being downloaded or it is corrupt. + console.log('File hash does not match', file_hash_sha256_check); + return false; + } + } + return true; + } else { + console.log(`Hashed file not found in cache: ${full_cached_hash_path}`); + return null; + } +} + + +// Download hash file to cache +// Used by Svelte Event Launcher +// Updated 2022-05-06 +exports.download_hash_file_to_cache = async function ({api_base_url, local_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(`*** Electron framework export: download_hash_file_to_cache() *** Base URL: ${api_base_url}; Host File Cache Path: ${local_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 subdirectory = hash_filename.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, subdirectory); + if (fs.existsSync(subdirectory_path)) { + } else { + fs.mkdirSync(subdirectory_path); + console.log(`Subdirectory directory created: ${subdirectory_path}`); + } + + let hash_file_cache_path = path.join(subdirectory_path, hash_filename); + if (fs.existsSync(hash_file_cache_path)) { + if (check_hash) { + const file_buffer = fs.readFileSync(hash_file_cache_path); + const file_hash_sha256 = crypto.createHash('sha256'); + file_hash_sha256.update(file_buffer); + + const file_hash_sha256_check = file_hash_sha256.digest('hex');S + if (file_hash_sha256_check == hash) { + console.log('File hash match', file_hash_sha256_check); + return true; + } else { + // This should only happen if the file is actively being downloaded or it is corrupt. + console.log('File hash does not match', file_hash_sha256_check); + return false; + } + } + } + // console.log(`!!!ABOUT TO CALL DOWNLOAD FILE HANDLER!!! exports.download_hash_file_to_cache(); Base URL: ${api_base_url}`); + let download_file_result = await ipcRenderer.invoke('download_file', api_base_url, endpoint, hash_file_cache_path).then((result) => { + if (result) { + console.log('IPC download file process finished successfully'); + return true; + } else if (result == null) { + console.log('IPC Download Result (file not found?):', result); + return null; + } else { + console.log('IPC Download Result (file being downloaded or something went wrong):', result); + return false; + } + }); + // console.log(`!!!DONE WITH DOWNLOAD FILE HANDLER!!! exports.download_hash_file_to_cache(); Base URL: ${api_base_url}`); + + return download_file_result; +} + + +// Download hash file to cache +// Used by Svelte Event Launcher +// Updated 2023-05-26 +exports.download_hash_file_to_cache_v2 = async function ({api_base_url, api_base_url_backup, local_file_cache_path, event_file_id=null, hash=null, verify_hash=true, overwrite_existing=false}) { + console.log('*** Aether App Native export: download_hash_file_to_cache_v2() ***'); + console.log(`Base URL: ${api_base_url}; Base URL Backup: ${api_base_url_backup}; Host File Cache Path: ${local_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`); + + if (fs.existsSync(local_file_cache_path)) { + } else { + // This should not happen. The directory needs to be created. + console.log(`Cache directory for hashed files was not found: ${local_file_cache_path}`); + return false; + } + + let endpoint = `/event/file/${event_file_id}/download`; + + let hash_filename = `${hash}.file`; + + let hash_subdirectory = hash_filename.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, hash_subdirectory); + + if (fs.existsSync(subdirectory_path)) { + } else { + fs.mkdirSync(subdirectory_path); + console.log(`Hashed file subdirectory directory created in cache directory: ${subdirectory_path}`); + } + + let full_cached_hash_path = path.join(subdirectory_path, hash_filename); + + if (fs.existsSync(full_cached_hash_path)) { + console.log(`Hashed file exists in cache: ${full_cached_hash_path}`); + + if (verify_hash) { + const file_buffer = fs.readFileSync(full_cached_hash_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); + if (overwrite_existing) { + console.log('Going to overwrite the existing file even though the hash matches.'); + } else { + return true; + } + } else { + // This should only happen if the file is actively being copied or it is corrupt. + 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; + } + } + } + } + + const tmp_full_save_path = full_cached_hash_path+'.tmp'; + + if (fs.existsSync(tmp_full_save_path)) { + 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; + return 'tmp'; + } + } + + console.log(`Trying IPC API download from: ${api_base_url}`); + let download_file_result = await ipcRenderer.invoke('download_file', api_base_url, endpoint, full_cached_hash_path, hash, verify_hash, overwrite_existing).then(async (result) => { + + if (result) { + console.log(`IPC API download file process finished successfully: ${hash}`); + return true; + } else if (result == null) { + console.log(`IPC API download result (file not found?):`, result); + return null; + } else if (result === 'in_progress') { + console.log(`IPC API primary download IN PROGRESS: ${hash}`); + // return false; // Should return result instead??? + return result; + } else if (result === 'tmp') { + console.log(`IPC API primary download FOUND TMP: ${hash}`); + // return false; // Should return result instead??? + return result; + } else { + console.log(`IPC API download result (file being downloaded or something went wrong): ${hash}`, result); + return false; + + // if (api_base_url_backup) { + // console.log(`Download FAILED! Trying again with backup API server. API Backup: ${api_base_url_backup}`); + // let download_backup_file_result = await ipcRenderer.invoke('download_file', api_base_url_backup, endpoint, full_cached_hash_path, hash, verify_hash, overwrite_existing).then((result_backup) => { + // if (result_backup) { + // console.log('IPC download file (from backup) process finished successfully'); + // return true; + // } else if (result_backup == null) { + // console.log('IPC Download Result (from backup) (file not found?):', result_backup); + // return null; + // } else { + // console.log('IPC Download Result (from backup) (file being downloaded or something went wrong):', result_backup); + // return false; + // } + // }); + // } else { + // return false; + // } + } + return true; + // }); + }).then(async (result) => { + if (result === false) { + console.log(`IPC API primary download FAILED!`); // Trying again with backup API server. API Backup: ${api_base_url_backup}`); + if (api_base_url_backup) { + console.log(`IPC API primary download FAILED! Trying again with backup API server. API Backup: ${api_base_url_backup}`); + console.log(`Trying IPC API backup download from: ${api_base_url_backup}`); + let download_backup_file_result = await ipcRenderer.invoke('download_file', api_base_url_backup, endpoint, full_cached_hash_path, hash, verify_hash, overwrite_existing).then((result_backup) => { + if (result_backup) { + console.log(`IPC API backup download file (from backup) process finished successfully: ${hash}`); + return true; + } else if (result_backup == null) { + console.log('IPC API backup download result (file not found?):', result_backup); + return null; + } else { + console.log(`IPC API backup download result (file being downloaded or something went wrong): ${hash}`, result_backup); + return false; + } + }); + return download_backup_file_result; + } else { + console.log(`IPC Download FAILED! No backup API server passed. Returning false.`); + return false; + } + + } else if (result === 'in_progress') { + console.log(`IPC API primary download IN PROGRESS: ${hash}`); + return false; // Should return result instead??? + } else if (result === 'tmp') { + console.log(`IPC API primary download FOUND TMP: ${hash}`); + return false; // Should return result instead??? + } else { + return result; + } + }); + + // if (download_file_result === false) { + // console.log(`Download FAILED! Trying again with backup API server. API Backup: ${api_base_url_backup}`) + // download_file_result = await ipcRenderer.invoke('download_file', api_base_url_backup, endpoint, full_cached_hash_path, hash, verify_hash, overwrite_existing).then((result) => { + // if (result) { + // console.log('IPC download file (from backup) process finished successfully'); + // return true; + // } else if (result == null) { + // console.log('IPC Download Result (from backup) (file not found?):', result); + // return null; + // } else { + // console.log('IPC Download Result (from backup) (file being downloaded or something went wrong):', result); + // return false; + // } + // }); + // } + + return download_file_result; +} + + + +// Open cached hash file after copying to temp directory +// Used by Svelte Event Launcher +// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11 +// Updated 2022-05-06 +exports.open_hash_file_to_temp = async function ({local_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: ${local_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`); + + let subdirectory = hash.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, subdirectory); + if (fs.existsSync(subdirectory_path)) { + } else { + console.log(`Hashed file subdirectory not found in cache: ${subdirectory_path}`); + return false; + } + + let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', subdirectory_path, hash, host_file_temp_path, filename).then((result) => { + console.log('IPC open hash file to temp finished'); + console.log(result); + return true; + }) + + // let result = await ipcRenderer.send('open_local_file', local_file_cache_path, hash, host_file_temp_path, filename); + // console.log(result); + + console.log(open_hash_file_to_temp_result); + console.log('End: open_hash_file_to_temp()'); + if (open_hash_file_to_temp_result) { + console.log('File opened successfully'); + return true; + } else { + console.log('File was not opened successfully'); + return false; + } +} + + + +// Open cached hash file after copying to temp directory +// Used by Svelte Event Launcher +// Updated 2022-10-12 +exports.open_hash_file_to_temp_v2 = async function ({local_file_cache_path, hash, host_file_temp_path, filename, verify_hash=true}) { + console.log('*** Aether App Native export: open_hash_file_to_temp_v2() ***'); + console.log(`Local File Cache Path: ${local_file_cache_path}; Hash: ${hash}; Local File Temp Path: ${host_file_temp_path}; Filename: ${filename}`); + + // console.log('Process: Check local hash file cache, Download hash file to cache, and Open cached hash file after copying to temp directory'); + + if (fs.existsSync(local_file_cache_path)) { + } else { + // This should not happen. The directory needs to be created. + console.log(`Cache directory for hashed files was not found: ${local_file_cache_path}`); + return false; + } + + let hash_filename = `${hash}.file`; + + let hash_subdirectory = hash_filename.substring(0,2); + let subdirectory_path = path.join(local_file_cache_path, hash_subdirectory); + + if (fs.existsSync(subdirectory_path)) { + } else { + // This should not happen. The subdirectory needs to be created. + console.log(`Hashed file subdirectory not found in cache directory: ${subdirectory_path}`); + return false; + } + + let full_cached_hash_path = path.join(subdirectory_path, hash_filename); + + if (fs.existsSync(full_cached_hash_path)) { + console.log(`Hashed file exists in cache: ${full_cached_hash_path}`); + + if (verify_hash) { + const file_buffer = fs.readFileSync(full_cached_hash_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 { + // This should only happen if the file is actively being downloaded or it is corrupt. + console.log('File hash does not match', file_hash_sha256_check); + return false; + } + } + } else { + console.log(`Hashed file not found in cache: ${full_cached_hash_path}`); + return null; + } + + let local_file_cache_path_w_sub = subdirectory_path; // I need to go back and clean up the variable names related file directory and file paths. + // NOTE: Setting check_hash to false since by default it was checked above. + let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', local_file_cache_path_w_sub, hash, host_file_temp_path, filename, verify_hash=false).then((result) => { + console.log('IPC open hash file to temp finished'); + if (result) { + console.log('Local hash file was opened from temp directory.'); + return result; + } else { + console.log('Local hash file was not opened from the temp directory. Something went wrong.'); + console.log(result); + return false; + } + }) + + // let result = await ipcRenderer.send('open_local_file', local_file_cache_path, hash, host_file_temp_path, filename); + // console.log(result); + + // console.log(open_hash_file_to_temp_result); + // console.log('End: open_hash_file_to_temp()'); + // if (open_hash_file_to_temp_result) { + // console.log('File opened successfully'); + // return true; + // } else { + // console.log('File was not opened successfully'); + // return false; + // } + + // let open_hash_file_to_temp_result = await ipcRenderer.invoke('open_hash_file_to_temp', subdirectory_path, hash, host_file_temp_path, filename).then((result) => { + // console.log('IPC open hash file to temp finished'); + // if (result) { + // console.log('Local hash file was opened from temp directory.'); + // return result; + // } else { + // console.log('Local hash file was not opened from the temp directory. Something went wrong.'); + // console.log(result); + // return false; + // } + // }) + + return open_hash_file_to_temp_result; +} + + + + +// Open local file +// Used by Svelte Event Launcher +// NOTE: Trying to replace this with something directly in the Svelte app part. 2022-10-11 +// Updated 2022-03-10 +exports.open_local_file = async function ({local_file_path, filename}) { + console.log('*** Electron framework export: open_local_file() ***'); + // console.log('Open local file'); + console.log(`Local File Path: ${local_file_path}; Filename: ${filename}`); + + // let full_local_file_path = path.join(local_file_path, filename); + // console.log(full_local_file_path); + + // if (fs.existsSync(full_local_file_path)) { + // console.log(`Local file exists: ${full_local_file_path}`); + // // return true; + // } else { + // return false; + // } + + let open_local_file_result = await ipcRenderer.invoke('open_local_file', local_file_path, filename).then((result) => { + console.log('IPC open local file finished'); + console.log(result); + return true; + }) + + console.log(open_local_file_result); + console.log('End: open_local_file()'); + if (open_local_file_result) { + console.log('File opened successfully'); + return true; + } else { + console.log('File was not opened successfully'); + return false; + } +} + + +// // Check local file cache and download from server if needed. +// // Updated 2022-03-09 +// // exports.check_file_cache = async function ({local_file_cache_path, event_file_id, hash}) { +// exports.check_file_cache = async function ({api_base_url, local_file_cache_path, event_file_id, hash}) { +// 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: ${local_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`); + +// // 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(local_file_cache_path, hash_filename); +// console.log(save_path); + +// if (fs.existsSync(save_path)) { +// console.log('Hashed file cache already exists: '+save_path); +// return true; +// } else { +// console.log('Hashed file not found in local cache. Downloading file: '+save_path); +// let endpoint = `/event/file/${event_file_id}/download`; +// let result = await ipcRenderer.send('download_file', api_base_url, endpoint, save_path); // Must download file using main node.js thread. +// console.log(result); + +// return new Promise((resolve, reject) => { +// ipcRenderer.once('download_file_reply', function(event, response){ +// console.log(response); +// return response; +// }) +// resolve(true); +// }); + +// // await ipcRenderer.once('download_file_reply', function(event, response){ +// // console.log(response); +// // return response; +// // }); + +// // result.then(function (response) { +// // console.log('Downloaded!!!???'); +// // return true; +// // }).catch(function (error) { +// // console.log(error); +// // return false; +// // }); + +// // return result; + +// // console.log(result); +// // if (result) { +// // return true; +// // } else { +// // return false; +// // } +// } +// } + + +// 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({api_base_url, local_file_cache_path, event_file_id, hash}) { + console.log('*** Electron framework: check_file_cache() ***'); + // console.log('Check local file cache and download from server if needed.'); + console.log(`Host File Cache Path: ${local_file_cache_path}; Event File ID: ${event_file_id}; Hash: ${hash}`); + + // 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(local_file_cache_path, hash_filename); + console.log(save_path); + + if (fs.existsSync(save_path)) { + console.log('Hashed file cache already exists: '+save_path); + return true; + } else { + console.log('Hashed file not found in local cache. Downloading file: '+save_path); + let endpoint = `/event/file/${event_file_id}/download`; + let result = await ipcRenderer.send('download_file', api_base_url, endpoint, save_path); // Must download file using main node.js thread. + console.log(result); + + return new Promise((resolve, reject) => { + ipcRenderer.once('download_file_reply', function(event, response){ + console.log(response); + return response; + }) + resolve(true); + }); + + // await ipcRenderer.once('download_file_reply', function(event, response){ + // console.log(response); + // return response; + // }); + + // result.then(function (response) { + // console.log('Downloaded!!!???'); + // return true; + // }).catch(function (error) { + // console.log(error); + // return false; + // }); + + // return result; + + // console.log(result); + // if (result) { + // return true; + // } else { + // return false; + // } + } +} + + +// 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({local_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: ${local_file_cache_path}; Hash: ${hash}; Host File Temp Path: ${host_file_temp_path}; Filename: ${filename}`); + + console.log(local_file_cache_path); + console.log(hash); + console.log(filename); + + let result = await ipcRenderer.send('open_local_file', local_file_cache_path, hash, host_file_temp_path, filename); + console.log(result); + + return true; +} + + +// No longer needed? Not referenced as of 2022-10-11 +exports.check_file_cache_and_open_local_file = async function ({local_file_cache_path, event_file_id, hash, host_file_temp_path, filename}) { + console.log('*** Electron framework: check_file_cache_and_open_local_file() ***'); + 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({local_file_cache_path: local_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({local_file_cache_path: local_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({local_file_cache_path: local_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; + }) +} + + +// Kill processes +// Signals: HUP (hang up), INT (interrupt), QUIT (quit), ABRT (abort), KILL (non-catchable, non-ignoraable kill), ALRMn (alarm clock), TERM (default; software termination signal) +// Updated 2022-05-07 +exports.kill_processes = async function ({process_name = null, process_id = null, signal = null}) { + console.log('*** Electron framework export: kill_processes() ***'); + console.log(process_name); // process_name or grep pattern + + let cmd = ''; + if (os.platform == 'darwin') { + if (signal == 'HUP') { + cmd = `killall -HUP '${process_name}'`; + } else if (signal == 'INT') { + cmd = `killall -INT '${process_name}'`; + } else if (signal == 'QUIT') { + cmd = `killall -QUIT '${process_name}'`; + } else if (signal == 'ABRT') { + cmd = `killall -ABRT '${process_name}'`; + } else if (signal == 'KILL') { + cmd = `killall -KILL '${process_name}'`; + } else if (signal == 'ALRM') { + cmd = `killall -ALRM '${process_name}'`; + } else if (signal == 'TERM') { + cmd = `killall -TERM '${process_name}'`; + } else if (process_id && signal == 'HUP') { + cmd = `killall -HUP ${process_id}`; + } else if (process_id && signal == 'INT') { + cmd = `killall -INT ${process_id}`; + } else if (process_id && signal == 'QUIT') { + cmd = `killall -QUIT ${process_id}`; + } else if (process_id && signal == 'ABRT') { + cmd = `killall -ABRT ${process_id}`; + } else if (process_id && signal == 'KILL') { + cmd = `killall -KILL ${process_id}`; + } else if (process_id && signal == 'ALRM') { + cmd = `killall -ALRM ${process_id}`; + } else if (process_id && signal == 'TERM') { + cmd = `killall -TERM ${process_id}`; + } else { + // cmd = `osascript -e 'quit app "${process_name}" saving no'`; + cmd = `osascript -e 'quit application "${process_name}" saving no'`; + } + + } else { + cmd = `pkill ${process_name}`; + } + + child_process.exec(cmd, (err, stdout, stdin) => { + // if (err) throw err; + if (err) console.log(err); + console.log(stdout); + }); + console.log(`Killed processes matching ${process_name}`); + + if (os.platform == 'darwin') { + if (process_name == 'Parallels:Acrobat Reader') { + // Regular expression: (Parallels).*(Acrobat Reader) + // This will find any process with Parallels and Acrobat Reader in the name + cmd = `pkill -i -f '(Parallels).*(Acrobat Reader)'`; + + child_process.exec(cmd, (err, stdout, stdin) => { + if (err) throw err; + console.log(stdout); + }); + console.log('Killed Parallels Acrobat Reader process'); + } + + if (process_name == 'Parallels:PowerPoint') { + // Regular expression: (Parallels).*(PowerPoint) + // This will find any process with Parallels and PowerPoint in the name + cmd = `pkill -i -f '(Parallels).*(PowerPoint)'`; + + child_process.exec(cmd, (err, stdout, stdin) => { + if (err) throw err; + console.log(stdout); + }); + console.log('Killed Parallels PowerPoint process'); + } + } + + // let signal = 'SIGTERM'; // 'SIGTERM', 'SIGINT', 'SIGHUP' + // process.kill(pid, signal); + // process.kill(pid, 0); // Special case test if process exists + + return true; +} + + +// Run raw osascript +// Updated 2022-05-07 +exports.run_osascript = async function ({cmd=null, interactive=false, language=null, flags='h', program_file=null}) { + console.log('*** Electron framework export: run_osascript() ***'); + console.log(cmd); + + if (os.platform == 'darwin') { + } else { + console.log('Not available for this platform. macOS (darwin) only.'); + return false; + } + + let osascript_str = ''; + + if (Array.isArray(cmd)) { + console.log('List of cmd strings'); + let cmds_str = ''; + for (let i = 0; i < cmd.length; i++) { + cmds_str += `-e '${cmd[i]}'`; + } + osascript_str = `osascript ${cmds_str}` + + } else if (typeof cmd === 'string') { + console.log('Single cmd string'); + osascript_str = `osascript -e '${cmd}'`; + } else { + return false; + } + + if (language) { + console.log(`Language: ${language}`); + osascript_str = `${osascript_str} -l ${language}`; + } + + if (flags) { + console.log(`Flags: ${flags}`); + osascript_str = `${osascript_str} -s ${flags}`; + } + + console.log(`OSA Script String: ${osascript_str}`); + child_process.exec(osascript_str, (err, stdout, stdin) => { + if (err) throw err; + console.log(stdout); + console.log(stdin); + }); + + console.log('Finished'); + return true; +} + + +// Run raw command +// Updated 2022-05-07 +exports.run_cmd = async function ({cmd=null, return_stdout=null, return_stdin=null, sync=null}) { + console.log('*** Electron framework export: run_cmd() ***'); + + console.log(`Command String: ${cmd}`); + + let result; + + if (!sync) { + result = child_process.exec(cmd, (err, stdout, stdin) => { + // if (err) throw err; + if (err) { + console.log('Error:', err); + return false; + }; + + console.log('stdout:', stdout); + // console.log('stdin:', stdin); + + if (return_stdout) { + console.log('Finished and returning stdout'); + return stdout; + } else { + console.log('Finished and returning true'); + return true; + } + }); + } else { + result = child_process.execSync(cmd, (err, stdout, stdin) => { + // if (err) throw err; + if (err) { + console.log('Error:', err); + return false; + }; + + console.log('stdout:', stdout); + // console.log('stdin:', stdin); + + if (return_stdout) { + console.log('Finished and returning stdout'); + return stdout; + } else { + console.log('Finished and returning true'); + return true; + } + }); + } + + console.log('Result:', result); + return result; +} + + +// Run raw command sync +// Updated 2022-05-07 +exports.run_cmd_sync = function ({cmd=null, return_stdout=null, return_stdin=null}) { + console.log('*** Electron framework export: run_cmd() ***'); + + console.log(`Command String: ${cmd}`); + + let stdout; + + try { + stdout = child_process.execSync(cmd, {encoding: 'utf8'}); + console.log('Std Out:', stdout); + } catch (err) { + console.error('Error:', err); + return false; + } + + if (return_stdout) { + console.log('Finished and returning stdout'); + return stdout; + } else { + console.log('Finished and returning true'); + return true; + } + + // let result; + // let stdout; + // let stderr; + // try { + // let { stdout, stderr } = child_process.execSync(cmd, (err, stdout, stdin) => { + // // if (err) throw err; + // if (err) { + // console.log('Error:', err); + // return false; + // }; + + // console.log('stdout:', stdout); + // // console.log('stdin:', stdin); + + // if (return_stdout) { + // console.log('Finished and returning stdout'); + // return stdout; + // } else { + // console.log('Finished and returning true'); + // return true; + // } + // }); + // } catch (err) { + // console.error(err); + // return false; + // } + + // console.log('Result:', result); + // return result; +} + + +// Run raw command +// Updated 2022-05-25 +exports.get_device_info = async function () { + console.log('*** Electron framework export: get_device_info() ***'); + + // https://nodejs.org/api/os.html + + let data = {}; + data['arch'] = os.arch(); + data['hostname'] = os.hostname(); + data['cpus'] = os.cpus(); + data['freemem'] = os.freemem(); + data['totalmem'] = os.totalmem(); + data['loadavg'] = os.loadavg(); + data['networkInterfaces'] = os.networkInterfaces(); + data['platform'] = os.platform(); + data['release'] = os.release(); + data['uptime'] = os.uptime(); + data['version'] = os.version(); + + console.log(data); + return data; +} + + + +// For loading JS file +function loadJS(){ + + // Gives -1 when the given input is not in the string + // i.e this file has not been added + + if(filesAdded.indexOf('script.js') !== -1) + return + + // Head tag + var head = document.getElementsByTagName('head')[0] + + // Creating script element + var script = document.createElement('script') + script.src = 'script.js' + script.type = 'text/javascript' + + // Adding script element + head.append(script) + + // Adding the name of the file to keep record + filesAdded += ' script.js' +} + +// To load CSS file +function loadCSS() { + + if(filesAdded.indexOf('styles.css') !== -1) + return + + var head = document.getElementsByTagName('head')[0] + + // Creating link element + var style = document.createElement('link') + style.href = 'styles.css' + style.type = 'text/css' + style.rel = 'stylesheet' + head.append(style); + + // Adding the name of the file to keep record + filesAdded += ' styles.css' +} + + + +exports.check_and_get_updated_native_app_config = function ({file_path='device_configs/ae_native_app_config.default.json', overwrite=false}) { + console.log('*** Aether App Native export: check_and_get_updated_native_app_config() ***'); + + if (os.platform == 'darwin') { + let default_osit_sync_app_config_path = path.join(default_osit_sync_app_root_path, file_path); + + let default_app_config_path = path.join(app_root_path, 'ae_native_app_config.default.json'); + console.log('macOS app root and config directory: '+default_app_config_path); + + fs.copyFileSync(default_osit_sync_app_config_path, default_app_config_path); + + if (overwrite) { + console.log('Overwriting the current active config file...'); + let active_app_config_path = path.join(app_root_path, 'ae_native_app_config.json'); + console.log('macOS app root and config directory: '+active_app_config_path); + + fs.copyFileSync(default_osit_sync_app_config_path, active_app_config_path); + } + } else if (os.platform == 'linux') { + app_root_path = path.join(home_directory, '.config/OSIT'); + console.log('Linux config directory: '+app_root_path); + return false; + } else { + console.log('Unknown OS... Is this Windows?'); + return false; + } + + return true; +} diff --git a/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/electron.js b/src/lib/electron_relay.js similarity index 100% rename from src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/electron.js rename to src/lib/electron_relay.js diff --git a/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/+page@(launcher).svelte b/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/+page@(launcher).svelte index 911db886..c4f1bea9 100644 --- a/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/+page@(launcher).svelte +++ b/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/+page@(launcher).svelte @@ -21,10 +21,12 @@ import { events_loc, events_sess, events_slct, events_trigger, events_trig } fro import { events_func } from '$lib/ae_events_functions'; -import { kill_processes, open_local_file_v2, run_cmd, run_cmd_sync, run_osascript, get_device_info } from './electron.js'; +import { kill_processes, open_local_file_v2, run_cmd, run_cmd_sync, run_osascript, get_device_info } from '$lib/electron_relay.js'; import Event_launcher_menu from './launcher_menu.svelte'; import Event_launcher_session_view from './launcher_session_view.svelte'; +// import native_app from '$lib/electron_native.js'; + // Exports @@ -247,8 +249,92 @@ if (browser) { onMount(() => { console.log('Pres Mgmt Launcher [slug]: +page@(launcher).svelte'); + + // if ($ae_event_launcher.app_mode == 'native') { + // handle_get_device_info($slct.event_device_id); + // } + + // const native_app = require('$lib./script'); + + // let native_app_config = native_app.load_init_config(); + // console.log('Native App Initial Config:', native_app_config); }); + + + +async function handle_get_device_info(event_device_id) { + console.log('*** handle_get_device_info() ***'); + // console.log(event_device_id); + + // console.log('GET DEVICE INFO!!! GET DEVICE INFO!!! GET DEVICE INFO!!!'); + + let get_device_info_promise = get_device_info({event_device_id: event_device_id}); + get_device_info_promise.then(function (result) { + // console.log('GOT DEVICE INFO!!! GOT DEVICE INFO!!! GOT DEVICE INFO!!!'); + console.log(get_device_info_promise); + let event_device_data = {}; + event_device_data['other_json'] = JSON.stringify(result); + + event_device_data['info_hostname'] = result.hostname; + + // console.log(event_device_data['other_json']); + + // console.log(result.networkInterfaces); + + let ni_data_li = []; + let ip_address_li = []; + + for (let network_interface in result.networkInterfaces) { + // console.log(network_interface); + let ni_data = {}; + + if (network_interface.startsWith('en')) { + console.log('Wired or wireless network interface found'); + + ni_data['name'] = network_interface; + + let ni_details = result.networkInterfaces[network_interface]; + // console.log(ni_details); + + for (let i=0; i < ni_details.length; i++) { + ni_data['address'] = ni_details[i].address; + ni_data['mac'] = ni_details[i].mac; + + ip_address_li.push(ni_details[i].address); + } + + ni_data_li.push(ni_data); + } + } + + event_device_data['meta_json'] = JSON.stringify(ni_data_li); + console.log(event_device_data['meta_json']); + + event_device_data['info_ip_list'] = ip_address_li.join('; '); + console.log(event_device_data['info_ip_list']); + + event_device_obj_up_promise = update_event_device_obj({event_device_id: event_device_id, data: event_device_data, log_lvl: 0}) + .then(function (up_event_device_result) { + // console.log('UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!!'); + if (up_event_device_result) { + console.log(up_event_device_result); + } + }) + .catch(function (error) { + console.log('No results returned or failed.'); + console.log(error); + }); + + }); + + return get_device_info_promise; +} + + + + + diff --git a/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/menu_session_list.svelte b/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/menu_session_list.svelte index d48c2643..07689280 100644 --- a/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/menu_session_list.svelte +++ b/src/routes/events_pres_mgmt/(launcher)/launcher/[slug]/menu_session_list.svelte @@ -66,7 +66,7 @@ let hover_timer: any = null; $events_slct.event_session_id = event_session_obj?.id; $events_slct.event_session_obj = event_session_obj; - ae_promises.slct_event_session_id = ({ + ae_promises.slct_event_session_id = events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: event_session_obj?.id, inc_file_li: true, @@ -96,7 +96,7 @@ let hover_timer: any = null; $events_slct.event_session_id = event_session_obj?.id; $events_slct.event_session_obj = event_session_obj; - ae_promises.slct_event_session_id = ({ + ae_promises.slct_event_session_id = events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: event_session_obj?.id, inc_file_li: true, diff --git a/src/routes/events_pres_mgmt/event/[slug]/+page.svelte b/src/routes/events_pres_mgmt/event/[slug]/+page.svelte index 82f68dd5..23dd7731 100644 --- a/src/routes/events_pres_mgmt/event/[slug]/+page.svelte +++ b/src/routes/events_pres_mgmt/event/[slug]/+page.svelte @@ -194,9 +194,7 @@ function process_search_string(search_str: string) { // params['qry__limit'] = 35; // } - search__event_session({ - api_cfg: $ae_api, - event_id: $events_slct.event_id, + handle_search__event_session({ ft_search_str: ft_search_str_new, lk_search_str: lk_search_str_new, // fulltext_search_qry_str: ft_search_str_new, @@ -213,8 +211,6 @@ function process_search_string(search_str: string) { async function handle_search__event_session( { - api_cfg, - event_id, ft_search_str = '', lk_search_str = '', search_delay = 0, @@ -227,8 +223,6 @@ async function handle_search__event_session( try_cache=false, log_lvl=1, }: { - api_cfg: any, - event_id: string, ft_search_str?: string, lk_search_str?: string, search_delay?: number, // In milliseconds @@ -269,7 +263,7 @@ async function handle_search__event_session( $events_sess.pres_mgmt.status_qry__search = 'loading'; - search_submit_results = events_func.handle_search__event_session({ + search_submit_results = events_func.search__event_session({ api_cfg: $ae_api, event_id: $events_slct.event_id, // type_code: type_code, diff --git a/src/routes/events_pres_mgmt/event/[slug]/reports/+page.svelte b/src/routes/events_pres_mgmt/event/[slug]/reports/+page.svelte index 821f96af..96a87e6c 100644 --- a/src/routes/events_pres_mgmt/event/[slug]/reports/+page.svelte +++ b/src/routes/events_pres_mgmt/event/[slug]/reports/+page.svelte @@ -228,14 +228,16 @@ async function handle_qry__event_session( enabled = $events_loc.pres_mgmt.qry_enabled ?? 'enabled', hidden = $events_loc.pres_mgmt.qry_hidden ?? 'not_hidden', limit = $events_loc.pres_mgmt.qry_limit__session ?? 150, - log_lvl=2, + log_lvl=0, }: { qry_files?: boolean, // file_count?: boolean, // file_count_all?: null|number, // ft_search_str?: string, // lk_search_str?: string, - params?: key_val, + enabled?: string, + hidden?: string, + limit?: number, log_lvl?: number, } ) { @@ -263,12 +265,14 @@ async function handle_qry__event_session( $events_sess.pres_mgmt.status_qry__search = 'processing'; $events_sess.pres_mgmt.status_rpt[$events_sess.pres_mgmt.show_report] = 'processing'; $events_slct.event_session_obj_li = search_results; - console.log(search_results); + if (log_lvl) { + console.log(`Search results:`, search_results); + } // $events_sess.pres_mgmt.status_qry__search = 'done'; }) .finally(() => { if (log_lvl) { - console.log('TEST SEARCH - Search done. Pulling out the event_session_id_randoms.'); + console.log('API query done. Pulling out the event_session_id_random values.'); } // console.log(`TEST search: ${$lq_kv__event_session_obj_li}`); diff --git a/src/routes/events_pres_mgmt/session_page_menu.svelte b/src/routes/events_pres_mgmt/session_page_menu.svelte index e8e38f08..aa8f5cd1 100644 --- a/src/routes/events_pres_mgmt/session_page_menu.svelte +++ b/src/routes/events_pres_mgmt/session_page_menu.svelte @@ -150,7 +150,7 @@ let ae_triggers: key_val = {}; on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: log_lvl @@ -204,7 +204,7 @@ let ae_triggers: key_val = {}; on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: log_lvl @@ -257,7 +257,7 @@ let ae_triggers: key_val = {}; on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: log_lvl diff --git a/src/routes/events_pres_mgmt/session_view.svelte b/src/routes/events_pres_mgmt/session_view.svelte index 9122335c..03d0e88d 100644 --- a/src/routes/events_pres_mgmt/session_view.svelte +++ b/src/routes/events_pres_mgmt/session_view.svelte @@ -148,7 +148,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: 1 @@ -177,7 +177,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: 1 @@ -213,7 +213,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random }); @@ -241,7 +241,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random }); @@ -287,7 +287,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: 1 @@ -421,7 +421,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: 1 @@ -625,7 +625,7 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - ({ + events_func.load_ae_obj_id__event_session({ api_cfg: $ae_api, event_session_id: $lq__event_session_obj?.event_session_id_random, log_lvl: 1 @@ -875,7 +875,9 @@ $: if ($lq__event_session_obj) { on:ae_crud_updated={e => { console.log(`ae_crud_updated:`, e.detail); - events_func.load_ae_obj_id__event_presentation({api_cfg: $ae_api, event_presentation_id: event_presentation_obj.event_presentation_id_random, log_lvl: 1}) + events_func.load_ae_obj_id__event_presentation({ + api_cfg: $ae_api, event_presentation_id: event_presentation_obj.event_presentation_id_random, log_lvl: 1 + }) .then(function (load_results) { }) .then(function (load_results) {