Special case jp2 rendering

This commit is contained in:
Manav Rathi 2024-07-06 16:57:46 +05:30
parent c86be54ac1
commit d0f585fc97
No known key found for this signature in database
2 changed files with 28 additions and 15 deletions

View File

@ -26,6 +26,15 @@ const needsJPEGConversionExtensions = [
export const needsJPEGConversion = (extension: string) =>
needsJPEGConversionExtensions.includes(extension.toLowerCase());
/**
* Return true if {@link extension} _might_ be supported by the user's browser.
*
* For example, JPEG 2000 (jp2) is supported by Safari, but not by Chrome or
* Firefox, and this function will return true for `jp2`.
*/
export const hasPartialBrowserSupport = (extension: string) =>
["jp2"].includes(extension.toLowerCase());
/**
* Return `true` if {@link extension} in for an HEIC-like file.
*/

View File

@ -1,4 +1,4 @@
import { needsJPEGConversion } from "@/media/formats";
import { hasPartialBrowserSupport, needsJPEGConversion } from "@/media/formats";
import { heicToJPEG } from "@/media/heic-convert";
import { isDesktop } from "@/next/app";
import log from "@/next/log";
@ -130,21 +130,25 @@ export const renderableImageBlob = async (
return await heicToJPEG(imageBlob);
}
return undefined;
// Continue if it might be possibly supported in some browsers,
// otherwise bail out.
if (!hasPartialBrowserSupport(extension)) return undefined;
}
// Either it is something that the browser already knows how to render
// (e.g. JPEG/PNG), or is a file extension that might be supported in
// some browsers (e.g. JPEG 2000), or a file extension that we haven't
// specifically whitelisted for conversion (any arbitrary extension not
// part of `needsJPEGConversion`).
//
// Give it to the browser, attaching the mime type if possible.
const mimeType = fileTypeInfo.mimeType;
if (!mimeType) {
log.info("Trying to render a file without a MIME type", fileName);
return imageBlob;
} else {
// Either it is something that the browser already knows how to
// render, or a file extension that we haven't specifically
// whitelisted for conversion.
const mimeType = fileTypeInfo.mimeType;
if (!mimeType) {
log.info(
"Trying to render a file without a MIME type",
fileName,
);
return imageBlob;
} else {
return new Blob([imageBlob], { type: mimeType });
}
return new Blob([imageBlob], { type: mimeType });
}
} catch (e) {
log.error(`Failed to get renderable image for ${fileName}`, e);