All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
146 lines
4.9 KiB
TypeScript
146 lines
4.9 KiB
TypeScript
import express, { NextFunction } from "express";
|
|
import { getLogin } from "../auth";
|
|
import { parseToken } from "../utils";
|
|
import path from "path";
|
|
import fs from "fs";
|
|
import { EasterEgg } from "../../../types/gen/types.gen";
|
|
|
|
const EASTER_EGGS_JSON_PATH = path.join(__dirname, "../../.easter-eggs.json");
|
|
const IMAGES_PATH = '../../resources/easterEggs';
|
|
|
|
type EasterEggsJson = {
|
|
[key: string]: EasterEgg[]
|
|
}
|
|
|
|
function generateUrl() {
|
|
let result = '';
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
const charactersLength = characters.length;
|
|
let counter = 0;
|
|
while (counter < 32) {
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
counter += 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Vrátí náhodně jeden z definovaných easter egg obrázků pro přihlášeného uživatele.
|
|
*
|
|
* @param req request
|
|
* @param res response
|
|
* @param next next
|
|
* @returns náhodný easter egg obrázek, nebo 404 pokud žádný není definován
|
|
*/
|
|
function getEasterEggImage(req: any, res: any, next: NextFunction) {
|
|
const login = getLogin(parseToken(req));
|
|
try {
|
|
if (login in easterEggs) {
|
|
const imagePath = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)].path;
|
|
res.sendFile(path.join(__dirname, IMAGES_PATH, imagePath));
|
|
return;
|
|
}
|
|
res.sendStatus(404);
|
|
} catch (e: any) { next(e) }
|
|
}
|
|
|
|
function getRandomPosition(startOffset: number, endOffset: number) {
|
|
const choice = Math.floor(Math.random() * 4);
|
|
|
|
if (choice === 0) {
|
|
// Vlevo nahoře
|
|
return {
|
|
left: `${startOffset}px`,
|
|
startLeft: `${startOffset}px`,
|
|
"--start-left": `${startOffset}px`,
|
|
top: `${startOffset}px`,
|
|
startTop: `${startOffset}px`,
|
|
"--start-top": `${startOffset}px`,
|
|
endLeft: `${endOffset}px`,
|
|
"--end-left": `${endOffset}px`,
|
|
endTop: `${endOffset}px`,
|
|
"--end-top": `${endOffset}px`,
|
|
rotate: '135deg',
|
|
}
|
|
} else if (choice === 1) {
|
|
// Vpravo nahoře
|
|
return {
|
|
right: `${startOffset}px`,
|
|
startRight: `${startOffset}px`,
|
|
"--start-right": `${startOffset}px`,
|
|
top: `${startOffset}px`,
|
|
startTop: `${startOffset}px`,
|
|
"--start-top": `${startOffset}px`,
|
|
endRight: `${endOffset}px`,
|
|
"--end-right": `${endOffset}px`,
|
|
endTop: `${endOffset}px`,
|
|
"--end-top": `${endOffset}px`,
|
|
rotate: '-135deg',
|
|
}
|
|
} else if (choice === 2) {
|
|
// Vpravo dole
|
|
return {
|
|
right: `${startOffset}px`,
|
|
startRight: `${startOffset}px`,
|
|
"--start-right": `${startOffset}px`,
|
|
bottom: `${startOffset}px`,
|
|
startBottom: `${startOffset}px`,
|
|
"--start-bottom": `${startOffset}px`,
|
|
endRight: `${endOffset}px`,
|
|
"--end-right": `${endOffset}px`,
|
|
endBottom: `${endOffset}px`,
|
|
"--end-bottom": `${endOffset}px`,
|
|
rotate: '-45deg',
|
|
}
|
|
} else if (choice === 3) {
|
|
// Vlevo dole
|
|
return {
|
|
left: `${startOffset}px`,
|
|
startLeft: `${startOffset}px`,
|
|
"--start-left": `${startOffset}px`,
|
|
bottom: `${startOffset}px`,
|
|
startBottom: `${startOffset}px`,
|
|
"--start-bottom": `${startOffset}px`,
|
|
endLeft: `${endOffset}px`,
|
|
"--end-left": `${endOffset}px`,
|
|
endBottom: `${endOffset}px`,
|
|
"--end-bottom": `${endOffset}px`,
|
|
rotate: '45deg',
|
|
}
|
|
}
|
|
}
|
|
|
|
const router = express.Router();
|
|
|
|
let easterEggs: EasterEggsJson;
|
|
|
|
// Registrace náhodných URL pro všechny existující easter eggy
|
|
|
|
if (fs.existsSync(EASTER_EGGS_JSON_PATH)) {
|
|
const content = fs.readFileSync(EASTER_EGGS_JSON_PATH, 'utf-8');
|
|
easterEggs = JSON.parse(content);
|
|
for (const [_, eggs] of Object.entries(easterEggs)) {
|
|
for (const easterEgg of eggs) {
|
|
const url = generateUrl();
|
|
easterEgg.url = url;
|
|
router.get(`/${url}`, async (req, res, next) => {
|
|
return getEasterEggImage(req, res, next);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Získání náhodného easter eggu pro přihlášeného uživatele
|
|
router.get("/", async (req, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
try {
|
|
if (easterEggs && login in easterEggs) {
|
|
const randomEasterEgg = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)];
|
|
const { path, startOffset, endOffset, ...strippedEasterEgg } = randomEasterEgg; // Path klient k ničemu nepotřebuje a nemá ho znát
|
|
return res.status(200).json({ ...strippedEasterEgg, ...getRandomPosition(startOffset, endOffset) });
|
|
}
|
|
return res.status(200).send();
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
export default router; |