diff --git a/src/lib/ae_api/api_get__crud_obj_id.ts b/src/lib/ae_api/api_get__crud_obj_id.ts index 2a67b3c9..9bf83aa0 100644 --- a/src/lib/ae_api/api_get__crud_obj_id.ts +++ b/src/lib/ae_api/api_get__crud_obj_id.ts @@ -5,24 +5,24 @@ import { get_object } from './api_get_object'; export async function get_ae_obj_id_crud( { api_cfg, - no_account_id=false, + 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={}, + 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 + // jwt = null, + headers = {}, + params = {}, + timeout = 25000, + return_meta = false, + log_lvl = 0 }: { api_cfg: any, no_account_id?: boolean, diff --git a/src/lib/ae_api/api_patch_object.ts b/src/lib/ae_api/api_patch_object.ts index 3dda4c36..62efa0bb 100644 --- a/src/lib/ae_api/api_patch_object.ts +++ b/src/lib/ae_api/api_patch_object.ts @@ -18,7 +18,7 @@ export let patch_object = async function patch_object( log_lvl?: number } ) { -console.log('*** patch_object() ***'); +console.log('*** patch_object() XXXX ***'); if (log_lvl) { // console.log(api_cfg); diff --git a/src/lib/ae_core_functions.ts b/src/lib/ae_core_functions.ts index cc825b04..d6aff02f 100644 --- a/src/lib/ae_core_functions.ts +++ b/src/lib/ae_core_functions.ts @@ -19,15 +19,15 @@ import { let ae_promises: key_val = {}; // Promise; -// Updated 2024-08-12 +// Updated 2024-10-02 async function check_hosted_file_obj_w_hash( { api_cfg, hosted_file_hash, - check_for_local=true, - params={}, - return_meta=false, - log_lvl=0 + check_for_local = true, // Forces a check on the host server for the file. + params = {}, + return_meta = false, + log_lvl = 0 } : { api_cfg: any, hosted_file_hash: string, diff --git a/src/lib/ae_utils/ae_utils.ts b/src/lib/ae_utils/ae_utils.ts index f0ed9011..f711c6eb 100644 --- a/src/lib/ae_utils/ae_utils.ts +++ b/src/lib/ae_utils/ae_utils.ts @@ -1,7 +1,8 @@ // Import external files first. Eventually this will be broken up in to smaller files. import { process_permission_checks } from './ae_utils__perm_checks'; import { iso_datetime_formatter } from './ae_utils__datetime_format'; - +import { is_datetime_recent } from './ae_utils__is_datetime_recent'; +import { format_bytes, guess_file_name, guess_file_extension, get_file_hash } from './ae_utils__files'; type key_str = { [key: string]: string; @@ -11,21 +12,6 @@ type key_val = { [key: string]: any; }; -function format_bytes( - bytes: number, - decimals: number = 2 - ) { - if (bytes === 0) return '0 Bytes'; - - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - - const i = Math.floor(Math.log(bytes) / Math.log(k)); - - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; -} - /* This utility function will add commas to a number. */ function number_w_commas(x) { @@ -33,63 +19,6 @@ function number_w_commas(x) { } -// Updated 2024-08-12 -function guess_file_name(filename_string: string) { - // console.log('*** guess_file_name() ***'); - if (!filename_string) { - return ''; - } - - if (filename_string.includes('.')) { - let file_name = filename_string.substring(0, filename_string.lastIndexOf('.')); - // console.log(file_name); - return file_name; - } else { - return filename_string; - } -} - - -// Updated 2024-08-12 -function guess_file_extension(filename_string: string) { - // console.log('*** guess_file_extension() ***'); - if (!filename_string) { - return ''; - } - - if (!filename_string.includes('.')) { - return ''; - } - - let file_extension = filename_string.substring(filename_string.lastIndexOf('.') + 1, filename_string.length) || filename_string; - // console.log(file_extension); - return file_extension; -} - - -// Updated 2024-08-12 -async function get_file_hash(file) { - return new Promise((resolve, reject) => { - let file_reader = new FileReader(); - - file_reader.onload = async function() { - if (file_reader.result.byteLength !== file.size) { - console.log('File was not read completely'); - reject("Error reading the file"); - } - - const hash_buffer = await crypto.subtle.digest('SHA-256', file_reader.result); - const hash_array = Array.from(new Uint8Array(hash_buffer)); - const hash_hex = hash_array.map(b => b.toString(16).padStart(2, '0')).join(''); - - resolve(hash_hex); - }; - - file_reader.readAsArrayBuffer(file); - }); -} - - /* This utility function looks for any form data with the prefixed name passed and returns a new object. * This function is used heavily! Be very careful making changes!!! * If rm_empty_id then it will remove/ignore fields matching. This helps with the API and new records/objects @@ -777,8 +706,8 @@ function return_obj_type_path({obj_type=null, obj_type_prop_name=null}) { } - export let ae_util = { + is_datetime_recent: is_datetime_recent, process_permission_checks: process_permission_checks, iso_datetime_formatter: iso_datetime_formatter, format_bytes: format_bytes, diff --git a/src/lib/ae_utils/ae_utils__files.ts b/src/lib/ae_utils/ae_utils__files.ts new file mode 100644 index 00000000..71551fea --- /dev/null +++ b/src/lib/ae_utils/ae_utils__files.ts @@ -0,0 +1,72 @@ +// These are all file related functions. + +export let format_bytes = function format_bytes( + bytes: number, + decimals: number = 2 + ) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} + +// Updated 2024-08-12 +export let guess_file_name = function guess_file_name(filename_string: string) { + // console.log('*** guess_file_name() ***'); + if (!filename_string) { + return ''; + } + + if (filename_string.includes('.')) { + let file_name = filename_string.substring(0, filename_string.lastIndexOf('.')); + // console.log(file_name); + return file_name; + } else { + return filename_string; + } +} + + +// Updated 2024-08-12 +export let guess_file_extension = function guess_file_extension(filename_string: string) { + // console.log('*** guess_file_extension() ***'); + if (!filename_string) { + return ''; + } + + if (!filename_string.includes('.')) { + return ''; + } + + let file_extension = filename_string.substring(filename_string.lastIndexOf('.') + 1, filename_string.length) || filename_string; + // console.log(file_extension); + return file_extension; +} + + +// Updated 2024-08-12 +export let get_file_hash = async function get_file_hash(file) { + return new Promise((resolve, reject) => { + let file_reader = new FileReader(); + + file_reader.onload = async function() { + if (file_reader.result.byteLength !== file.size) { + console.log('File was not read completely'); + reject("Error reading the file"); + } + + const hash_buffer = await crypto.subtle.digest('SHA-256', file_reader.result); + const hash_array = Array.from(new Uint8Array(hash_buffer)); + const hash_hex = hash_array.map(b => b.toString(16).padStart(2, '0')).join(''); + + resolve(hash_hex); + }; + + file_reader.readAsArrayBuffer(file); + }); +} diff --git a/src/lib/ae_utils/ae_utils__is_datetime_recent.ts b/src/lib/ae_utils/ae_utils__is_datetime_recent.ts new file mode 100644 index 00000000..aa3cd3be --- /dev/null +++ b/src/lib/ae_utils/ae_utils__is_datetime_recent.ts @@ -0,0 +1,23 @@ +// Function to check if the file (or anything) timestamp was created within the last X minutes +export let is_datetime_recent = function is_datetime_recent( + { + datetime, + minutes + }: { + datetime: string, + minutes: number + }) { + console.log(`*** is_datetime_recent() *** datetime=${datetime} minutes=${minutes}`); + + let now: any = new Date(); + let then: any = new Date(datetime); + + let diff = now - then; + let diff_minutes = Math.floor(diff / 60000); + + if (diff_minutes < minutes) { + return true; + } else { + return false; + } +} \ No newline at end of file diff --git a/src/lib/api.ts b/src/lib/api.ts index 5c0d3714..6766b46c 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -342,9 +342,10 @@ export let update_ae_obj_id_crud = async function update_ae_obj_id_crud( if (!data) { data = {}; - data['super_key'] = key; - data['jwt'] = jwt; } + data['super_key'] = key; + data['jwt'] = jwt; + // NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST // This obj_v_name is the view name to use when returning data. Do not prefix it with v_. This is checked and done automatically by the API. diff --git a/src/lib/element_manage_event_file_li.svelte b/src/lib/element_manage_event_file_li.svelte index 97739763..bcb466e5 100644 --- a/src/lib/element_manage_event_file_li.svelte +++ b/src/lib/element_manage_event_file_li.svelte @@ -316,7 +316,7 @@ onMount(() => { api_cfg: $ae_api, event_file_id: event_file_obj.event_file_id_random, data_kv: event_file_data, - log_lvl: 3 + log_lvl: 0 }) .then (function (update_results) { console.log(`Update results:`, update_results); @@ -517,7 +517,12 @@ onMount(() => { - + + {#if display_mode == 'default'} diff --git a/src/routes/events_pres_mgmt/ae_comp__event_file_obj_tbl.svelte b/src/routes/events_pres_mgmt/ae_comp__event_file_obj_tbl.svelte index 870ae977..19bc6793 100644 --- a/src/routes/events_pres_mgmt/ae_comp__event_file_obj_tbl.svelte +++ b/src/routes/events_pres_mgmt/ae_comp__event_file_obj_tbl.svelte @@ -176,7 +176,10 @@ let lq_kv__event_file_obj_li = liveQuery( {ae_util.format_bytes(event_file_obj?.file_size)} - +
{ae_util.iso_datetime_formatter(event_file_obj?.created_on, 'dddd')} @@ -185,7 +188,11 @@ let lq_kv__event_file_obj_li = liveQuery( {ae_util.iso_datetime_formatter(event_file_obj?.created_on, 'date_long_month_day')}
- + {ae_util.iso_datetime_formatter(event_file_obj?.created_on, 'time_12_short')}