refactor: improve type safety, Svelte 5 reactivity, and API resilience
This commit is contained in:
@@ -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('');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user