Cover raw and TIFF import in tests and show decode details

The new tests pin the CR2/NEF/CR3/DNG and TIFF routing, container
signatures and TIFF proxy decoding, with an opt-in fixture run for real
raw files. The Info panel now reports the decoder and the proxy size, so
a file that fell back to a camera preview is visible at a glance.
This commit is contained in:
2026-11-28 20:57:53 +00:00
parent 27360ffae7
commit edfc6cdc80
8 changed files with 176 additions and 0 deletions
+1
View File
@@ -2170,6 +2170,7 @@ route('POST', '/api/photos/:id/original', async ({ req, res, params }) => {
width: info?.width || decoded.width,
height: info?.height || decoded.height,
mime: 'image/jpeg',
previewBytes: decoded.jpeg.length,
previewUrl: `/api/photos/${params.id}/preview`,
thumbUrl: `/api/photos/${params.id}/thumb`,
note: decoded.note ?? null,
+108
View File
@@ -0,0 +1,108 @@
import { readFileSync, existsSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import {
RAW_EXTENSIONS,
decodeRaw,
decodeStill,
isRawName,
isStillName,
looksLikeRaw,
rawEngine,
} from './raw.mjs';
/** TIFF-family containers: CR2, NEF and DNG all start with the same header. */
function tiffHeader() {
const buffer = Buffer.alloc(64);
Buffer.from('49492a00', 'hex').copy(buffer, 0);
return buffer;
}
function withAscii(bytes) {
const buffer = Buffer.alloc(64);
buffer.write(bytes, 0, 'latin1');
return buffer;
}
describe('raw format coverage', () => {
it.each(['cr2', 'nef', 'cr3', 'dng'])('routes .%s to the raw decoder', (ext) => {
expect(RAW_EXTENSIONS.has(ext)).toBe(true);
expect(isRawName(`IMG_0001.${ext}`)).toBe(true);
expect(isRawName(`IMG_0001.${ext.toUpperCase()}`)).toBe(true);
expect(isStillName(`IMG_0001.${ext}`)).toBe(false);
});
it.each(['tif', 'tiff'])('routes .%s to the still decoder', (ext) => {
expect(isStillName(`scan.${ext}`)).toBe(true);
expect(isStillName(`scan.${ext.toUpperCase()}`)).toBe(true);
expect(isRawName(`scan.${ext}`)).toBe(false);
});
it('recognises the container signatures of the main families', () => {
expect(looksLikeRaw(tiffHeader())).toBe(true); // CR2, NEF, DNG
const cr3 = Buffer.alloc(64);
cr3.write('ftyp', 4, 'latin1');
expect(looksLikeRaw(cr3)).toBe(true);
expect(looksLikeRaw(withAscii('IIRO'))).toBe(true); // ORF
expect(looksLikeRaw(withAscii('IIU\0'))).toBe(true); // RW2
expect(looksLikeRaw(withAscii('FUJIFILMCCD-RAW'))).toBe(true); // RAF
expect(looksLikeRaw(withAscii('FOVb'))).toBe(true); // X3F
expect(looksLikeRaw(Buffer.alloc(64))).toBe(false);
expect(looksLikeRaw(Buffer.alloc(8))).toBe(false);
});
});
describe('TIFF decoding', () => {
it('turns a TIFF into a JPEG proxy', async () => {
const { default: sharp } = await import('sharp');
const tiff = await sharp({
create: {
width: 48,
height: 32,
channels: 3,
background: { r: 20, g: 120, b: 210 },
},
})
.tiff()
.toBuffer();
const decoded = await decodeStill(tiff);
expect(decoded.engine).toBe('sharp');
expect(decoded.ext).toBe('jpg');
expect(decoded.width).toBe(48);
expect(decoded.height).toBe(32);
expect(decoded.jpeg.subarray(0, 2)).toEqual(Buffer.from([0xff, 0xd8]));
});
it('rejects a file that is not an image', async () => {
await expect(decodeStill(Buffer.from('not a tiff at all'))).rejects.toThrow(
/could not decode/i,
);
});
});
/**
* Real containers are optional: point LUMINARY_RAW_FIXTURES at a directory
* holding sample.cr2, sample.nef, sample.cr3 and sample.dng. The samples are
* deliberately not committed (they are 10-40 MB each and camera-specific), but
* when they are present this exercises the same libraw path an import takes.
*/
const FIXTURES = process.env.LUMINARY_RAW_FIXTURES;
const fixture = (ext) => (FIXTURES ? path.join(FIXTURES, `sample.${ext}`) : '');
const available = (ext) => Boolean(FIXTURES) && existsSync(fixture(ext));
const rawFormats = ['cr2', 'nef', 'cr3', 'dng'];
describe.skipIf(!FIXTURES)('real raw containers', () => {
it('has libraw available', async () => {
expect(await rawEngine()).toBe('ready');
});
for (const ext of rawFormats) {
it.skipIf(!available(ext))(`decodes sample.${ext}`, async () => {
const decoded = await decodeRaw(readFileSync(fixture(ext)));
expect(decoded.jpeg.length).toBeGreaterThan(4096);
expect(['libraw', 'embedded']).toContain(decoded.engine);
expect(decoded.width).toBeGreaterThan(0);
expect(decoded.height).toBeGreaterThan(0);
}, 120000);
}
});
+12
View File
@@ -2615,10 +2615,22 @@ export function MetadataPanel({
const event = events.find((e) => e.id === photo.eventId);
const [keyword, setKeyword] = useState('');
const [presetId, setPresetId] = useState('');
const decodeLabel = photo.raw
? photo.decode === 'embedded'
? 'LibRaw · camera preview fallback'
: 'LibRaw demosaic'
: photo.decode === 'sharp'
? 'sharp JPEG proxy'
: 'Browser';
const proxyLabel = `${photo.width} × ${photo.height}${
photo.proxyBytes ? ` · ${(photo.proxyBytes / (1024 * 1024)).toFixed(1)} MB` : ''
}`;
const rows: [string, string][] = [
['File', photo.name],
['Dimensions', `${photo.width} × ${photo.height}`],
['Size', `${(photo.size / (1024 * 1024)).toFixed(2)} MB`],
['Decode', decodeLabel],
['Proxy', proxyLabel],
['Camera', photo.meta.camera ?? '—'],
['Lens', photo.meta.lens ?? '—'],
['ISO', photo.meta.iso ? String(photo.meta.iso) : '—'],
+2
View File
@@ -30,6 +30,8 @@ export interface UploadResult {
size?: number;
previewUrl?: string;
thumbUrl?: string;
/** Size of the JPEG proxy the server wrote, when it decoded this file. */
previewBytes?: number;
note?: string | null;
}
+4
View File
@@ -652,6 +652,10 @@ export function normalizePhoto(input: Partial<PhotoRecord> & { id: string }): Ph
typeof input.previewVersion === 'number' && Number.isFinite(input.previewVersion)
? input.previewVersion
: undefined,
proxyBytes:
typeof input.proxyBytes === 'number' && Number.isFinite(input.proxyBytes)
? input.proxyBytes
: undefined,
proofAt: typeof input.proofAt === 'number' && Number.isFinite(input.proofAt) ? input.proofAt : undefined,
name: input.name ?? 'Untitled',
width: input.width ?? 0,
+1
View File
@@ -162,6 +162,7 @@ async function importRaw(
history: [{ label: 'Import', ts: Date.now(), edits }],
raw,
decode: upload.engine,
proxyBytes: upload.previewBytes,
previewUrl,
});
+46
View File
@@ -0,0 +1,46 @@
import { describe, expect, it } from 'vitest';
import {
RAW_ACCEPT,
RAW_EXTENSIONS,
SERVER_IMAGE_EXTENSIONS,
isRawFile,
isTiffFile,
} from './raw';
import { isSupportedFile } from './import';
/** isRawFile/isTiffFile only read the name; isSupportedFile also reads type. */
function fakeFile(name: string, type = ''): File {
return { name, type } as File;
}
describe('import format routing', () => {
it.each(['CR2', 'NEF', 'CR3', 'DNG'])('sends .%s to the server raw decoder', (ext) => {
const file = fakeFile(`IMG_0001.${ext}`, 'application/octet-stream');
expect(RAW_EXTENSIONS.has(ext.toLowerCase())).toBe(true);
expect(isRawFile(file)).toBe(true);
expect(isTiffFile(file)).toBe(false);
expect(isSupportedFile(file)).toBe(true);
});
it.each(['TIF', 'TIFF'])('sends .%s to the server still decoder', (ext) => {
const file = fakeFile(`scan.${ext}`, 'image/tiff');
expect(SERVER_IMAGE_EXTENSIONS.has(ext.toLowerCase())).toBe(true);
expect(isTiffFile(file)).toBe(true);
expect(isRawFile(file)).toBe(false);
expect(isSupportedFile(file)).toBe(true);
});
it('still accepts the browser-decoded formats', () => {
expect(isSupportedFile(fakeFile('photo.jpg', 'image/jpeg'))).toBe(true);
expect(isSupportedFile(fakeFile('photo.png', 'image/png'))).toBe(true);
expect(isSupportedFile(fakeFile('photo.webp', 'image/webp'))).toBe(true);
expect(isSupportedFile(fakeFile('photo.avif', 'image/avif'))).toBe(true);
expect(isSupportedFile(fakeFile('notes.txt', 'text/plain'))).toBe(false);
});
it('offers every supported extension in the file picker', () => {
for (const ext of ['cr2', 'nef', 'cr3', 'dng', 'tif', 'tiff', 'sraw']) {
expect(RAW_ACCEPT.split(',')).toContain(`.${ext}`);
}
});
});
+2
View File
@@ -327,6 +327,8 @@ export interface PhotoRecord {
thumbUrl?: string;
/** Bumped when the stored RAW is re-decoded, to invalidate cached previews. */
previewVersion?: number;
/** Bytes of the server-written editable proxy (raw and TIFF imports). */
proxyBytes?: number;
/** set when this photo was imported from an Immich album */
immich?: { albumId: string; assetId: string; checksum: string };
/** timestamp of the last rendered client proof published to the server */