How to use the mime.lookup function in mime

To help you get started, we’ve selected a few mime 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 tparisi / Skybox / service / node_modules / express / node_modules / connect / lib / middleware / static.js View on Github external
fs.stat(path, function(err, stat){
    // mime type
    type = mime.lookup(path);

    // ignore ENOENT
    if (err) {
      if (fn) return fn(err);
      return 'ENOENT' == err.code
        ? next()
        : next(err);
    // redirect directory in case index.html is present
    } else if (stat.isDirectory()) {
      if (!redirect) return next();
      res.statusCode = 301;
      res.setHeader('Location', url.pathname + '/');
      res.end('Redirecting to ' + url.pathname + '/');
      return;
    }
github sballesteros / dcat / index.js View on Github external
}

          if (this.packager.isClassOrSubClassOf(r['@type'], 'Dataset')) {
            //TODO about
            r.distribution = _.extend({'@type': 'DataDownload'}, encoding);
          } else if (this.packager.isClassOrSubClassOf(r['@type'], 'Code')) {
            //try to guess programming language
            if (stats.isDirectory()) {
              var langs = [];
              for (var i=0; i
github asciidisco / serveit2 / index.js View on Github external
options.push[requestedFile].forEach(function(file) {
        var stream = res.push(file, {
          request: { accept: '*/*'},
          response: {'content-type': mime.lookup(file)},
        });
        stream.on('error', console.error);

        try {
          var pushable = fs.readFileSync(root + file);
          stream.end(pushable);
          if (!options.quiet) console.log('PUSH', '/' + file, '-', pushable.length);
        } catch (e) {
          console.error('Error pushing file:', root + file, e);
        }
      });
    }
github brrd / Abricotine / app / js / init.js View on Github external
function isTextFile (path) {
            try {
                var fileStat = fs.statSync(path);
                if (fileStat.isFile()) {
                    var mimetype = mime.lookup(path);
                    return (mimetype && mimetype.substr(0,4) === 'text');
                }
                return false;
            }
            catch(err) {
                console.log(err);
                return false;
            }
        }
        for (var i=0; i
github jessica-taylor / hashlattice / lib / yamlinterface.js View on Github external
function getHeaders(path) {
  return '{"Content-Type": "' + mime.lookup(path) + '"}';
};
github Juniper / contrail-web-core / src / serverroot / utils / common.utils.js View on Github external
fs.stat(file, function (err, stats) {
        if(err) {
            handleError('sendFile', res, err);
            return;
        }
        else {
            var filename = path.basename(file);
            var mimetype = mime.lookup(file);
            res.setHeader('Content-type', mimetype);
            res.setHeader('Content-disposition', 'attachment; filename=' + filename);
            var stream = fs.createReadStream(file);
            stream.on('error', function (err) {
                res.statusCode = global.HTTP_STATUS_INTERNAL_ERROR;
                res.end(String(err));
            });
            stream.pipe(res);
            stream.on('end', function() {
                var cmd = 'rm -f ' + file;
                executeShellCommand(cmd,function(err, stdout, stderr) {
                    if(err) {
                        handleError('sendFile', res, err);
                        return;
                    }
                });
github davidmerfield / Blot / app / build / plugins / autoImage / index.js View on Github external
function IsImage (url) {
  return url && mime.lookup(url) && mime.lookup(url).slice(0, 6) === 'image/';
}
github apache / cordova-browser / bin / templates / project / cordova / lib / run.js View on Github external
response.write('<title>Directory listing of '+ urlPath + '</title>');
                response.write('<h3>Items in this directory</h3>');
                var items = fs.readdirSync(filePath);
                response.write('<ul>');
                for (var i in items) {
                    var file = items[i];
                    if (file) {
                        response.write('<li><a href="'+file+'">'+file+'</a></li>\n');
                    }
                }
                response.write('</ul>');
                response.end();
            } else if (!isFileChanged(filePath)) {
                do304();
            } else {
                var mimeType = mime.lookup(filePath);
                var respHeaders = {
                    'Content-Type': mimeType
                };
                var readStream = fs.createReadStream(filePath);

                var acceptEncoding = request.headers['accept-encoding'] || '';
                if (acceptEncoding.match(/\bgzip\b/)) {
                    respHeaders['content-encoding'] = 'gzip';
                    readStream = readStream.pipe(zlib.createGzip());
                } else if (acceptEncoding.match(/\bdeflate\b/)) {
                    respHeaders['content-encoding'] = 'deflate';
                    readStream = readStream.pipe(zlib.createDeflate());
                }

                var mtime = new Date(fs.statSync(filePath).mtime).toUTCString();
                respHeaders['Last-Modified'] = mtime;
github vizorvr / patches / lib / gridfs-storage.js View on Github external
.then(function(stat) {
		var opts = { filename: path, mode: 'w' }
		if (mimetype)
			opts.content_type = mimetype
		else
			opts.content_type = mime.lookup(path)

		if (stat)
			opts._id = stat._id

		return that._gfs.createWriteStream(opts)
	})
}
github pubnub / pubnub-api / games / ImpactJS / example-game / server.js View on Github external
fs.readFile(__dirname + path, function(err, data){
      if (err) {
      return send_404(res);
      }
      var content_type = mime.lookup(path);

      res.writeHead(200,{ 'Content-Type': content_type });
      res.write(data, 'utf8');
      res.end();
      });
};