SHOW:
|
|
- or go back to the newest paste.
| 1 | // var mimetypes = require('path/to/mimetypes');
| |
| 2 | // mimetypes.guess('path/to/file.txt');
| |
| 3 | // mimetypes.extension('application/javascript');
| |
| 4 | 'use strict'; | |
| 5 | ||
| 6 | const fs = require('fs');
| |
| 7 | const path = require('path');
| |
| 8 | ||
| 9 | const MIMETYPE_OCTEAT_STREAM = 'application/octeat-stream'; | |
| 10 | ||
| 11 | ||
| 12 | class MimeTypes {
| |
| 13 | constructor(file) {
| |
| 14 | this.types = new Map; | |
| 15 | this.extensions = new Map; | |
| 16 | this.load(file); | |
| 17 | - | // if (!fs.existsSync(file)) {
|
| 17 | + | |
| 18 | - | // console.warn(`File does not exists: ${file}`);
|
| 18 | + | |
| 19 | - | // return; |
| 19 | + | |
| 20 | - | // } |
| 20 | + | const content = fs.readFileSync(file, 'ascii'); |
| 21 | - | const data = fs.readFileSync(file, 'ascii'); |
| 21 | + | const lines = content.split('\n');
|
| 22 | - | const lines = data.split('\n');
|
| 22 | + | |
| 23 | // Удаляем комментарий | |
| 24 | line = line.replace(/#.*/, '').trim(); | |
| 25 | - | if (line) {
|
| 25 | + | // Пустая строка |
| 26 | - | const fields = line.split(/\s+/); |
| 26 | + | if (!line) {
|
| 27 | - | // const type = fields.shift().toLowerCase(); |
| 27 | + | continue; |
| 28 | - | // const extensions = fields.map( |
| 28 | + | } |
| 29 | - | // Function.prototype.call, |
| 29 | + | const fields = line.split(/\s+/); |
| 30 | - | // String.prototype.toLowerCase |
| 30 | + | // Не указаны расширения |
| 31 | - | // ); |
| 31 | + | if (fields.length < 2) {
|
| 32 | - | // Указан MIME type, но не укзаны расширения |
| 32 | + | continue; |
| 33 | - | if (1 > fields.length) {
|
| 33 | + | } |
| 34 | - | continue; |
| 34 | + | const type = fields[0]; |
| 35 | - | } |
| 35 | + | const extensions = fields.slice(1); |
| 36 | - | const type = fields[0]; |
| 36 | + | this.extensions.set(type, extensions[0]); |
| 37 | - | const extensions = fields.slice(1); |
| 37 | + | for (const extension of extensions) {
|
| 38 | - | this.extensions.set(type, extensions[0]); |
| 38 | + | this.types.set(extension, type); |
| 39 | - | for (const extension of extensions) {
|
| 39 | + | } |
| 40 | - | this.types.set(extension, type); |
| 40 | + | |
| 41 | - | } |
| 41 | + | |
| 42 | - | } |
| 42 | + | |
| 43 | /** | |
| 44 | * Возвращает MIME type для файла. | |
| 45 | * @param {string} file Имя файла.
| |
| 46 | * @return {string}
| |
| 47 | */ | |
| 48 | guess(file) {
| |
| 49 | const type = this.types.get(path.extname(file).slice(1)); | |
| 50 | if (type) {
| |
| 51 | return type; | |
| 52 | - | // const matches = /\.([^.\/?]+)(?=$|\?)/.exec(url); |
| 52 | + | |
| 53 | - | // if (matches) {
|
| 53 | + | |
| 54 | - | // const type = this.types.get(matches[1].toLowerCase()); |
| 54 | + | |
| 55 | - | // if (type) {
|
| 55 | + | |
| 56 | - | // return type; |
| 56 | + | |
| 57 | - | // } |
| 57 | + | |
| 58 | - | // } |
| 58 | + | |
| 59 | - | const extension = path.extname(); |
| 59 | + | |
| 60 | */ | |
| 61 | extension(type) {
| |
| 62 | const extension = this.extensions.get(type); | |
| 63 | // Добавляем точку к расширению | |
| 64 | return extension ? '.' + extension : ''; | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | ||
| 69 | - | // const extension = this.extensions.get( |
| 69 | + | module.exports = new MimeTypes(path.join(__dirname, 'mime.types')); |