Skip to content

Commit

Permalink
chore(build): check file-type of audio/video/font files
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Nov 25, 2022
1 parent 03a4ac7 commit b75f210
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion build/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import zlib from "node:zlib";
import chalk from "chalk";
import cliProgress from "cli-progress";
import { program } from "@caporal/core";
import FileType from "file-type";
import { prompt } from "inquirer";

import { Document, slugToFolder, translationsOf } from "../content";
Expand Down Expand Up @@ -220,7 +221,23 @@ async function buildDocuments(
}

for (const filePath of fileAttachments) {
// We *could* use symlinks instead. But, there's no point :)
// Ensure that binary files contain what their extension indicates.
if (/\.(mp3|mp4|ttf|webm|woff2?)$/i.test(filePath)) {
const ext = filePath.split(".").pop();
const type = await FileType.fromFile(filePath);
if (!type) {
throw new Error(
`Failed to detect type of file attachment: ${filePath}`
);
}
if (ext.toLowerCase() !== type.ext) {
throw new Error(
`Unexpected type '${type.mime}' (*.${ext}) detected for file attachment: ${filePath}.`
);
}
}

// We *could* use symlinks instead. But, there's no point :
// Yes, a symlink is less disk I/O but it's nominal.
fs.copyFileSync(filePath, path.join(outPath, path.basename(filePath)));
}
Expand Down

0 comments on commit b75f210

Please sign in to comment.