enh(mimetypes) MimeType handling extended functionality
This commit is contained in:
@@ -1,24 +1,112 @@
|
|||||||
export default new Map([
|
/**
|
||||||
['text', 'text/plain'],
|
* A custom class that wraps a Map and provides additional functionality for MIME types.
|
||||||
['html', 'text/html'],
|
*/
|
||||||
['css', 'text/css'],
|
export default class MimeTypes {
|
||||||
['csv', 'text/csv'],
|
/**
|
||||||
['js', 'text/javascript'],
|
* The underlying Map object that stores the MIME types.
|
||||||
['gif', 'image/gif'],
|
*
|
||||||
['jpeg', 'image/jpeg'],
|
* @type {Map<string, string>}
|
||||||
['png', 'image/png'],
|
* @private
|
||||||
['svg', 'image/svg+xml'],
|
*/
|
||||||
['midi', 'audio/midi'],
|
static mimeTypes = new Map([
|
||||||
['mp3', 'audio/mpeg'],
|
['text', 'text/plain'],
|
||||||
['oga', 'audio/ogg'],
|
['html', 'text/html'],
|
||||||
['mp4', 'video/mp4'],
|
['css', 'text/css'],
|
||||||
['mpeg', 'video/mpeg'],
|
['csv', 'text/csv'],
|
||||||
['ogv', 'video/ogg'],
|
['js', 'text/javascript'],
|
||||||
['zip', 'application/zip'],
|
['gif', 'image/gif'],
|
||||||
['pdf', 'application/pdf'],
|
['jpeg', 'image/jpeg'],
|
||||||
['epub', 'application/epub+zip'],
|
['png', 'image/png'],
|
||||||
['gz', 'application/gzip'],
|
['svg', 'image/svg+xml'],
|
||||||
['bin', 'application/octet-stream'],
|
['midi', 'audio/midi'],
|
||||||
['json', 'application/json'],
|
['mp3', 'audio/mpeg'],
|
||||||
['jsonld', 'application/ld+json'],
|
['oga', 'audio/ogg'],
|
||||||
]);
|
['mp4', 'video/mp4'],
|
||||||
|
['mpeg', 'video/mpeg'],
|
||||||
|
['ogv', 'video/ogg'],
|
||||||
|
['zip', 'application/zip'],
|
||||||
|
['pdf', 'application/pdf'],
|
||||||
|
['epub', 'application/epub+zip'],
|
||||||
|
['gz', 'application/gzip'],
|
||||||
|
['bin', 'application/octet-stream'],
|
||||||
|
['json', 'application/json'],
|
||||||
|
['jsonld', 'application/ld+json'],
|
||||||
|
['webp', 'image/webp'],
|
||||||
|
['avif', 'image/avif'],
|
||||||
|
['ico', 'image/x-icon'],
|
||||||
|
['webm', 'video/webm'],
|
||||||
|
['wav', 'audio/wav'],
|
||||||
|
['aac', 'audio/aac'],
|
||||||
|
['flac', 'audio/flac'],
|
||||||
|
['7z', 'application/x-7z-compressed'],
|
||||||
|
['rar', 'application/x-rar-compressed'],
|
||||||
|
['tar', 'application/x-tar'],
|
||||||
|
['xml', 'application/xml'],
|
||||||
|
['yaml', 'application/x-yaml'],
|
||||||
|
['wasm', 'application/wasm'],
|
||||||
|
['otf', 'font/otf'],
|
||||||
|
['ttf', 'font/ttf'],
|
||||||
|
['woff', 'font/woff'],
|
||||||
|
['woff2', 'font/woff2'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a MIME type exists for a given file extension.
|
||||||
|
* File extensions are case-insensitive.
|
||||||
|
*
|
||||||
|
* @param {string} extension - The file extension.
|
||||||
|
* @returns {boolean} True if the MIME type exists, false otherwise.
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
static has(extension) {
|
||||||
|
return MimeTypes.mimeTypes.has(extension.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the MIME type for a given file extension.
|
||||||
|
* File extensions are case-insensitive.
|
||||||
|
*
|
||||||
|
* @param {string} extension - The file extension.
|
||||||
|
* @returns {string|undefined} The corresponding MIME type or undefined if not found.
|
||||||
|
*/
|
||||||
|
static get(extension) {
|
||||||
|
return MimeTypes.mimeTypes.get(extension.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the MIME type for a given file extension.
|
||||||
|
* File extensions are case-insensitive.
|
||||||
|
*
|
||||||
|
* @param {string} extension - The file extension.
|
||||||
|
* @param {string} mimeType - The MIME type to set.
|
||||||
|
*/
|
||||||
|
static set(extension, mimeType) {
|
||||||
|
MimeTypes.mimeTypes.set(extension.toLowerCase(), mimeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves an iterator of MIME types belonging to a specific category.
|
||||||
|
*
|
||||||
|
* @param {string} category - The category to filter by (e.g., 'image', 'audio').
|
||||||
|
* @returns {IterableIterator<string>} An iterator of MIME types belonging to the specified category.
|
||||||
|
*/
|
||||||
|
static *getByCategory(category) {
|
||||||
|
for (const mimeType of MimeTypes.mimeTypes.values()) {
|
||||||
|
if (mimeType.startsWith(`${category}/`)) {
|
||||||
|
yield mimeType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new MIME type for a given file extension.
|
||||||
|
* File extensions are case-insensitive.
|
||||||
|
*
|
||||||
|
* @param {string} extension - The file extension.
|
||||||
|
* @param {string} mimeType - The MIME type to add.
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
static add(extension, mimeType) {
|
||||||
|
MimeTypes.mimeTypes.set(extension.toLowerCase(), mimeType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user