code quality updates
This commit is contained in:
+19
-22
@@ -1,19 +1,20 @@
|
||||
const router = require('express').Router();
|
||||
const axios = require('axios');
|
||||
const { escapeSvg, safeColor } = require('../lib/sanitize');
|
||||
const { parseSize, parseStyle, parseLabelpos, parseFontweight } = require('../lib/validate');
|
||||
const { FONT_FAMILY, calcFontSize, estimateTextWidth } = require('../lib/svg-utils');
|
||||
const logger = require('../lib/logger');
|
||||
|
||||
router.get('/badge', async (req, res) => {
|
||||
const {
|
||||
icon = 'simpleicons',
|
||||
label = 'Simple Icons',
|
||||
style = 'rect', // badge stílusa: rect (négyzetes), flat (lekerekített sarkok)
|
||||
bgicon = 'none', // ikon háttérszín, alap: világosszürke
|
||||
bglabel = 'none', // label háttérszín, alap: sötétszürke
|
||||
color = '#000000',
|
||||
size = 24,
|
||||
labelpos = 'right',
|
||||
fontweight = 'normal',
|
||||
} = req.query;
|
||||
const icon = req.query.icon || 'simpleicons';
|
||||
const label = req.query.label || 'Simple Icons';
|
||||
const style = parseStyle(req.query.style);
|
||||
const bgicon = req.query.bgicon || 'none';
|
||||
const bglabel = req.query.bglabel || 'none';
|
||||
const color = req.query.color || '#000000';
|
||||
const size = parseSize(req.query.size);
|
||||
const labelpos = parseLabelpos(req.query.labelpos);
|
||||
const fontweight = parseFontweight(req.query.fontweight);
|
||||
|
||||
try {
|
||||
// download icon SVG from SimpleIcons CDN
|
||||
@@ -41,15 +42,11 @@ router.get('/badge', async (req, res) => {
|
||||
const safeFontWeight = escapeSvg(fontweight);
|
||||
|
||||
const padding = 10;
|
||||
const iconSize = parseInt(size);
|
||||
const fontSize = Math.round(iconSize * 0.6);
|
||||
const textPadding = Math.round(iconSize * 0.4);
|
||||
const radius = 0;
|
||||
const approxCharWidth = fontSize * 0.6;
|
||||
const textWidth = effectiveLabel.length * approxCharWidth;
|
||||
|
||||
// Kinyerjük az ikon SVG-ből a width és height attribútumokat (ha vannak)
|
||||
let iconViewBoxWidth = iconSize;
|
||||
const iconSize = size;
|
||||
const fontSize = calcFontSize(iconSize);
|
||||
const textPadding = Math.round(iconSize * 0.4);
|
||||
const radius = style === 'flat' ? 3 : 0;
|
||||
const textWidth = estimateTextWidth(effectiveLabel, fontSize);
|
||||
let iconViewBoxHeight = iconSize;
|
||||
const widthMatch = iconSVG.match(/width="(\d+\.?\d*)"/);
|
||||
const heightMatch = iconSVG.match(/height="(\d+\.?\d*)"/);
|
||||
@@ -78,7 +75,7 @@ router.get('/badge', async (req, res) => {
|
||||
|
||||
let width, height, iconGroup, textElem;
|
||||
|
||||
const fontFamily = "Verdana,Geneva,DejaVu Sans,sans-serif";
|
||||
const fontFamily = FONT_FAMILY;
|
||||
const fontWeight = safeFontWeight; // paraméterből
|
||||
|
||||
if (labelpos === 'left') {
|
||||
@@ -149,7 +146,7 @@ router.get('/badge', async (req, res) => {
|
||||
if (err.response && err.response.status === 404) {
|
||||
return res.status(404).send('Icon not found');
|
||||
} else {
|
||||
console.error(err);
|
||||
logger.error({ err, icon, url: req.url }, 'Badge generation failed');
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user