Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const fs = require('fs');
- const path = require('path');
- const MIMETYPE_OCTEAT_STREAM = 'application/octeat-stream';
- class MimeTypes {
- constructor(file) {
- this.types = new Map;
- this.extensions = new Map;
- this.load(file);
- }
- load(file) {
- // if (!fs.existsSync(file)) {
- // console.warn(`File does not exists: ${file}`);
- // return;
- // }
- const data = fs.readFileSync(file, 'ascii');
- const lines = data.split('\n');
- for (let line of lines) {
- line = line.replace(/#.*/, '').trim();
- if (line) {
- const fields = line.split(/\s+/);
- // const type = fields.shift().toLowerCase();
- // const extensions = fields.map(
- // Function.prototype.call,
- // String.prototype.toLowerCase
- // );
- // Указан MIME type, но не укзаны расширения
- if (1 > fields.length) {
- continue;
- }
- const type = fields[0];
- const extensions = fields.slice(1);
- this.extensions.set(type, extensions[0]);
- for (const extension of extensions) {
- this.types.set(extension, type);
- }
- }
- }
- }
- /**
- * Возвращает MIME type для файла.
- * @param {string} file Имя файла.
- * @return {string}
- */
- guess(file) {
- // const matches = /\.([^.\/?]+)(?=$|\?)/.exec(url);
- // if (matches) {
- // const type = this.types.get(matches[1].toLowerCase());
- // if (type) {
- // return type;
- // }
- // }
- const extension = path.extname();
- return MIMETYPE_OCTEAT_STREAM;
- }
- /**
- * Возвращает расширение файла для определенного типа.
- * @param {string} type MIME-Type.
- * @return {string}
- */
- extension(type) {
- // const extension = this.extensions.get(
- // TeXt/HTML ;charset=utf-8 → text/html
- // type.replace(/^\s*|\s*(?:;.*)?$/g, '').toLowerCase()
- // );
- const extension = this.extensions.get(type);
- // Добавляем точку к расширению
- return extension ? '.' + extension : '';
- }
- }
- module.exports = new MimeTypes(path.join(__dirname, 'mime.types'));
- // mime = require('D:\\Projects\\NodeReverseProxy\\mimetypes');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement