Warning, /firebird/firebird-ng/src/app/utils/data-fetching.utils.ts is written in an unsupported language. File is not indexed.
0001
0002 import JSZip from 'jszip';
0003 import * as events from "node:events";
0004
0005 /**
0006 * Read a zip file and return its contents as an object.
0007 * @param file The file or array buffer to be read.
0008 * @returns Map with file paths in zip as keys and the files'
0009 * string contents as values.
0010 */
0011 export async function readZipFile(file: File | ArrayBuffer) {
0012 const archive = new JSZip();
0013 const filesWithData = new Map<string, string>();
0014
0015 await archive.loadAsync(file);
0016 for (const filePath in archive.files) {
0017 const fileData = await archive.file(filePath)?.async('string');
0018 if(fileData) {
0019 filesWithData.set(filePath, fileData);
0020 }
0021 }
0022
0023 return filesWithData;
0024 }
0025
0026 export async function fetchTextFile(fileURL: string): Promise<string> {
0027 // Load file here!
0028 try{
0029 const loadingTimeMessage = `${fetchTextFile.name}: fetching ${fileURL}`;
0030 console.time(loadingTimeMessage);
0031 const response = await fetch(fileURL);
0032 if (!response.ok) {
0033 throw new Error(`HTTP error! Status: ${response.status}`);
0034 }
0035 const fileText = await response.text();
0036 console.timeEnd(loadingTimeMessage);
0037 return fileText;
0038 }
0039 catch (error) {
0040 console.error(`Error fetching ${fileURL}: ${error}`);
0041 throw error;
0042 }
0043 }
0044
0045
0046 export async function fetchBinaryFile(fileURL: string): Promise<ArrayBuffer> {
0047 // Load file here!
0048 try{
0049 const loadingTimeMessage = `${fetchBinaryFile.name}: fetching ${fileURL}`;
0050 console.time(loadingTimeMessage);
0051 const fileBuffer = await (await fetch(fileURL)).arrayBuffer();
0052 console.timeEnd(loadingTimeMessage);
0053 return fileBuffer;
0054 }
0055 catch (error) {
0056 console.error(`Error fetching ${fileURL}: ${error}`);
0057 throw error;
0058 }
0059 }
0060
0061
0062 /**
0063 * Handle zip containing event data files.
0064 * @param fileURL URL to the zip file.
0065 * @returns An empty promise. ;(
0066 */
0067 export async function loadZipFileEvents(fileURL: string) {
0068
0069 const fileBuffer = await fetchBinaryFile(fileURL);
0070
0071 let filesWithData: Map<string, string>;
0072 // Using a try catch block to catch any errors in Promises
0073 try {
0074 console.time('loadZipFileEvents: reading zip contents');
0075 filesWithData = await readZipFile(fileBuffer);
0076 console.timeEnd('loadZipFileEvents: reading zip contents');
0077 } catch (error) {
0078 console.error('Error while reading zip', fileURL, fileBuffer, error);
0079 throw error;
0080 }
0081
0082 const allEventsObject = {};
0083 // JSON event data
0084 for(let [fileName, fileData] of filesWithData) {
0085 if(!fileName.endsWith('.json')) continue; // We need only JSon!
0086
0087 const parsingProfileMessage = `${loadZipFileEvents.name}: parsing JSON from '${fileName}'`
0088 console.time(parsingProfileMessage);
0089 console.profile(parsingProfileMessage);
0090 Object.assign(allEventsObject, JSON.parse(fileData));
0091 console.timeEnd(parsingProfileMessage);
0092 console.profileEnd(parsingProfileMessage);
0093 }
0094
0095 return allEventsObject;
0096 }
0097
0098
0099
0100 /**
0101 * Handle zip containing event data files.
0102 * @param fileURL URL to the zip file.
0103 * @returns An empty promise. ;(
0104 */
0105 export async function loadJSONFileEvents(fileURL: string) {
0106
0107 const fileText = await fetchTextFile(fileURL);
0108
0109 const allEventsObject = {};
0110 // JSON event data
0111
0112 const parsingProfileMessage = `${loadJSONFileEvents.name}: parsing JSON from '${fileURL}'`
0113 console.time(parsingProfileMessage);
0114 console.profile(parsingProfileMessage);
0115 Object.assign(allEventsObject, JSON.parse(fileText));
0116 console.timeEnd(parsingProfileMessage);
0117 console.profileEnd(parsingProfileMessage);
0118
0119 return allEventsObject;
0120 }