How to detect password protected file in Angular 14+ without using Promise calls

55 Views Asked by At

`Have a requirement of identifying the uploaded file is a password protected file or not. Based in the response I have to block the file being uploaded to the server.

Html:

<input type="file" (change)="upload($event)">

In my component I have multiple checks based on file size and type of the files and in addition to it I had added the below code refering from another article.

Detect in browser if PDF is locked or encrypted

const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function () {

var files = new Blob([reader.result], {type: 'application/pdf'});
files.text().then(x=> {
    console.log("isEncrypted", x.includes("Encrypt")) // true, if Encrypted
    console.log("isEncrypted", x.substring(x.lastIndexOf("<<"), x.lastIndexOf(">>")).includes("/Encrypt"));
    console.log(file.name);
});

The problem is after the FormControl for each document is created then the files.text() promise is called. I also tried using async and await but still facing problems to achieve it.

I need a solution where I can avoid promise calls and instantly get the result whether file is encrypted or not.

I'm prohibited to use any other third party library.

0

There are 0 best solutions below