moved these to dedicated functions

pull/20/head
tycrek 3 years ago
parent edf2ffbfab
commit 16f696baa2
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -11,6 +11,14 @@ const { HTTP, HTTPS, KILOBYTES } = require('./MagicNumbers.json');
const path = (...paths) => Path.join(__dirname, ...paths);
function getTrueHttp() {
return ('http').concat(useSsl ? 's' : '').concat('://');
}
function getTrueDomain(d = domain) {
return d.concat((port === HTTP || port === HTTPS || isProxied) ? '' : `:${port}`);
}
function getSafeExt(type) {
return type.includes('video') ? '.mp4' : type.includes('gif') ? '.gif' : '';
}
@ -19,6 +27,14 @@ function getS3url(s3key, type) {
return `https://${s3bucket}.${s3endpoint}/${s3key}${getSafeExt(type)}`;
}
function randomHexColour() { // From: https://www.geeksforgeeks.org/javascript-generate-random-hex-codes-color/
const letters = "0123456789ABCDEF";
let colour = '#';
for (let i = 0; i < 6; i++) // skipcq: JS-0074
colour += letters[(Math.floor(Math.random() * letters.length))];
return colour;
}
const idModes = {
zws: 'zws', // Zero-width spaces (see: https://zws.im/)
og: 'original', // Use original uploaded filename
@ -33,11 +49,12 @@ GENERATORS.set(idModes.gfy, gfyGen);
module.exports = {
path,
getTrueHttp,
getTrueDomain,
randomHexColour,
log: console.log,
saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }),
verify: (req, users) => req.headers.authorization && Object.prototype.hasOwnProperty.call(users, req.headers.authorization),
getTrueHttp: () => ('http').concat(useSsl ? 's' : '').concat('://'),
getTrueDomain: (d = domain) => d.concat((port === HTTP || port === HTTPS || isProxied) ? '' : `:${port}`),
renameFile: (req, newName) => new Promise((resolve, reject) => {
try {
const paths = [req.file.destination, newName];
@ -56,13 +73,6 @@ module.exports = {
const i = Math.floor(Math.log(bytes) / Math.log(KILOBYTES));
return parseFloat((bytes / Math.pow(KILOBYTES, i)).toFixed(decimals < 0 ? 0 : decimals)).toString().concat(` ${sizes[i]}`);
},
randomHexColour: () => { // From: https://www.geeksforgeeks.org/javascript-generate-random-hex-codes-color/
const letters = "0123456789ABCDEF";
let colour = '#';
for (let i = 0; i < 6; i++) // skipcq: JS-0074
colour += letters[(Math.floor(Math.random() * letters.length))];
return colour;
},
arrayEquals: (arr1, arr2) => arr1.length === arr2.length && arr1.slice().sort().every((value, index) => value === arr2.slice().sort()[index]),
downloadTempS3: (file) => new Promise((resolve, reject) =>
fetch(getS3url(file.randomId, file.mimetype))

Loading…
Cancel
Save