fix(journey): align media validation with server checks

Validate spooled uploads in order with the shared iterator and keep media format detection in a shared utility. Adapt the controller unit fixtures and cover raster signatures, including animated PNG.
This commit is contained in:
Maurice
2026-12-02 09:27:30 +01:00
parent 24ddc3e4b0
commit 3089aee593
7 changed files with 161 additions and 111 deletions
@@ -23,6 +23,12 @@ import os from 'node:os';
import path from 'node:path';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
// Route unit tests use stand-in uploads. Integration tests exercise real file headers.
vi.mock('../../../src/utils/media-format', async (importOriginal) => ({
...(await importOriginal<typeof import('../../../src/utils/media-format')>()),
fileMatchesRasterType: vi.fn().mockResolvedValue(true),
}));
const user = { id: 1, username: 'u', role: 'user', email: 'u@example.test' } as User;
function svc(o: Partial<JourneyService> = {}): JourneyService {
@@ -8,6 +8,7 @@
* kept, a PNG whose checksums still hold and still decodes).
*/
import { redactLocationInBuffer, redactLocationInFile } from '../../../../src/nest/memories/media-redact';
import { fileMatchesRasterType } from '../../../../src/utils/media-format';
import { jpegWithExif } from '../../../helpers/exif-jpeg';
import exifr from 'exifr';
@@ -102,6 +103,28 @@ function box(type: string, ...parts: Buffer[]): Buffer {
return Buffer.concat([head, body]);
}
describe('fileMatchesRasterType', () => {
it.each([
['image/jpeg', 'ffd8ff'],
['image/png', '89504e470d0a1a0a'],
['image/apng', '89504e470d0a1a0a'],
['image/gif', '4749463839610000'],
['image/webp', '524946460000000057454250'],
['image/avif', '00000010667479706176696600000000'],
['image/heic', '00000010667479706865696300000000'],
['image/bmp', '424d000000000000000000000000'],
['image/vnd.microsoft.icon', '000001000100'],
])('recognizes a %s header', async (contentType, hex) => {
const file = writeTemp('raster-header', Buffer.from(hex, 'hex'));
await expect(fileMatchesRasterType(file, contentType)).resolves.toBe(true);
});
it('distinguishes an image header from bytes of a different format', async () => {
const file = writeTemp('wrong-image-format', phonePhoto());
await expect(fileMatchesRasterType(file, 'image/png')).resolves.toBe(false);
});
});
/** A QuickTime movie as an iPhone lays out its location: udta location atom and the mdta keys of moov/meta. */
function iphoneMovie(): Buffer {
const xyz = Buffer.alloc(4 + ISO6709.length);
@@ -1,4 +1,4 @@
import { contentTypeFor } from '../../../../src/nest/storage/content-type';
import { contentTypeFor, journeyInlineType } from '../../../../src/nest/storage/content-type';
import { describe, it, expect } from 'vitest';
@@ -13,3 +13,16 @@ describe('contentTypeFor', () => {
expect(contentTypeFor('files/weird.xyzzy')).toBe('application/octet-stream');
});
});
describe('journeyInlineType', () => {
it.each([
['picture.apng', 'image/apng'],
['picture.HEIF', 'image/heif'],
['clip.M4V', 'video/mp4'],
['clip.webm', 'video/webm'],
['unknown.svg', undefined],
['unknown.txt', undefined],
] as const)('uses the inline media type for %s', (name, type) => {
expect(journeyInlineType(name)).toBe(type);
});
});