Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,17 @@ function extractVideoFrame(buf) {
const os = require('os');
const path = require('path');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vec-'));
const input = path.join(dir, 'clip.bin');
const output = path.join(dir, 'frame.png');
fs.writeFileSync(input, buf);
const result = spawnSync(ffmpeg, ['-y', '-ss', '1', '-i', input, '-frames:v', '1', output], { timeout: 8000 });
if (result.status !== 0 || !fs.existsSync(output)) return null;
return fs.readFileSync(output);
try {
const input = path.join(dir, 'clip.bin');
const output = path.join(dir, 'frame.png');
fs.writeFileSync(input, buf);
const result = spawnSync(ffmpeg, ['-y', '-ss', '1', '-i', input, '-frames:v', '1', output], { timeout: 8000 });
if (result.status !== 0 || !fs.existsSync(output)) return null;
return fs.readFileSync(output);
} finally {
// The clip and its frame are a customer's attachment. They do not stay on disk.
fs.rmSync(dir, { recursive: true, force: true });
}
}

async function videoErrorLines(attachments, { fetchImpl, extractFrame, recognize } = {}) {
Expand Down
16 changes: 16 additions & 0 deletions test/attachments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,19 @@ test('a video frame keeps the error line and the clip is not sent on', async ()
{ video: true }
), false);
});

test('a customer video is not left on disk after its frame is read', async () => {
const fs = require('node:fs');
const os = require('node:os');
const leftovers = () => fs.readdirSync(os.tmpdir()).filter((name) => name.startsWith('vec-'));
const before = new Set(leftovers());
const lines = await videoErrorLines(
[{ name: 'clip.mp4', contentType: 'video/mp4', url: 'https://cdn.example/clip.mp4' }],
{
fetchImpl: async () => ({ ok: true, arrayBuffer: async () => Buffer.from('not a real video') }),
recognize: async () => 'server_error 1011',
}
);
assert.equal(lines, '');
assert.deepEqual(leftovers().filter((name) => !before.has(name)), []);
});