Skip to content

Commit

Permalink
Resolves multiple issues with validateFileUpload resolves ohcnetwork#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirrajsingh committed Mar 23, 2024
1 parent e1fb260 commit 8ed5aa1
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/Components/Patient/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,45 @@ export const FileUpload = (props: FileUploadProps) => {
});
};

const validateFileUpload = () => {
const isFileTypeSupported = (file: File): Promise<boolean> => {
console.log("here3");
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
const arr = new Uint8Array(reader.result as ArrayBuffer).subarray(0, 4);
let header = "";
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}

// Define magic numbers for image, audio, and PDF files
const imageMagicNumbers = ["89504e47", "ffd8ffe0", "ffd8ffe1"]; // PNG, JPG, JPEG
const audioMagicNumbers = ["494433", "4d546864", "52494646"]; // MP3, MIDI, WAV
const pdfMagicNumber = "25504446"; // PDF

// Check if the header matches any of the magic numbers
if (
imageMagicNumbers.includes(header) ||
audioMagicNumbers.includes(header) ||
header === pdfMagicNumber
) {
// File type is supported
resolve(true);
} else {
// File type is not supported
resolve(false);
}
};

reader.onerror = () => {
reject(new Error("Failed to read file."));
};

reader.readAsArrayBuffer(file);
});
};
const validateFileUpload = async () => {
console.log("here");
const filenameLength = uploadFileName.trim().length;
const f = file;
if (f === undefined || f === null) {
Expand All @@ -989,6 +1027,13 @@ export const FileUpload = (props: FileUploadProps) => {
setUploadFileError("Maximum size of files is 100 MB");
return false;
}
const fileTypeSupported = await isFileTypeSupported(f);
if (!fileTypeSupported) {
setUploadFileError(
"File type not supported.Supported file formats : .png, .jpeg, .jpg, .mp3, .midi, .wav, .pdf"
);
return false;
}
return true;
};
const markUploadComplete = (data: CreateFileResponse) => {
Expand All @@ -1003,7 +1048,8 @@ export const FileUpload = (props: FileUploadProps) => {
};

const handleUpload = async () => {
if (!validateFileUpload()) return;
if (!(await validateFileUpload())) return;
console.log("here2");
const f = file;

const category = "UNSPECIFIED";
Expand Down Expand Up @@ -1572,6 +1618,7 @@ export const FileUpload = (props: FileUploadProps) => {
onClick={() => {
setFile(null);
setUploadFileName("");
setUploadFileError("");
}}
>
<CareIcon icon="l-times" className="text-lg" />
Expand Down

0 comments on commit 8ed5aa1

Please sign in to comment.