refactor: improve type safety, Svelte 5 reactivity, and API resilience
This commit is contained in:
@@ -33,7 +33,8 @@ export type key_val = {
|
||||
};
|
||||
|
||||
/* This utility function will add commas to a number. */
|
||||
function number_w_commas(x) {
|
||||
function number_w_commas(x: number | string) {
|
||||
if (!x) return '0';
|
||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
}
|
||||
|
||||
@@ -86,6 +87,14 @@ function create_a_element({
|
||||
extension = null,
|
||||
text = 'Download',
|
||||
class_li = 'text-blue-500'
|
||||
}: {
|
||||
account_id: string;
|
||||
base_url: string;
|
||||
hosted_file_id: string;
|
||||
filename?: string | null;
|
||||
extension?: string | null;
|
||||
text?: string;
|
||||
class_li?: string;
|
||||
}) {
|
||||
return `<a href="${base_url}/hosted_file/${hosted_file_id}/download?x_no_account_id_token=${account_id}&filename=${filename}" class="${class_li}">${text}</a>`;
|
||||
}
|
||||
@@ -99,6 +108,15 @@ function create_img_element({
|
||||
class_li = 'max-w-64',
|
||||
style = '',
|
||||
inc_link = false
|
||||
}: {
|
||||
account_id: string;
|
||||
base_url: string;
|
||||
hosted_file_id: string;
|
||||
filename?: string | null;
|
||||
extension?: string | null;
|
||||
class_li?: string;
|
||||
style?: string;
|
||||
inc_link?: boolean;
|
||||
}) {
|
||||
let img_html = '';
|
||||
if (filename) {
|
||||
@@ -129,6 +147,14 @@ function create_video_element({
|
||||
extension = null,
|
||||
class_li = 'max-w-64',
|
||||
inc_link = false
|
||||
}: {
|
||||
account_id: string;
|
||||
base_url: string;
|
||||
hosted_file_id: string;
|
||||
filename?: string | null;
|
||||
extension?: string | null;
|
||||
class_li?: string;
|
||||
inc_link?: boolean;
|
||||
}) {
|
||||
let video_html = '';
|
||||
if (filename) {
|
||||
|
||||
@@ -96,7 +96,8 @@ export const split_iv_and_base64 = function split_iv_and_base64(combined: string
|
||||
}
|
||||
const [iv_hex, encrypted_base64_string] = combined.split(':');
|
||||
const base64 = encrypted_base64_string;
|
||||
const iv = new Uint8Array(iv_hex.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
||||
const match_result = iv_hex.match(/.{1,2}/g);
|
||||
const iv = new Uint8Array((match_result || []).map((byte) => parseInt(byte, 16)));
|
||||
if (log_lvl) {
|
||||
console.log(`IV: ${iv}; Encrypted:`, base64);
|
||||
}
|
||||
|
||||
@@ -64,17 +64,25 @@ export const guess_file_extension = function guess_file_extension(filename_strin
|
||||
};
|
||||
|
||||
// Updated 2024-08-12
|
||||
export const get_file_hash = async function get_file_hash(file) {
|
||||
export const get_file_hash = async function get_file_hash(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file_reader = new FileReader();
|
||||
|
||||
file_reader.onload = async function () {
|
||||
if (file_reader.result.byteLength !== file.size) {
|
||||
console.log('File was not read completely');
|
||||
const result = file_reader.result;
|
||||
if (!result || typeof result === 'string') {
|
||||
console.log('File was not read completely or is in wrong format');
|
||||
reject('Error reading the file');
|
||||
return;
|
||||
}
|
||||
|
||||
const hash_buffer = await crypto.subtle.digest('SHA-256', file_reader.result);
|
||||
if (result.byteLength !== file.size) {
|
||||
console.log('File was not read completely');
|
||||
reject('Error reading the file');
|
||||
return;
|
||||
}
|
||||
|
||||
const hash_buffer = await crypto.subtle.digest('SHA-256', result);
|
||||
const hash_array = Array.from(new Uint8Array(hash_buffer));
|
||||
const hash_hex = hash_array.map((b) => b.toString(16).padStart(2, '0')).join('');
|
||||
|
||||
|
||||
@@ -1,32 +1,13 @@
|
||||
export function return_obj_type_path({ obj_type = null, obj_type_prop_name = null }) {
|
||||
export function return_obj_type_path({
|
||||
obj_type = null,
|
||||
obj_type_prop_name = null
|
||||
}: {
|
||||
obj_type?: string | null,
|
||||
obj_type_prop_name?: string | null
|
||||
}) {
|
||||
console.log('*** return_obj_type_path() ***');
|
||||
|
||||
let obj_type_path = null;
|
||||
|
||||
const known_obj_type_li = [
|
||||
'account',
|
||||
'address',
|
||||
'archive',
|
||||
'archive_content',
|
||||
'contact',
|
||||
'event_badge',
|
||||
'event_exhibit',
|
||||
'event_file',
|
||||
'event_location',
|
||||
'event_person',
|
||||
'event_presentation',
|
||||
'event_presenter',
|
||||
'event_registration',
|
||||
'event_session',
|
||||
'event',
|
||||
'hosted_file',
|
||||
'order_line',
|
||||
'order',
|
||||
'person',
|
||||
'post',
|
||||
'post_comment',
|
||||
'user'
|
||||
];
|
||||
let obj_type_path: string | null = null;
|
||||
|
||||
const known_obj_type_li_dict = [
|
||||
{ name: 'account', display: 'Account', path: 'account' },
|
||||
@@ -59,7 +40,7 @@ export function return_obj_type_path({ obj_type = null, obj_type_prop_name = nul
|
||||
{ name: 'user', display: 'User', path: 'user' }
|
||||
];
|
||||
|
||||
if (obj_type) {
|
||||
if (obj_type && obj_type_prop_name) {
|
||||
// Need to loop through known for safety?
|
||||
obj_type_path = obj_type_prop_name.replaceAll('_', '/');
|
||||
} else if (obj_type_prop_name) {
|
||||
|
||||
Reference in New Issue
Block a user