Implemented remapping of expiration

This commit is contained in:
Glenn de Haan
2022-11-15 22:17:10 +01:00
parent 1dbbcd9e44
commit 998d9479c7
3 changed files with 38 additions and 4 deletions

20
modules/time.js Normal file
View File

@@ -0,0 +1,20 @@
/**
* Convert time minutes
*
* @param minutes
* @returns {string}
*/
module.exports = (minutes) => {
if (minutes < 60) {
return `${minutes} minute(s)`;
}
const hours = minutes / 60;
if (hours < 24) {
return `${hours % 1 === 0 ? hours : hours.toFixed(2)} ${(hours > 1) ? 'hours' : 'hour'}`;
}
const days = hours / 24;
return `${days} ${(days > 1) ? 'days' : 'day'}`;
}

View File

@@ -11,6 +11,7 @@ const app = express();
*/
const logo = require('./modules/logo');
const types = require('./modules/types');
const time = require('./modules/time');
const unifi = require('./modules/unifi');
/**
@@ -29,7 +30,7 @@ logo();
*/
console.log('[VoucherType] Loaded the following types:');
voucherTypes.forEach((type, key) => {
console.log(`[VoucherType][${key}] ${type.expiration} minutes, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
console.log(`[VoucherType][${key}] ${time(type.expiration)}, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
});
/**
@@ -79,6 +80,7 @@ app.get('/', (req, res) => {
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
app_header: timeHeader,
sid: uuidv4(),
timeConvert: time,
voucher_types: voucherTypes
});
});
@@ -88,13 +90,20 @@ app.post('/', async (req, res) => {
return;
}
const check = req.body.password === (process.env.SECURITY_CODE || "0000");
const passwordCheck = req.body.password === (process.env.SECURITY_CODE || "0000");
if(!check) {
if(!passwordCheck) {
res.redirect(encodeURI(`/?error=Invalid password!`));
return;
}
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']);
if(!typeCheck) {
res.redirect(encodeURI(`/?error=Unknown type!`));
return;
}
res.redirect(encodeURI(`/voucher?code=${req.body.password}&type=${req.body['voucher-type']}`));
});
app.get('/voucher', async (req, res) => {
@@ -103,6 +112,11 @@ app.get('/voucher', async (req, res) => {
return;
}
if(!(process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.query.type)) {
res.status(400).send();
return;
}
const hour = new Date().getHours();
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
const voucherCode = await unifi(types(req.query.type, true));

View File

@@ -56,7 +56,7 @@
<div class="relative inline-block w-full">
<select id="voucher-type" name="voucher-type" class="shadow appearance-none border rounded w-full py-2 px-3 pr-8 text-gray-700 mt-1 leading-tight focus:outline-none focus:shadow-outline dark:bg-neutral-800 dark:border-neutral-700 dark:text-gray-100" required>
<% voucher_types.forEach((type) => { %>
<option value="<%= type.raw %>"><%= type.expiration %> minutes, <%= type.usage === '1' ? 'single-use' : 'multi-use' %><%= typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : '' %><%= typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : '' %><%= typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : '' %><%= typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : '' %></option>
<option value="<%= type.raw %>"><%= timeConvert(type.expiration) %>, <%= type.usage === '1' ? 'single-use' : 'multi-use' %><%= typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : '' %><%= typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : '' %><%= typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : '' %><%= typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : '' %></option>
<% }); %>
</select>
<div class="absolute inset-y-0 right-0 flex items-center px-2 mt-1 pointer-events-none">