projects
/
squeep-api-dingus
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
fdabc31
)
minor refactor of extension to mime-type lookups
author
Justin Wind
<justin.wind+git@gmail.com>
Sat, 26 Aug 2023 18:45:15 +0000
(11:45 -0700)
committer
Justin Wind
<justin.wind+git@gmail.com>
Sat, 26 Aug 2023 18:45:15 +0000
(11:45 -0700)
lib/mime-helper.js
patch
|
blob
|
history
diff --git
a/lib/mime-helper.js
b/lib/mime-helper.js
index 086dc6a4bf16b8a59765b8f60e97d23fe96f849f..687d27b7c33bf9baca52c9e47df45313dc7cb542 100644
(file)
--- a/
lib/mime-helper.js
+++ b/
lib/mime-helper.js
@@
-4,15
+4,23
@@
const mimeDb = require('mime-db');
const defaultType = 'application/octet-stream';
const mimeDb = require('mime-db');
const defaultType = 'application/octet-stream';
+
+/**
+ * Expects mime-db source data as object:
+ * mime-type:
+ * source: <String>
+ * charset: <String>
+ * compressible: <Boolean>
+ * extensions: <String[]>
+ * Populate `extension` as an index from extensions to mime-types.
+ */
const extension = {};
for (const [mimeType, entry] of Object.entries(mimeDb)) {
const extension = {};
for (const [mimeType, entry] of Object.entries(mimeDb)) {
- if (entry.extensions) {
- entry.extensions.forEach((ext) => {
- if (!(ext in extension)) {
- extension[ext] = [];
- }
- extension[ext].push(mimeType);
- });
+ for (const ext of entry?.extensions || []) {
+ if (!(ext in extension)) {
+ extension[ext] = [];
+ }
+ extension[ext].push(mimeType);
}
}
}
}
@@
-20,8
+28,9
@@
for (const [mimeType, entry] of Object.entries(mimeDb)) {
* Return a suitable type for a file extension.
* @param {string} ext file extension
* @param {string} def type to return if no match
* Return a suitable type for a file extension.
* @param {string} ext file extension
* @param {string} def type to return if no match
+ * @returns {string}
*/
*/
-const extensionToMime = (ext, def = defaultType) => extension
[ext] && extension[ext][extension[ext].length - 1
] || def;
+const extensionToMime = (ext, def = defaultType) => extension
?.[ext]?.[0
] || def;
module.exports = {
extensionToMime,
module.exports = {
extensionToMime,