feat(video): wire video converters, resolution validation, and timeout into app.js/worker.js
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -527,3 +527,75 @@ describe('POST /api/jobs — audio', () => {
|
||||
expect(response.body.targets).not.toContain('mp3');
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/jobs — video', () => {
|
||||
it('creates a pending job converting an mp4 upload to webm with a valid resolution', async () => {
|
||||
const fixturePath = path.join(config.storageDir, 'clip.mp4');
|
||||
await fs.writeFile(
|
||||
fixturePath,
|
||||
Buffer.concat([Buffer.from([0, 0, 0, 0x18]), Buffer.from('ftyp'), Buffer.from('isom')])
|
||||
);
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.field('targetFormats', JSON.stringify(['webm']))
|
||||
.field('qualities', JSON.stringify([720]))
|
||||
.attach('files', fixturePath, 'clip.mp4');
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body.jobs[0].status).toBe('pending');
|
||||
|
||||
const job = await getJobByUuid(prisma, response.body.jobs[0].id);
|
||||
expect(job.sourceFormat).toBe('mp4');
|
||||
expect(job.targetFormat).toBe('webm');
|
||||
expect(job.family).toBe('video');
|
||||
expect(job.quality).toBe(720);
|
||||
|
||||
await fs.unlink(fixturePath);
|
||||
});
|
||||
|
||||
it('creates a pending job with no quality value (original resolution)', async () => {
|
||||
const fixturePath = path.join(config.storageDir, 'clip-original.mp4');
|
||||
await fs.writeFile(
|
||||
fixturePath,
|
||||
Buffer.concat([Buffer.from([0, 0, 0, 0x18]), Buffer.from('ftyp'), Buffer.from('isom')])
|
||||
);
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.field('targetFormats', JSON.stringify(['mkv']))
|
||||
.attach('files', fixturePath, 'clip-original.mp4');
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
const job = await getJobByUuid(prisma, response.body.jobs[0].id);
|
||||
expect(job.quality).toBeNull();
|
||||
|
||||
await fs.unlink(fixturePath);
|
||||
});
|
||||
|
||||
it('rejects a resolution outside the fixed 480/720/1080 set', async () => {
|
||||
const fixturePath = path.join(config.storageDir, 'bad-resolution.mp4');
|
||||
await fs.writeFile(
|
||||
fixturePath,
|
||||
Buffer.concat([Buffer.from([0, 0, 0, 0x18]), Buffer.from('ftyp'), Buffer.from('isom')])
|
||||
);
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.field('targetFormats', JSON.stringify(['webm']))
|
||||
.field('qualities', JSON.stringify([360]))
|
||||
.attach('files', fixturePath, 'bad-resolution.mp4');
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body.jobs[0].error).toMatch(/Invalid quality/);
|
||||
|
||||
await fs.unlink(fixturePath);
|
||||
});
|
||||
|
||||
it('lists the other 4 video formats as targets for mp4, and never lists mp4 as its own target', async () => {
|
||||
const response = await request(app).get('/api/formats').query({ source: 'mp4' });
|
||||
|
||||
expect(response.body.targets).toEqual(expect.arrayContaining(['webm', 'mov', 'avi', 'mkv']));
|
||||
expect(response.body.targets).not.toContain('mp4');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import { createJob, getJobByUuid, getJobErrorLog } from '../src/jobs/jobReposito
|
||||
import { registerImageConverters } from '../src/converters/image.js';
|
||||
import { registerIcoConverter } from '../src/converters/ico.js';
|
||||
import { registerEbookConverter } from '../src/converters/ebook.js';
|
||||
import { registerVideoConverters } from '../src/converters/video.js';
|
||||
import { processPendingJobs } from '../src/worker.js';
|
||||
|
||||
const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
|
||||
@@ -23,6 +24,7 @@ beforeAll(async () => {
|
||||
registerImageConverters();
|
||||
registerIcoConverter();
|
||||
registerEbookConverter();
|
||||
registerVideoConverters();
|
||||
config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-worker-')) };
|
||||
await ensureStorageDirs(config);
|
||||
prisma = getPrismaClient(config);
|
||||
@@ -227,4 +229,42 @@ describe('processPendingJobs', () => {
|
||||
expect(options).toEqual({ timeout: config.ebookJobTimeoutMs });
|
||||
expect(config.ebookJobTimeoutMs).not.toBe(60000);
|
||||
});
|
||||
|
||||
it('converts a pending video job to done using config.videoJobTimeoutMs, not the default 60s timeout', async () => {
|
||||
execFileMock.mockReset();
|
||||
execFileMock.mockImplementation((file, args, options, callback) => {
|
||||
writeFileSync(args[args.length - 1], 'fake converted video output');
|
||||
callback(null, '', '');
|
||||
});
|
||||
|
||||
const uuid = '22222222-2222-4222-8222-222222222222';
|
||||
const inputFilePath = uploadPath(config, uuid, 'mp4');
|
||||
await fs.writeFile(inputFilePath, 'fake mp4 content');
|
||||
|
||||
await createJob(prisma, {
|
||||
uuid,
|
||||
family: 'video',
|
||||
sourceFormat: 'mp4',
|
||||
targetFormat: 'webm',
|
||||
originalFilename: 'clip.mp4',
|
||||
inputPath: `${uuid}.mp4`,
|
||||
inputMimeType: 'video/mp4',
|
||||
inputSizeBytes: 17,
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||
quality: 720,
|
||||
});
|
||||
|
||||
await processPendingJobs(prisma, config);
|
||||
|
||||
const job = await getJobByUuid(prisma, uuid);
|
||||
expect(job.status).toBe('done');
|
||||
const [file, args, options] = execFileMock.mock.calls[0];
|
||||
expect(file).toBe('ffmpeg');
|
||||
expect(args).toEqual([
|
||||
'-y', '-i', inputFilePath, '-c:v', 'libvpx-vp9', '-c:a', 'libopus', '-vf', 'scale=-2:720',
|
||||
outputPath(config, uuid, 'webm'),
|
||||
]);
|
||||
expect(options).toEqual({ timeout: config.videoJobTimeoutMs });
|
||||
expect(config.videoJobTimeoutMs).not.toBe(60000);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user