diff --git a/src/constants.js b/src/constants.js index 2f0bc6f..28d0dc1 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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 = ( diff --git a/src/helpers/image.js b/src/helpers/image.js index 48ece44..b896bc1 100644 --- a/src/helpers/image.js +++ b/src/helpers/image.js @@ -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; }; @@ -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')}`; diff --git a/src/helpers/render.js b/src/helpers/render.js index 07f6417..7f905eb 100644 --- a/src/helpers/render.js +++ b/src/helpers/render.js @@ -4,16 +4,15 @@ 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) { @@ -21,7 +20,7 @@ if (!process.env.FONTS_LOADED) { path: fontPath, family } = font; - registerFont(path.join(INTERNAL_STATIC_DIR, fontPath), { family }); + registerFont(resolveItemPath(fontPath), { family }); } } process.env.FONTS_LOADED = 1; @@ -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) {} @@ -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 { diff --git a/src/helpers/resolution.js b/src/helpers/resolution.js new file mode 100644 index 0000000..3bc63ee --- /dev/null +++ b/src/helpers/resolution.js @@ -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 +}; \ No newline at end of file