Use createImageBitmap as hailmary

This commit is contained in:
Surma
2018-07-19 14:37:36 +01:00
parent b7c223bc0d
commit 13ac3ed5b2
3 changed files with 27 additions and 36 deletions

View File

@@ -128,16 +128,18 @@ const magicNumberToMimeType = new Map<RegExp, string>([
[/^RIFF....WEBPVP8 /, 'image/webp'],
]);
export async function sniffMimeType(blob: Blob): Promise<string | undefined> {
const firstChunk = await blobToArrayBuffer(blob.slice(0, 1024));
const firstChunkString = Array.from(
new Uint8Array(firstChunk)).map(v => String.fromCodePoint(v),
).join('');
export async function sniffMimeType(blob: Blob): Promise<string | ''> {
const firstChunk = await blobToArrayBuffer(blob.slice(0, 16));
const firstChunkString =
Array.from(new Uint8Array(firstChunk))
.map(v => String.fromCodePoint(v))
.join('');
for (const [detector, mimeType] of magicNumberToMimeType.entries()) {
if (detector.test(firstChunkString)) {
return mimeType;
}
}
return '';
}
export function createImageBitmapPolyfill(blob: Blob): Promise<ImageBitmap> {