Fix JXL mimetype (#970)

This commit is contained in:
Jake Archibald
2021-03-10 14:56:59 +00:00
committed by GitHub
parent 42cfbe11a1
commit ef77da9e96
3 changed files with 18 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import {
canDecodeImageType, canDecodeImageType,
abortable, abortable,
assertSignal, assertSignal,
ImageMimeTypes,
} from '../util'; } from '../util';
import { import {
PreprocessorState, PreprocessorState,
@@ -102,7 +103,7 @@ async function decodeImage(
if (mimeType === 'image/webp') { if (mimeType === 'image/webp') {
return await workerBridge.webpDecode(signal, blob); return await workerBridge.webpDecode(signal, blob);
} }
if (mimeType === 'image/jpegxl') { if (mimeType === 'image/jxl') {
return await workerBridge.jxlDecode(signal, blob); return await workerBridge.jxlDecode(signal, blob);
} }
if (mimeType === 'image/webp2') { if (mimeType === 'image/webp2') {
@@ -178,10 +179,13 @@ async function compressImage(
encodeData.options as any, encodeData.options as any,
); );
// This type ensures the image mimetype is consistent with our mimetype sniffer
const type: ImageMimeTypes = encoder.meta.mimeType;
return new File( return new File(
[compressedData], [compressedData],
sourceFilename.replace(/.[^.]*$/, `.${encoder.meta.extension}`), sourceFilename.replace(/.[^.]*$/, `.${encoder.meta.extension}`),
{ type: encoder.meta.mimeType }, { type },
); );
} }

View File

@@ -137,7 +137,7 @@ export function blobToText(blob: Blob): Promise<string> {
return new Response(blob).text(); return new Response(blob).text();
} }
const magicNumberToMimeType = new Map<RegExp, string>([ const magicNumberMapInput = [
[/^%PDF-/, 'application/pdf'], [/^%PDF-/, 'application/pdf'],
[/^GIF87a/, 'image/gif'], [/^GIF87a/, 'image/gif'],
[/^GIF89a/, 'image/gif'], [/^GIF89a/, 'image/gif'],
@@ -150,11 +150,17 @@ const magicNumberToMimeType = new Map<RegExp, string>([
[/^RIFF....WEBPVP8[LX ]/, 'image/webp'], [/^RIFF....WEBPVP8[LX ]/, 'image/webp'],
[/^\xF4\xFF\x6F/, 'image/webp2'], [/^\xF4\xFF\x6F/, 'image/webp2'],
[/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/, 'image/avif'], [/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/, 'image/avif'],
[/^\xff\x0a/, 'image/jpegxl'], [/^\xff\x0a/, 'image/jxl'],
[/^\x00\x00\x00\x0cJXL \x0d\x0a\x87\x0a/, 'image/jpegxl'], [/^\x00\x00\x00\x0cJXL \x0d\x0a\x87\x0a/, 'image/jxl'],
]); ] as const;
export async function sniffMimeType(blob: Blob): Promise<string> { export type ImageMimeTypes = typeof magicNumberMapInput[number][1];
const magicNumberToMimeType = new Map<RegExp, ImageMimeTypes>(
magicNumberMapInput,
);
export async function sniffMimeType(blob: Blob): Promise<ImageMimeTypes | ''> {
const firstChunk = await blobToArrayBuffer(blob.slice(0, 16)); const firstChunk = await blobToArrayBuffer(blob.slice(0, 16));
const firstChunkString = Array.from(new Uint8Array(firstChunk)) const firstChunkString = Array.from(new Uint8Array(firstChunk))
.map((v) => String.fromCodePoint(v)) .map((v) => String.fromCodePoint(v))

View File

@@ -15,7 +15,7 @@ import type { EncodeOptions } from 'codecs/jxl/enc/jxl_enc';
export { EncodeOptions }; export { EncodeOptions };
export const label = 'JPEG XL (beta)'; export const label = 'JPEG XL (beta)';
export const mimeType = 'image/jpegxl'; export const mimeType = 'image/jxl';
export const extension = 'jxl'; export const extension = 'jxl';
export const defaultOptions: EncodeOptions = { export const defaultOptions: EncodeOptions = {
speed: 4, speed: 4,