Skip to content

Commit b12d23b

Browse files
committed
Now setting content-type for assets served in memory for capture.
1 parent 4eeff9a commit b12d23b

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

package-lock.json

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "capture-template",
3-
"version": "1.1.15",
3+
"version": "1.1.16",
44
"description": "This library is responsible for expanding a template web page and then capturing it PNG or PDF.",
55
"main": "build/index.js",
66
"types": "build/index.d.ts",
@@ -39,6 +39,7 @@
3939
"express": "^4.16.4",
4040
"fs-extra": "^7.0.1",
4141
"inflate-template": "^1.1.8",
42+
"mime-types": "^2.1.26",
4243
"nightmare": "^3.0.1",
4344
"promisify-any": "^2.0.1",
4445
"yargs": "^12.0.5"
@@ -47,6 +48,7 @@
4748
"@types/chai": "4.0.4",
4849
"@types/express": "^4.16.1",
4950
"@types/fs-extra": "^5.0.5",
51+
"@types/mime-types": "^2.1.0",
5052
"@types/mocha": "2.2.43",
5153
"@types/node": "8.0.28",
5254
"@types/yargs": "^11.1.2",

src/web-server.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as http from 'http';
33
import * as path from "path";
44
import { ITemplate } from 'inflate-template';
55
import { ILog } from "./index";
6+
import * as mime from "mime-types";
67

78
/**
89
* Defines an in-memory file served by the web server.
@@ -96,20 +97,6 @@ export class WebServer implements IWebServer {
9697
return "http://127.0.0.1:" + this.assignedPortNo;
9798
}
9899

99-
//
100-
// Load a file from the template, caching it as necesary.
101-
//
102-
private async loadTemplateFile(template: ITemplate, url: string): Promise<Buffer> {
103-
const fileSystemPath = path.join(...url.split('/'));
104-
const templateFile = template.find(fileSystemPath);
105-
if (!templateFile) {
106-
throw new Error("Couldn't find file '" + url + "' in template.");
107-
}
108-
109-
const expandedFileContent = await templateFile.expand();
110-
return expandedFileContent;
111-
}
112-
113100
/**
114101
* Start the web-server.
115102
*/
@@ -121,7 +108,19 @@ export class WebServer implements IWebServer {
121108
app.use("/", async (request, response, next) => {
122109
try {
123110
const fileName = request.url === "/" ? "index.html" : request.url;
124-
response.send(await this.loadTemplateFile(template, fileName));
111+
const fileSystemPath = path.join(...fileName.split('/'));
112+
const templateFile = template.find(fileSystemPath);
113+
if (!templateFile) {
114+
throw new Error("Couldn't find file '" + fileSystemPath + "' in template.");
115+
}
116+
117+
const fileContent = await templateFile.expand();
118+
const fileExt = path.extname(fileName);
119+
const mimeType = mime.lookup(fileExt);
120+
if (mimeType) {
121+
response.setHeader("content-type", mimeType);
122+
}
123+
response.send(fileContent);
125124
}
126125
catch (err) {
127126
this.error("Error loading template file.");

0 commit comments

Comments
 (0)