Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
}
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
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);
}
});
}
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
function getHeaders(path) {
return '{"Content-Type": "' + mime.lookup(path) + '"}';
};
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;
}
});
function IsImage (url) {
return url && mime.lookup(url) && mime.lookup(url).slice(0, 6) === 'image/';
}
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;
.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)
})
}
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();
});
};