本文整理匯總了TypeScript中mime.lookup函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript lookup函數的具體用法?TypeScript lookup怎麽用?TypeScript lookup使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了lookup函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: sendFile
/**
* Sends the file in response
* @param Response response
*/
function sendFile(response: http.ServerResponse, request: http.ServerRequest)
{
try
{
let buf = fs.readFileSync(directoryPath);
let type = mime.lookup(directoryPath);
let fileExtension = directoryPath.substr(directoryPath.lastIndexOf('.')+1);
response.statusCode = 200;
response.setHeader('Content-Length', status["size"].toString());
response.setHeader('Content-Type', type);
response.setHeader('X-Content-Type-Options', 'nosniff');
response.setHeader('X-XSS-Protection', '1; mode=block');
response.setHeader('X-Frame-Options', 'SAMEORIGIN');
response.setHeader('Cache-Control', 'private, max-age=10800');
//Check for Gzip support
if (request.headers['accept-encoding'] && request.headers['accept-encoding'].match(/\bgzip\b/))
{
if (fileExtension === "css" || fileExtension === "js" || fileExtension === "html")
{
buf = encodeToGZIP(buf, response);
}
}
response.end(buf, 'binary');
}
catch (e)
{
ErrorLogger().Debug(e);
}
}
示例2: processRequest
function processRequest() {
try {
var stat = fs.statSync(filename);
if(!stat.isFile()) {
if (stat.isDirectory()) {
filename = pathJoin(filename, "index.html");
stat = fs.statSync(filename);
if(!stat.isFile()) throw "next";
} else {
throw "next";
}
}
} catch(e) {
return next();
}
// server content
var content = fs.readFileSync(filename);
content = handleRangeHeaders(content, req, res);
res.setHeader("Access-Control-Allow-Origin", "*"); // To support XHR, etc.
res.setHeader("Content-Type", mime.lookup(filename));
res.setHeader("Content-Length", content.length);
if(options.headers) {
for(var name in options.headers) {
res.setHeader(name, options.headers[name]);
}
}
if (res.send) res.send(content);
else res.end(content);
}
示例3: pushResources
export function pushResources(
options: ServerOptions, req: express.Request, res: Response) {
if (res.push && options.protocol === 'h2' && options.pushManifestPath &&
!req.get('x-is-push')) {
// TODO: Handle preload link headers
const pushManifest =
getPushManifest(options.root, options.pushManifestPath);
const resources = pushManifest[req.path];
if (resources) {
const root = options.root;
for (const filename of Object.keys(resources)) {
const stream =
res.push(filename, {
request: {accept: '*/*'},
response: {
'content-type': mime.lookup(filename),
// Add an X-header to the pushed request so we don't trigger
// pushes for pushes
'x-is-push': 'true'
}
})
.on('error',
(err: {}) =>
console.error('failed to push', filename, err));
fs.createReadStream(path.join(root, filename)).pipe(stream);
}
}
}
}
示例4: sendMission
function sendMission(request, response, missionId: number, missionZip: string, downloadMissionName: string) {
var missionWorkingDir = `${Settings.PATH.Mission.WORKING_DIR}/${missionId}`;
var missionZipPath = `${missionWorkingDir}/${missionZip}`;
response.setHeader('Content-disposition', `attachment; filename=${downloadMissionName}.zip`);
response.setHeader('Content-type', mime.lookup(missionZipPath));
response.sendFile(missionZipPath, { root: './' }, () => removeMissionWorkingDir(missionWorkingDir) );
}
示例5: uploadBlob
async function uploadBlob(blobService: azure.BlobService, quality: string, blobName: string, file: string): Promise<void> {
const blobOptions = {
contentType: mime.lookup(file),
cacheControl: 'max-age=31536000, public'
};
await new Promise((c, e) => blobService.createBlockBlobFromLocalFile(quality, blobName, file, blobOptions, err => err ? e(err) : c()));
}
示例6: readWithFallback
private async readWithFallback(filepath, extensions): Promise<{Body: Buffer, ContentType: string}> {
for (let ext of extensions) {
try {
var res = await readFile(filepath + ext);
return {Body: res, ContentType: mime.lookup(filepath + ext)};
} catch (err) {}
}
throw new Error(filepath + ' not found');
}
示例7: function
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
response.writeHead(404, { 'Content-Type': 'text/html' });
} else {
response.writeHead(200, { 'Content-Type': mime.lookup(pathname) });
response.end(data, 'binary')
}
});
示例8: function
module.exports.download = function(req, res) {
var id = req.param('id');
var name = req.param('name');
var file = req.param('file');
var path = KDataBase + '/' + name + '/' + file;
console.log('download: ' + path);
//var SkipperDisk = require('skipper-disk');
//var fileAdapter = SkipperDisk(/* optional opts */);
//fileAdapter.read(path).on('error', function (err) {
// return res.serverError(err);
//}).pipe(res);
var mimetype = mime.lookup(path);
res.setHeader('Content-disposition', 'attachment; filename=' + file);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(path);
filestream.pipe(res)
};
示例9: put
async put(path, obj, contentType): Promise<void> {
var params = {
Bucket: this.bucket,
Key: normalizePath(path),
Body: obj,
ContentType: contentType !== undefined ? contentType : mime.lookup(path)
};
await this.putObject(params);
debug('put ' + params.Key);
// S3 doesn't infer '.html' on filenames,
// so we have to put both 'path' and 'path.html'
if (params.ContentType === 'text/html' && !/\.html$/.test(params.Key)) {
params.Key = params.Key + '.html';
await this.putObject(params);
debug('put ' + params.Key);
}
}
示例10: viewStatic
export function viewStatic( filename: string, headers: relaxjs.ResponseHeaders = {} ) : Q.Promise< relaxjs.Embodiment > {
var fname = '[view static]';
var log = _log.child( { func: 'internals.viewStatic'} );
var mtype = mime.lookup(filename);
var laterAction = Q.defer< relaxjs.Embodiment >();
var staticFile = '.' + filename;
log.info('serving %s',staticFile);
if ( !fs.existsSync(staticFile) ) {
log.warn('File "%s" not found',staticFile);
laterAction.reject( new relaxjs.RxError(`File ${filename} not found`, 'File Not Found', 404 ) );
}
else {
fs.stat( staticFile , ( err, stats: fs.Stats ) => {
log.info(`Sreaming ${staticFile}`);
headers['content-length'] = stats.size.toString();
var readStream = fs.createReadStream( staticFile );
var reply = new relaxjs.Embodiment( mtype, 200, readStream );
reply.additionalHeaders = headers;
laterAction.resolve( reply );
});
}
/*
fs.readFile( staticFile, function( err : Error, content : Buffer ) {
if ( err ) {
log.warn('%s file "%s" not found',fname,staticFile);
laterAction.reject( new relaxjs.RxError( filename + ' not found', 'File Not Found', 404 ) );
}
else {
var reply = new relaxjs.Embodiment( mtype, 200, content );
reply.additionalHeaders = headers;
laterAction.resolve( reply );
}
});
*/
return laterAction.promise;
}