brand new unit tests finally
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m9s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m9s
This commit is contained in:
@@ -48,7 +48,9 @@ export const generateFileChecksum = async (file: File): Promise<string> => {
|
||||
console.error('crypto.subtle is not available in this environment. SHA-256 generation will fail.');
|
||||
}
|
||||
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
|
||||
// Ensure buffer is a TypedArray or ArrayBuffer that crypto.subtle accepts
|
||||
// Wrapping in Uint8Array handles both ArrayBuffer and SharedArrayBuffer, and helps with JSDOM compatibility
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', new Uint8Array(buffer));
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
return hashHex;
|
||||
|
||||
@@ -42,13 +42,45 @@ const renderPageToImageFile = async (
|
||||
return new File([blob], newFileName, { type: 'image/jpeg' });
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper to read file as ArrayBuffer using FileReader (fallback for environments missing file.arrayBuffer)
|
||||
*/
|
||||
const readFileAsArrayBuffer = (file: File): Promise<ArrayBuffer> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
if (reader.result instanceof ArrayBuffer) {
|
||||
resolve(reader.result);
|
||||
} else {
|
||||
reject(new Error('FileReader result was not an ArrayBuffer'));
|
||||
}
|
||||
};
|
||||
reader.onerror = () => {
|
||||
reject(new Error(`FileReader error: ${reader.error?.message}`));
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches a PDF document from a File object.
|
||||
* @param pdfFile The PDF file.
|
||||
* @returns A promise that resolves to the pdf.js document object.
|
||||
*/
|
||||
const getPdfDocument = async (pdfFile: File) => {
|
||||
const arrayBuffer = await pdfFile.arrayBuffer();
|
||||
let arrayBuffer: ArrayBuffer;
|
||||
|
||||
if (typeof pdfFile.arrayBuffer === 'function') {
|
||||
try {
|
||||
arrayBuffer = await pdfFile.arrayBuffer();
|
||||
} catch (error) {
|
||||
console.warn('pdfFile.arrayBuffer() failed, falling back to FileReader', error);
|
||||
arrayBuffer = await readFileAsArrayBuffer(pdfFile);
|
||||
}
|
||||
} else {
|
||||
arrayBuffer = await readFileAsArrayBuffer(pdfFile);
|
||||
}
|
||||
|
||||
const pdf: PDFDocumentProxy = await pdfjsLib.getDocument(arrayBuffer).promise;
|
||||
return pdf;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user