btrz-liquid-templates 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "btrz-liquid-templates",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Extension and helpers for liquid templates to deal with our data and other things we need to encapsulate",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/httpimg.js CHANGED
@@ -1,13 +1,5 @@
1
1
  const axios = require("axios");
2
2
 
3
- function getPrefix(url) {
4
- const extention = url.split(".").pop();
5
- if (extention.toLowerCase() === "png") {
6
- return "data:image/png;base64,";
7
- }
8
- return "data:image/jpeg;base64,";
9
- }
10
-
11
3
  function HttpImg(engine) {
12
4
  this.registerTag("httpImg", {
13
5
  parse: function(tagToken, remainTokens) {
@@ -19,7 +11,16 @@ function HttpImg(engine) {
19
11
  const response = await axios.get(url, {
20
12
  responseType: "arraybuffer"
21
13
  });
22
- return `${getPrefix(url)}${Buffer.from(response.data, "binary").toString("base64")}`;
14
+ if (response && response.headers && response.headers["content-type"]) {
15
+ const contentType = response.headers["content-type"];
16
+ if (contentType.startsWith("image/png")) {
17
+ return `data:image/png;base64,${Buffer.from(response.data, "binary").toString("base64")}`;
18
+ } else if (contentType.startsWith("image/jpeg")) {
19
+ return `data:image/jpeg;base64,${Buffer.from(response.data, "binary").toString("base64")}`;
20
+ }
21
+ }
22
+ //default to a placeholder image if the content type is not recognized
23
+ return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mP8P4PhPwMVAeOogaMGjho4auCogSPVQACqOzPNQ/wwqAAAAABJRU5ErkJggg==';
23
24
  } catch (e) {
24
25
  console.log(e);
25
26
  return "";
@@ -198,6 +198,26 @@ and some more here`,
198
198
  });
199
199
  });
200
200
 
201
+
202
+ it("should not load non image urls", async () => {
203
+ const tpl = require("../src/index");
204
+ const template = `{
205
+ "content": [
206
+ {
207
+ "image" : "{% httpImg 'https://www.google.com' %}"
208
+ }
209
+ ]
210
+ }`;
211
+ const documentDefinition = await tpl.processToObj(template, data);
212
+ expect(documentDefinition).to.be.eql({
213
+ "content": [
214
+ {
215
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mP8P4PhPwMVAeOogaMGjho4auCogSPVQACqOzPNQ/wwqAAAAABJRU5ErkJggg=="
216
+ }
217
+ ]
218
+ });
219
+ });
220
+
201
221
  it("should apply tag after translations", async () => {
202
222
  const tpl = require("../src/index");
203
223
  const template = `{