API bug fixes. Clean up. New util functions. Highlight times for newer files.

This commit is contained in:
Scott Idem
2024-10-02 17:32:36 -04:00
parent 1004104de0
commit 89dd410aeb
9 changed files with 138 additions and 101 deletions

View File

@@ -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,

View File

@@ -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);

View File

@@ -19,15 +19,15 @@ import {
let ae_promises: key_val = {}; // Promise<any>;
// 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,

View File

@@ -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,

View File

@@ -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);
});
}

View File

@@ -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;
}
}

View File

@@ -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.

View File

@@ -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(() => {
</span>
</span>
<span>
<!-- If this file was uploaded (created) within the last 15 minutes we want to highlight it in blue. -->
<span
class:bg-yellow-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 30})}
class:bg-green-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 240})}
class:bg-blue-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 2880})}
>
{#if display_mode == 'default'}
<!-- <span class="fas fa-cloud-upload-alt mx-1"></span> -->
<!-- Uploaded: -->

View File

@@ -176,7 +176,10 @@ let lq_kv__event_file_obj_li = liveQuery(
</button>
</td>
<td class="px-4 py-2">{ae_util.format_bytes(event_file_obj?.file_size)}</td>
<td class="px-4 py-2">
<td
class="px-4 py-2"
>
<div>
<span>
{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')}
</span>
</div>
<span>
<span
class:bg-yellow-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 30})}
class:bg-green-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 240})}
class:bg-blue-200={ae_util.is_datetime_recent({datetime: event_file_obj?.created_on, minutes: 2880})}
>
{ae_util.iso_datetime_formatter(event_file_obj?.created_on, 'time_12_short')}
</span>
</td>