From c3b36b55a3de653797d0becc0c2398a8677a6663 Mon Sep 17 00:00:00 2001 From: Glenn de Haan Date: Sun, 13 Oct 2024 10:37:26 +0200 Subject: [PATCH] Implemented the print.json translation file. Added a new print dialog. Moved /print function to a post request. Updated translation.js debug output. Implemented translator within print.js. Fixed typos. Removed unused utils from email and print components render function --- locales/en/print.json | 15 ++++++++ modules/print.js | 65 ++++++++++++++++++++--------------- modules/translation.js | 2 +- server.js | 30 +++++++++++++--- template/components/email.ejs | 2 +- template/components/print.ejs | 44 ++++++++++++++++++++++++ template/voucher.ejs | 30 +++++++++++++--- 7 files changed, 150 insertions(+), 38 deletions(-) create mode 100644 locales/en/print.json create mode 100644 template/components/print.ejs diff --git a/locales/en/print.json b/locales/en/print.json new file mode 100644 index 0000000..37bee9f --- /dev/null +++ b/locales/en/print.json @@ -0,0 +1,15 @@ +{ + "title": "WiFi Voucher Code", + "connect": "Connect to", + "password": "Password", + "or": "or", + "scan": "Scan to connect", + "details": "Voucher Details", + "type": "Type", + "multiUse": "Multi-use", + "singleUse": "Single-use", + "duration": "Duration", + "dataLimit": "Data Limit", + "downloadLimit": "Download Limit", + "uploadLimit": "Upload Limit" +} diff --git a/modules/print.js b/modules/print.js index e518688..fee79f0 100644 --- a/modules/print.js +++ b/modules/print.js @@ -11,6 +11,7 @@ const PrinterTypes = require('node-thermal-printer').types; const variables = require('./variables'); const log = require('./log'); const qr = require('./qr'); +const translation = require('./translation'); /** * Import own utils @@ -27,10 +28,14 @@ module.exports = { * Generates a voucher as a PDF * * @param voucher + * @param language * @return {Promise} */ - pdf: (voucher) => { + pdf: (voucher, language) => { return new Promise(async (resolve) => { + // Create new translator + const t = translation('print', language); + const doc = new PDFDocument({ bufferPages: true, size: [226.77165354330398, size(voucher)], @@ -54,7 +59,7 @@ module.exports = { doc.font('Helvetica-Bold') .fontSize(20) - .text(`WiFi Voucher Code`, { + .text(`${t('title')}`, { align: 'center' }); doc.font('Helvetica-Bold') @@ -68,7 +73,7 @@ module.exports = { if(variables.unifiSsid !== '') { doc.font('Helvetica') .fontSize(10) - .text(`Connect to: `, { + .text(`${t('connect')}: `, { continued: true }); doc.font('Helvetica-Bold') @@ -83,7 +88,7 @@ module.exports = { .text(`,`); doc.font('Helvetica') .fontSize(10) - .text(`Password: `, { + .text(`${t('password')}: `, { continued: true }); doc.font('Helvetica-Bold') @@ -93,16 +98,16 @@ module.exports = { }); doc.font('Helvetica') .fontSize(10) - .text(` or,`); + .text(` ${t('or')},`); } else { doc.font('Helvetica') .fontSize(10) - .text(` or,`); + .text(` ${t('or')},`); } doc.font('Helvetica') .fontSize(10) - .text(`Scan to connect:`); + .text(`${t('scan')}:`); doc.image(await qr(), 75, variables.unifiSsidPassword !== '' ? 215 : 205, {fit: [75, 75], align: 'center', valign: 'center'}); doc.moveDown(6); @@ -112,7 +117,7 @@ module.exports = { doc.font('Helvetica-Bold') .fontSize(12) - .text(`Voucher Details`); + .text(`${t('details')}`); doc.font('Helvetica-Bold') .fontSize(10) @@ -120,16 +125,16 @@ module.exports = { doc.font('Helvetica-Bold') .fontSize(10) - .text(`Type: `, { + .text(`${t('type')}: `, { continued: true }); doc.font('Helvetica') .fontSize(10) - .text(voucher.quota === 0 ? 'Multi-use' : 'Single-use'); + .text(voucher.quota === 0 ? t('multiUse') : t('singleUse')); doc.font('Helvetica-Bold') .fontSize(10) - .text(`Duration: `, { + .text(`${t('duration')}: `, { continued: true }); doc.font('Helvetica') @@ -139,7 +144,7 @@ module.exports = { if(voucher.qos_usage_quota) { doc.font('Helvetica-Bold') .fontSize(10) - .text(`Data Limit: `, { + .text(`${t('dataLimit')}: `, { continued: true }); doc.font('Helvetica') @@ -150,7 +155,7 @@ module.exports = { if(voucher.qos_rate_max_down) { doc.font('Helvetica-Bold') .fontSize(10) - .text(`Download Limit: `, { + .text(`${t('downloadLimit')}: `, { continued: true }); doc.font('Helvetica') @@ -161,7 +166,7 @@ module.exports = { if(voucher.qos_rate_max_up) { doc.font('Helvetica-Bold') .fontSize(10) - .text(`Upload Limit: `, { + .text(`${t('uploadLimit')}: `, { continued: true }); doc.font('Helvetica') @@ -177,10 +182,14 @@ module.exports = { * Sends a print job to an ESC/POS compatible network printer * * @param voucher + * @param language * @return {Promise} */ - escpos: (voucher) => { + escpos: (voucher, language) => { return new Promise(async (resolve, reject) => { + // Create new translator + const t = translation('print', language); + const printer = new ThermalPrinter({ type: PrinterTypes.EPSON, interface: `tcp://${variables.printerIp}` @@ -202,7 +211,7 @@ module.exports = { printer.alignCenter(); printer.newLine(); printer.setTextSize(2, 2); - printer.println('WiFi Voucher Code'); + printer.println(`${t('title')}`); printer.setTextSize(1, 1); printer.println(`${voucher.code.slice(0, 5)}-${voucher.code.slice(5)}`); printer.setTextNormal(); @@ -213,7 +222,7 @@ module.exports = { printer.newLine(); printer.alignLeft(); - printer.print('Connect to: '); + printer.print(`${t('connect')}: `); printer.setTypeFontB(); printer.setTextSize(1, 1); printer.print(variables.unifiSsid); @@ -221,18 +230,18 @@ module.exports = { if(variables.unifiSsidPassword) { printer.print(','); printer.newLine(); - printer.print('Password: '); + printer.print(`${t('password')}: `); printer.setTypeFontB(); printer.setTextSize(1, 1); printer.print(variables.unifiSsidPassword); printer.setTextNormal(); - printer.print(' or,'); + printer.print(` ${t('or')},`); printer.newLine(); } else { - printer.print(' or,'); + printer.print(` ${t('or')},`); printer.newLine(); } - printer.println('Scan to connect:'); + printer.println(`${t('scan')}:`); printer.alignCenter(); await printer.printImageBuffer(await qr(true)); } @@ -243,20 +252,20 @@ module.exports = { printer.alignLeft(); printer.setTypeFontB(); printer.setTextSize(1, 1); - printer.println('Voucher Details'); + printer.println(`${t('details')}`); printer.setTextNormal(); printer.drawLine(); printer.setTextDoubleHeight(); printer.invert(true); - printer.print('Type:'); + printer.print(`${t('type')}:`); printer.invert(false); - printer.print(voucher.quota === 0 ? ' Multi-use' : ' Single-use'); + printer.print(voucher.quota === 0 ? ` ${t('multiUse')}` : ` ${t('singleUse')}`); printer.newLine(); printer.setTextDoubleHeight(); printer.invert(true); - printer.print('Duration:'); + printer.print(`${t('duration')}:`); printer.invert(false); printer.print(` ${time(voucher.duration)}`); printer.newLine(); @@ -264,7 +273,7 @@ module.exports = { if(voucher.qos_usage_quota) { printer.setTextDoubleHeight(); printer.invert(true); - printer.print('Data Limit:'); + printer.print(`${t('dataLimit')}:`); printer.invert(false); printer.print(` ${bytes(voucher.qos_usage_quota, 2)}`); printer.newLine(); @@ -273,7 +282,7 @@ module.exports = { if(voucher.qos_rate_max_down) { printer.setTextDoubleHeight(); printer.invert(true); - printer.print('Download Limit:'); + printer.print(`${t('downloadLimit')}:`); printer.invert(false); printer.print(` ${bytes(voucher.qos_rate_max_down, 1, true)}`); printer.newLine(); @@ -282,7 +291,7 @@ module.exports = { if(voucher.qos_rate_max_up) { printer.setTextDoubleHeight(); printer.invert(true); - printer.print('Upload Limit:'); + printer.print(`${t('uploadLimit')}:`); printer.invert(false); printer.print(` ${bytes(voucher.qos_rate_max_up, 1, true)}`); printer.newLine(); diff --git a/modules/translation.js b/modules/translation.js index 9a65149..017abd6 100644 --- a/modules/translation.js +++ b/modules/translation.js @@ -43,6 +43,6 @@ module.exports = (module, language = 'en', fallback = 'en') => { } // Check if debugging is enabled. If enabled only return key - return variables.translationDebug ? `%${key}%` : translations[key]; + return variables.translationDebug ? `t('${key}')` : translations[key]; }; }; diff --git a/server.js b/server.js index c731b5c..da21e0d 100644 --- a/server.js +++ b/server.js @@ -257,9 +257,33 @@ if(variables.serviceWeb) { return e._id === req.params.id; }); + if(voucher) { + res.render('components/print', { + baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '', + languages, + voucher, + updated: cache.updated + }); + } else { + res.status(404); + res.render('404', { + baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '' + }); + } + }); + app.post('/voucher/:id/print', [authorization.web], async (req, res) => { + if(variables.printerType === '') { + res.status(501).send(); + return; + } + + const voucher = cache.vouchers.find((e) => { + return e._id === req.params.id; + }); + if(voucher) { if(variables.printerType === 'pdf') { - const buffers = await print.pdf(voucher); + const buffers = await print.pdf(voucher, req.body.language); const pdfData = Buffer.concat(buffers); res.writeHead(200, { 'Content-Length': Buffer.byteLength(pdfData), @@ -269,7 +293,7 @@ if(variables.serviceWeb) { } if(variables.printerType === 'escpos') { - const printResult = await print.escpos(voucher).catch((e) => { + const printResult = await print.escpos(voucher, req.body.language).catch((e) => { res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); }); @@ -297,8 +321,6 @@ if(variables.serviceWeb) { if(voucher) { res.render('components/email', { baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '', - timeConvert: time, - bytesConvert: bytes, languages, voucher, updated: cache.updated diff --git a/template/components/email.ejs b/template/components/email.ejs index 90a2575..dc029a0 100644 --- a/template/components/email.ejs +++ b/template/components/email.ejs @@ -5,7 +5,7 @@
-
+