Skip to content

Commit

Permalink
Fixed SERVICE_WEB variable not working correctly. Implemented missing…
Browse files Browse the repository at this point in the history
… HA voucher_custom config setting. Fixed VOUCHER_CUSTOM variable not working correctly. Fixed an issue where HA voucher_types config was not used during validation causing an incorrect error. Added bytes conversion
  • Loading branch information
glenndehaan committed Apr 8, 2024
1 parent a80f533 commit 78c891c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
22 changes: 22 additions & 0 deletions modules/bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Converts bytes to a human readable format
*
* @param bytes
* @param type
* @param perSecond
* @param decimals
* @return {string}
*/
module.exports = (bytes, type = 0, perSecond = false, decimals = 2) => {
if (bytes === 0) return '0 Bytes';

const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'].toSpliced(0, type);

const i = Math.floor(Math.log(bytes) / Math.log(k));

const suffix = perSecond ? sizes[i] + 'ps' : sizes[i].toUpperCase();

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + suffix;
}
14 changes: 8 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const cache = require('./modules/cache');
const logo = require('./modules/logo');
const types = require('./modules/types');
const time = require('./modules/time');
const bytes = require('./modules/bytes');
const unifi = require('./modules/unifi');

/**
Expand All @@ -39,8 +40,8 @@ const app = express();
* Define global functions and variables
*/
const voucherTypes = types(config('voucher_types') || process.env.VOUCHER_TYPES || '480,1,,,;');
const voucherCustom = (process.env.VOUCHER_CUSTOM === 'true') || true;
const webService = (process.env.SERVICE_WEB === 'true') || true;
const voucherCustom = config('voucher_custom') !== null ? config('voucher_custom') : process.env.VOUCHER_CUSTOM ? process.env.VOUCHER_CUSTOM !== 'false' : true;
const webService = process.env.SERVICE_WEB ? process.env.SERVICE_WEB !== 'false' : true;
const apiService = (process.env.SERVICE_API === 'true') || false;
const authDisabled = (process.env.DISABLE_AUTH === 'true') || false;

Expand Down Expand Up @@ -184,7 +185,7 @@ if(webService) {
}

if(req.body['voucher-type'] !== 'custom') {
const typeCheck = (process.env.VOUCHER_TYPES || '480,1,,,;').split(';').includes(req.body['voucher-type']);
const typeCheck = (config('voucher_types') || process.env.VOUCHER_TYPES || '480,1,,,;').split(';').includes(req.body['voucher-type']);

if (!typeCheck) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Unknown Type!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
Expand Down Expand Up @@ -318,7 +319,7 @@ if(webService) {
});
doc.font('Helvetica')
.fontSize(10)
.text(`${voucher.qos_usage_quota}MB`);
.text(`${bytes(voucher.qos_usage_quota, 2)}`);
}

if(voucher.qos_rate_max_down) {
Expand All @@ -329,7 +330,7 @@ if(webService) {
});
doc.font('Helvetica')
.fontSize(10)
.text(`${voucher.qos_rate_max_down}kbps`);
.text(`${bytes(voucher.qos_rate_max_down, 1, true)}`);
}

if(voucher.qos_rate_max_up) {
Expand All @@ -340,7 +341,7 @@ if(webService) {
});
doc.font('Helvetica')
.fontSize(10)
.text(`${voucher.qos_rate_max_up}kbps`);
.text(`${bytes(voucher.qos_rate_max_up, 1, true)}`);
}

doc.end();
Expand Down Expand Up @@ -379,6 +380,7 @@ if(webService) {
error: req.flashMessage.type === 'error',
error_text: req.flashMessage.message || '',
timeConvert: time,
bytesConvert: bytes,
voucher_types: voucherTypes,
voucher_custom: voucherCustom,
vouchers: cache.vouchers,
Expand Down
6 changes: 3 additions & 3 deletions template/voucher.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@
<svg viewBox="0 0 2 2" class="h-0.5 w-0.5 flex-none fill-gray-500 dark:fill-gray-300">
<circle cx="1" cy="1" r="1" />
</svg>
<p class="truncate"><%= voucher.qos_usage_quota %>MB Data Limit</p>
<p class="truncate"><%= bytesConvert(voucher.qos_usage_quota, 2) %> Data Limit</p>
<% } %>
<% if(voucher.qos_rate_max_down) { %>
<svg viewBox="0 0 2 2" class="h-0.5 w-0.5 flex-none fill-gray-500 dark:fill-gray-300">
<circle cx="1" cy="1" r="1" />
</svg>
<p class="truncate"><%= voucher.qos_rate_max_down %>kbps Download Limit</p>
<p class="truncate"><%= bytesConvert(voucher.qos_rate_max_down, 1, true) %> Download Limit</p>
<% } %>
<% if(voucher.qos_rate_max_up) { %>
<svg viewBox="0 0 2 2" class="h-0.5 w-0.5 flex-none fill-gray-500 dark:fill-gray-300">
<circle cx="1" cy="1" r="1" />
</svg>
<p class="truncate"><%= voucher.qos_rate_max_up %>kbps Upload Limit</p>
<p class="truncate"><%= bytesConvert(voucher.qos_rate_max_up, 1, true) %> Upload Limit</p>
<% } %>
</div>
</div>
Expand Down

0 comments on commit 78c891c

Please sign in to comment.