alm
Version:
The best IDE for TypeScript
36 lines (35 loc) • 1.23 kB
JavaScript
;
/**
* This is a custom middleware that serves raw image files to the client
*/
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var utils = require("../common/utils");
/** Code */
function getRawFile(req, res) {
/**
* The URL is just the filePath
* Client requests `/images/{filePath}`
* MAC : `/users/bas/foo` -> all good
* Windows : `/d:/users/bas/foo` -> don't want that leading `/` (I think express adds it?)
*/
var filePath = req.url;
if (/^win/.test(process.platform)) {
filePath = filePath.substring(1);
}
// filePath = __dirname + "/../../resources/icon.png"; // DEBUG
/** For non image files error out */
if (!utils.isImage(filePath)) {
res.writeHead(404, { 'content-type': 'text/plain' });
res.write("Error 404: Resource not found.");
res.end();
return;
}
res.writeHead(200, { 'content-type': utils.getImageMimeType(filePath) });
fs.createReadStream(filePath).pipe(res);
}
exports.getRawFile = getRawFile;
function registerImgServerWithExpress(app) {
app.use(utils.imageUrl, getRawFile);
}
exports.registerImgServerWithExpress = registerImgServerWithExpress;