From 93d64642f393848c9b895a5a46cdee7a2355aae6 Mon Sep 17 00:00:00 2001 From: Timothy Carambat Date: Wed, 2 Oct 2024 15:13:31 -0700 Subject: [PATCH] Add exception handling for special case files like `Dockerfile` and `Jenkinsfile` (#2410) --- collector/processSingleFile/index.js | 2 +- collector/utils/files/mime.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/collector/processSingleFile/index.js b/collector/processSingleFile/index.js index bdefb79e09..a00b139ed4 100644 --- a/collector/processSingleFile/index.js +++ b/collector/processSingleFile/index.js @@ -38,7 +38,7 @@ async function processSingleFile(targetFilename, options = {}) { }; const fileExtension = path.extname(fullFilePath).toLowerCase(); - if (!fileExtension) { + if (fullFilePath.includes(".") && !fileExtension) { return { success: false, reason: `No file extension found. This file cannot be processed.`, diff --git a/collector/utils/files/mime.js b/collector/utils/files/mime.js index b747d59755..ad3ff57829 100644 --- a/collector/utils/files/mime.js +++ b/collector/utils/files/mime.js @@ -1,5 +1,5 @@ const MimeLib = require("mime"); - +const path = require("path"); class MimeDetector { nonTextTypes = ["multipart", "image", "model", "audio", "video"]; badMimes = [ @@ -44,8 +44,26 @@ class MimeDetector { ); } + // These are file types that are not detected by the mime library and need to be processed as text files. + // You should only add file types that are not detected by the mime library, are parsable as text, and are files + // with no extension. Otherwise, their extension should be added to the overrides array. + #specialTextFileTypes = ["dockerfile", "jenkinsfile"]; + + /** + * Returns the MIME type of the file. If the file has no extension found, it will be processed as a text file. + * @param {string} filepath + * @returns {string} + */ getType(filepath) { - return this.lib.getType(filepath); + const parsedMime = this.lib.getType(filepath); + if (!!parsedMime) return parsedMime; + + // If the mime could not be parsed, it could be a special file type like Dockerfile or Jenkinsfile + // which we can reliably process as text files. + const baseName = path.basename(filepath)?.toLowerCase(); + if (this.#specialTextFileTypes.includes(baseName)) return "text/plain"; + + return null; } }