Skip to content

Commit

Permalink
Use separate helper for all path resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
paramsiddharth committed Jul 17, 2021
1 parent c6047e0 commit 8d3bb54
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');

const INTERNAL_STATIC_DIR = process.env.INTERNAL_STATIC_DIR ?? path.resolve(__dirname, '..', 'static');
const INTERNAL_STATIC_DIR = process.env.INTERNAL_STATIC_DIR ?? path.resolve(__dirname, '..', 'static') + path.sep;

// Reducing to consume less memory, exceeding it will cause the rendered output to be scaled down
const MAX_CAIRO_DIMENSION = (
Expand Down
17 changes: 5 additions & 12 deletions src/helpers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ const {
lookup: mimeType
} = require('mime-types');

const {
INTERNAL_STATIC_DIR
} = require('../constants');
const { resolveItemPath } = require('./resolution');
const { INTERNAL_STATIC_DIR } = require('../constants');

// Test if image is valid
const validateImage = async src => {
if (!/^(https?\:\/\/|data\:)/.test(src)) {
try {
const location = path.join(INTERNAL_STATIC_DIR, src);
await fs.stat(location);
} catch(e) {
return false;
}
}
if (!/^(https?\:\/\/|data\:)/.test(src))
return resolveItemPath(src);
return true;
};

Expand Down Expand Up @@ -80,7 +73,7 @@ const getImageLocation = async src => {
};

const imgToBase64 = async name => {
const buf = await fs.readFile(path.join(INTERNAL_STATIC_DIR, name));
const buf = await fs.readFile(resolveItemPath(name));
const extension = path.extname(name);
const contentType = mimeType(extension);
const dataURI = `data:${contentType};base64,${buf.toString('base64')}`;
Expand Down
15 changes: 7 additions & 8 deletions src/helpers/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ const { createCanvas, loadImage, registerFont } = require('canvas');
const strftime = require('strftime');

const {
getPlaceholder, isValidPlaceholder
} = require('../helpers/placeholder');
const {
INTERNAL_STATIC_DIR,
MAX_CAIRO_DIMENSION,
SINGLE_WHITE_PIXEL
} = require('../constants');

const { getPlaceholder } = require('./placeholder');
const { resolveItemPath } = require('./resolution');

if (!process.env.FONTS_LOADED) {
const RESOURCES = path.join(INTERNAL_STATIC_DIR, 'items.json');
const RESOURCES = resolveItemPath('items.json');
if (fs.existsSync(RESOURCES)) {
const fonts = fs.readJSONSync(RESOURCES).filter(i => i.type === 'font');
for (const font of fonts) {
const {
path: fontPath,
family
} = font;
registerFont(path.join(INTERNAL_STATIC_DIR, fontPath), { family });
registerFont(resolveItemPath(fontPath), { family });
}
}
process.env.FONTS_LOADED = 1;
Expand Down Expand Up @@ -89,7 +88,7 @@ const render = async (cert, fmt) => {
}

try {
const bgImg = await loadImage(path.join(INTERNAL_STATIC_DIR, cert.background));
const bgImg = await loadImage(resolveItemPath(cert.background));
ctx.drawImage(bgImg, 0, 0, width, height);
} catch(e) {}

Expand Down Expand Up @@ -134,7 +133,7 @@ const render = async (cert, fmt) => {
value = SINGLE_WHITE_PIXEL;
}

const toLoad = value.startsWith('data:') ? value : path.join(INTERNAL_STATIC_DIR, value);
const toLoad = value.startsWith('data:') ? value : resolveItemPath(value);
const imgToDraw = await loadImage(toLoad);
ctx.drawImage(imgToDraw, x, y, width, height);
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/resolution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');
const fs = require('fs-extra');

const { INTERNAL_STATIC_DIR } = require('../constants');

const resolveItemPath = src => {
for (const DIR of [INTERNAL_STATIC_DIR]) {
if (DIR == null)
continue;
const location = path.join(DIR, src);
if (fs.existsSync(location));
return location;
}

return false;
};

module.exports = {
resolveItemPath
};

0 comments on commit 8d3bb54

Please sign in to comment.