Merge branch 'v2.1-dev'
[squeep-api-dingus] / lib / mime-helper.js
1 /* eslint-disable security/detect-object-injection */
2 'use strict';
3
4 const mimeDb = require('mime-db');
5
6 const defaultType = 'application/octet-stream';
7 const extension = {};
8 for (const [mimeType, entry] of Object.entries(mimeDb)) {
9 if (entry.extensions) {
10 entry.extensions.forEach((ext) => {
11 if (!(ext in extension)) {
12 extension[ext] = [];
13 }
14 extension[ext].push(mimeType);
15 });
16 }
17 }
18
19 /**
20 * Return a suitable type for a file extension.
21 * @param {string} ext file extension
22 * @param {string} def type to return if no match
23 */
24 const extensionToMime = (ext, def = defaultType) => extension[ext] && extension[ext][extension[ext].length - 1] || def;
25
26 module.exports = {
27 extensionToMime,
28 };