Import TIFF through the server and survive a wedged raw decoder
Browsers have no TIFF decoder, so TIFF import failed at createImageBitmap and the develop canvas asked for the undecodable original. TIFF now takes the raw proxy path (original stored, sharp writes the JPEG proxy) and the editor reads that proxy. LibRaw demosaic and preview calls are bounded so one bad file falls back to the embedded preview instead of hanging.
This commit is contained in:
+43
-6
@@ -28,6 +28,26 @@ export function isRawName(name = '') {
|
||||
return RAW_EXTENSIONS.has(ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard images that browsers cannot decode. TIFF takes the raw upload path
|
||||
* (store the original, serve a JPEG proxy) but is decoded by sharp, not LibRaw.
|
||||
*/
|
||||
export const SERVER_IMAGE_EXTENSIONS = new Set(['tif', 'tiff']);
|
||||
|
||||
export function isStillName(name = '') {
|
||||
const ext = name.split('.').pop()?.toLowerCase() ?? '';
|
||||
return SERVER_IMAGE_EXTENSIONS.has(ext);
|
||||
}
|
||||
|
||||
/** Rejects instead of waiting forever when a decoder wedges on one file. */
|
||||
function withTimeout(promise, ms, message) {
|
||||
let timer;
|
||||
const guard = new Promise((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(message)), ms);
|
||||
});
|
||||
return Promise.race([promise, guard]).finally(() => clearTimeout(timer));
|
||||
}
|
||||
|
||||
/** True when the buffer starts with a container signature LibRaw can open. */
|
||||
export function looksLikeRaw(buffer) {
|
||||
if (!buffer || buffer.length < 64) return false;
|
||||
@@ -45,6 +65,19 @@ export function looksLikeRaw(buffer) {
|
||||
|
||||
let engineState = 'unloaded';
|
||||
|
||||
export async function decodeStill(buffer) {
|
||||
const proxy = await toJpegProxy(buffer, RAW_PREVIEW_EDGE);
|
||||
if (!proxy) throw new Error('Could not decode this image');
|
||||
const size = await jpegInfo(proxy.data);
|
||||
return {
|
||||
jpeg: proxy.data,
|
||||
width: size?.width || proxy.width,
|
||||
height: size?.height || proxy.height,
|
||||
engine: 'sharp',
|
||||
ext: proxy.ext,
|
||||
};
|
||||
}
|
||||
|
||||
export async function rawEngine() {
|
||||
if (engineState === 'unloaded') {
|
||||
try {
|
||||
@@ -187,10 +220,14 @@ export function extractEmbeddedJpeg(buffer) {
|
||||
async function embeddedPreview(buffer) {
|
||||
if ((await rawEngine()) === 'ready') {
|
||||
try {
|
||||
const thumb = await rawPool.use(async (lib) => {
|
||||
await lib.open(Buffer.from(buffer), { useCameraWb: true });
|
||||
return lib.thumbnailData();
|
||||
});
|
||||
const thumb = await withTimeout(
|
||||
rawPool.use(async (lib) => {
|
||||
await lib.open(Buffer.from(buffer), { useCameraWb: true });
|
||||
return lib.thumbnailData();
|
||||
}),
|
||||
12000,
|
||||
'LibRaw preview timed out',
|
||||
);
|
||||
if (thumb?.data && thumb.format === 'jpeg' && thumb.data.length > 4096) {
|
||||
return Buffer.from(thumb.data);
|
||||
}
|
||||
@@ -209,10 +246,10 @@ export async function decodeRaw(buffer) {
|
||||
let librawError = null;
|
||||
if (looksLikeRaw(buffer)) {
|
||||
try {
|
||||
let frame = await demosaic(buffer, true);
|
||||
let frame = await withTimeout(demosaic(buffer, true), 45000, 'LibRaw timed out on this file');
|
||||
// No usable matrix (an odd body or older engine): decode the familiar sRGB
|
||||
// way instead of shipping camera samples nobody can convert.
|
||||
if (!frame.rgbCam || !frame.rgb16) frame = await demosaic(buffer, false);
|
||||
if (!frame.rgbCam || !frame.rgb16) frame = await withTimeout(demosaic(buffer, false), 45000, 'LibRaw timed out on this file');
|
||||
const { width, height, rgbCam } = frame;
|
||||
// Both buffers are rewritten in place: `rgb` becomes the sRGB proxy and
|
||||
// `rgb16` the camera-native sidecar, sharing one white point.
|
||||
|
||||
Reference in New Issue
Block a user