How to use the mime-types.contentType function in mime-types

To help you get started, we’ve selected a few mime-types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AnyChart / AnyChart-NodeJS / lib / anychart-node.js View on Github external
for (var i = 0, len = resources.length; i < len; i++) {
    var resource = resources[i];
    var type = resource.type;

    if (type == mime.contentType('css')) {
      var style = document.createElement('style');
      style.innerHTML = resource.body;
      head.appendChild(style);

      //todo font loading
      // var font = new FontFaceObserver('Conv_interdex');
      // font.load().then(function () {
      //
      // });
    } else if (type == mime.contentType('js')) {
      scripts += ' ' + resource.body + ';';
    }
  }

  var err = null;
  try {
    var script = new vm.VM({
      timeout: 5000,
      sandbox: window
    });
    script.run(scripts);
  } catch (e) {
    console.log(e);
    err = e;
  }
github NuSkooler / enigma-bbs / core / file_area_web.js View on Github external
if(err) {
                    return this.fileNotFound(resp);
                }

                resp.on('close', () => {
                    //  connection closed *before* the response was fully sent
                    //  :TODO: Log and such
                });

                resp.on('finish', () => {
                    //  transfer completed fully
                    this.updateDownloadStatsForUserIdAndSystem(servedItem.userId, stats.size, [ fileEntry ]);
                });

                const headers = {
                    'Content-Type'          : mimeTypes.contentType(filePath) || mimeTypes.contentType('.bin'),
                    'Content-Length'        : stats.size,
                    'Content-Disposition'   : `attachment; filename="${fileEntry.fileName}"`,
                };

                const readStream = fs.createReadStream(filePath);
                resp.writeHead(200, headers);
                return readStream.pipe(resp);
            });
        });
github danielcardoso / html-pages / lib / server.js View on Github external
res.writeHead(301, {
        Location: newPath
      });

      res.end('Redirecting to ' + newPath);
      return;
    }

    const directoryIndexPath = path.join(related, '/' + flags.directoryIndex);

    // If exists open the directory index file
    if (flags.directoryIndex !== '' && fs.existsSync(directoryIndexPath)) {
      res.setHeader(
        'Content-Type',
        mime.contentType(path.extname(directoryIndexPath))
      );

      return httpRequest.stream({
        req,
        path: directoryIndexPath,
        streamOptions,
        res
      });
    }

    // If no listing is available return an error 403
    if (flags.noListing === true) {
      return httpRequest.micro({
        req,
        res,
        code: 403
github AnyChart / AnyChart-NodeJS / lib / anychart-node.js View on Github external
function applyResourcesToDoc(params, resources, callback) {
  var document = params.document;
  var head = document.getElementsByTagName('head')[0];
  var window = document.defaultView;
  var scripts = '';

  for (var i = 0, len = resources.length; i < len; i++) {
    var resource = resources[i];
    var type = resource.type;

    if (type == mime.contentType('css')) {
      var style = document.createElement('style');
      style.innerHTML = resource.body;
      head.appendChild(style);

      //todo font loading
      // var font = new FontFaceObserver('Conv_interdex');
      // font.load().then(function () {
      //
      // });
    } else if (type == mime.contentType('js')) {
      scripts += ' ' + resource.body + ';';
    }
  }

  var err = null;
  try {
github cerner / terra-toolkit / src / wdio / services / ExpressDevServerService.js View on Github external
app.get('*', (req, res, next) => {
        let filename = req.url;
        // Setup a default index for the server.
        if (filename === '/') {
          filename = `/${index}`;
        }

        const filepath = `${webpackConfig.output.path}${filename}`;

        if (fs.existsSync(filepath)) {
          res.setHeader('content-type', mime.contentType(path.extname(filename)));
          res.send(fs.readFileSync(filepath));
        } else if (filename === '/favicon.ico') {
          res.sendStatus(200);
        } else {
          next();
        }
      });
github jMavarez / Tana / src / js / renderer / portal.js View on Github external
function setupText() {
  const title = getFileName(state.payload);
  $title.text(title);
  document.title = title;

  const $code = $('#text > code');

  const fileType = mime.contentType(state.payload);
  const fileExtension = mime.extension(fileType);

  fs.readFile(state.payload, 'utf8', (err, data) => {
    if (err) console.error(err);

    const clazz = (fileExtension) ? `lang-${fileExtension}` : '';
    $code.addClass(clazz);
    $code.el().innerHTML = data;
    $text.addClass('show');

    hideLoader();

    Highlightjs.initHighlighting();
  });
}
github rethinkdb / horizon / cli / src / serve.js View on Github external
fs.readFile(filePath, 'binary', (err2, file) => {
            if (err2) {
              res.writeHead(500, { 'Content-Type': 'text/plain' });
              res.end(`${err2}\n`);
            } else {
              const type = get_type(path.extname(filePath)) || false;
              if (type) {
                res.writeHead(200, { 'Content-Type': type });
              } else {
                res.writeHead(200);
              }
              res.end(file, 'binary');
            }
          });
        } else if (stats.isDirectory()) {
github polixjs / polix / extends / response.js View on Github external
set type(type) {
    type = getType(type);
    if (type) {
      this.set('Content-Type', type);
    } else {
      this.remove('Content-Type');
    }
  },
github brocessing / copilote / scripts / utils / templateMiddleware.js View on Github external
.then(rendered => {
      const contentType = mime.contentType(path.extname(page.output))
      middleware.res.setHeader('Content-Type', contentType)
      middleware.res.end(rendered)
    })
    .catch(err => gracefulError(middleware.res, err))
github release-it / release-it / lib / plugin / github / GitHub.js View on Github external
uploadAsset(filePath) {
    const url = this.uploadUrl;
    const name = path.basename(filePath);
    const contentType = mime.contentType(name) || 'application/octet-stream';
    const contentLength = fs.statSync(filePath).size;

    return this.retry(async bail => {
      try {
        const options = {
          url,
          file: fs.createReadStream(filePath),
          name,
          headers: {
            'content-type': contentType,
            'content-length': contentLength
          }
        };
        this.debug(options);
        const response = await this.client.repos.uploadReleaseAsset(options);
        this.debug(response.data);