DEV Community

Discussion on: Async Components, FileReader, and Angular

Collapse
 
patricepeartree profile image
Patrícia Pereira

Just a small question, why do the four first bytes tell us the real type of the file?

Also, by changing your validation for cycle to a some, it looks cleaner and you can even spare yourself some iterations:

const isValid = acceptedMimeTypes.some(mimetype => contentBuffer === mimetype.magicNo);
Collapse
 
aalvarez89 profile image
Andrew Alvarez

Hi Patricia! Thanks for the some method! I had never seen it before!. I'll definitely change it.
For your other question, I'll try to reply to the best of my knowledge.
Any file doesn't matter if it's an image, binary, audio, etc. contains data which ultimately gets processed by your CPU and then gets displayed correspondingly. Let's say you open an image with a text editor, you'll see a sequence of squiggly characters that make no sense. Yet, in the beginning, the file contains a signature that tells you what kind of file it was when it got created.
I don't really know why it is like that but I guess that the developers made it that way for software to be able to recognize which kind of files they are and how to read them.