Skip to content

Instantly share code, notes, and snippets.

@Totati
Last active June 8, 2021 12:22
Show Gist options
  • Save Totati/11ddde815d5bf255d38ce823261c43ac to your computer and use it in GitHub Desktop.
Save Totati/11ddde815d5bf255d38ce823261c43ac to your computer and use it in GitHub Desktop.
Read the magic number of a file
export async function getMagicNumber(file: File, signatureLength: number = 4) {
return file
.slice(0, signatureLength)
.arrayBuffer()
.then((buffer) =>
Array.from(new Uint8Array(buffer))
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('')
.toUpperCase()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment