From edfc6cdc80e4e8fee5fa3602f3e314cefc452863 Mon Sep 17 00:00:00 2001 From: Brian Hursey Date: Sat, 28 Nov 2026 20:57:53 +0000 Subject: [PATCH] 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. --- server/index.mjs | 1 + server/raw.test.mjs | 108 ++++++++++++++++++++++++++++++++++++++ src/components/Panels.tsx | 12 +++++ src/lib/api.ts | 2 + src/lib/edits.ts | 4 ++ src/lib/import.ts | 1 + src/lib/raw.test.ts | 46 ++++++++++++++++ src/types.ts | 2 + 8 files changed, 176 insertions(+) create mode 100644 server/raw.test.mjs create mode 100644 src/lib/raw.test.ts diff --git a/server/index.mjs b/server/index.mjs index 5ff7a26..916e8a5 100644 --- a/server/index.mjs +++ b/server/index.mjs @@ -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, diff --git a/server/raw.test.mjs b/server/raw.test.mjs new file mode 100644 index 0000000..ba3d25c --- /dev/null +++ b/server/raw.test.mjs @@ -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); + } +}); diff --git a/src/components/Panels.tsx b/src/components/Panels.tsx index f061fed..2625aca 100644 --- a/src/components/Panels.tsx +++ b/src/components/Panels.tsx @@ -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) : '—'], diff --git a/src/lib/api.ts b/src/lib/api.ts index bc99958..946b91a 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -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; } diff --git a/src/lib/edits.ts b/src/lib/edits.ts index 75669e8..17cb851 100644 --- a/src/lib/edits.ts +++ b/src/lib/edits.ts @@ -652,6 +652,10 @@ export function normalizePhoto(input: Partial & { 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, diff --git a/src/lib/import.ts b/src/lib/import.ts index 5a874f0..569f505 100644 --- a/src/lib/import.ts +++ b/src/lib/import.ts @@ -162,6 +162,7 @@ async function importRaw( history: [{ label: 'Import', ts: Date.now(), edits }], raw, decode: upload.engine, + proxyBytes: upload.previewBytes, previewUrl, }); diff --git a/src/lib/raw.test.ts b/src/lib/raw.test.ts new file mode 100644 index 0000000..d6fd162 --- /dev/null +++ b/src/lib/raw.test.ts @@ -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}`); + } + }); +}); diff --git a/src/types.ts b/src/types.ts index 227f15d..7f51f10 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 */