summary refs log tree commit diff
path: root/src/api/routes/guilds/#guild_id/widget.png.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:29:30 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:41:21 +1000
commit99ee7e9400f06e8718612d8b52d15215dc620774 (patch)
tree08de8c5d3985b9c2eaa419f5198f891ecd82d012 /src/api/routes/guilds/#guild_id/widget.png.ts
parentRemove the cdn storage location log (diff)
downloadserver-99ee7e9400f06e8718612d8b52d15215dc620774.tar.xz
Prettier
Diffstat (limited to 'src/api/routes/guilds/#guild_id/widget.png.ts')
-rw-r--r--src/api/routes/guilds/#guild_id/widget.png.ts92
1 files changed, 80 insertions, 12 deletions
diff --git a/src/api/routes/guilds/#guild_id/widget.png.ts b/src/api/routes/guilds/#guild_id/widget.png.ts
index c17d511e..eaec8f07 100644
--- a/src/api/routes/guilds/#guild_id/widget.png.ts
+++ b/src/api/routes/guilds/#guild_id/widget.png.ts
@@ -24,8 +24,13 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 
 	// Fetch parameter
 	const style = req.query.style?.toString() || "shield";
-	if (!["shield", "banner1", "banner2", "banner3", "banner4"].includes(style)) {
-		throw new HTTPError("Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", 400);
+	if (
+		!["shield", "banner1", "banner2", "banner3", "banner4"].includes(style)
+	) {
+		throw new HTTPError(
+			"Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').",
+			400,
+		);
 	}
 
 	// Setup canvas
@@ -34,7 +39,17 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	const sizeOf = require("image-size");
 
 	// TODO: Widget style templates need Fosscord branding
-	const source = path.join(__dirname, "..", "..", "..", "..", "..", "assets", "widget", `${style}.png`);
+	const source = path.join(
+		__dirname,
+		"..",
+		"..",
+		"..",
+		"..",
+		"..",
+		"assets",
+		"widget",
+		`${style}.png`,
+	);
 	if (!fs.existsSync(source)) {
 		throw new HTTPError("Widget template does not exist.", 400);
 	}
@@ -50,30 +65,68 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	switch (style) {
 		case "shield":
 			ctx.textAlign = "center";
-			await drawText(ctx, 73, 13, "#FFFFFF", "thin 10px Verdana", presence);
+			await drawText(
+				ctx,
+				73,
+				13,
+				"#FFFFFF",
+				"thin 10px Verdana",
+				presence,
+			);
 			break;
 		case "banner1":
 			if (icon) await drawIcon(ctx, 20, 27, 50, icon);
 			await drawText(ctx, 83, 51, "#FFFFFF", "12px Verdana", name, 22);
-			await drawText(ctx, 83, 66, "#C9D2F0FF", "thin 11px Verdana", presence);
+			await drawText(
+				ctx,
+				83,
+				66,
+				"#C9D2F0FF",
+				"thin 11px Verdana",
+				presence,
+			);
 			break;
 		case "banner2":
 			if (icon) await drawIcon(ctx, 13, 19, 36, icon);
 			await drawText(ctx, 62, 34, "#FFFFFF", "12px Verdana", name, 15);
-			await drawText(ctx, 62, 49, "#C9D2F0FF", "thin 11px Verdana", presence);
+			await drawText(
+				ctx,
+				62,
+				49,
+				"#C9D2F0FF",
+				"thin 11px Verdana",
+				presence,
+			);
 			break;
 		case "banner3":
 			if (icon) await drawIcon(ctx, 20, 20, 50, icon);
 			await drawText(ctx, 83, 44, "#FFFFFF", "12px Verdana", name, 27);
-			await drawText(ctx, 83, 58, "#C9D2F0FF", "thin 11px Verdana", presence);
+			await drawText(
+				ctx,
+				83,
+				58,
+				"#C9D2F0FF",
+				"thin 11px Verdana",
+				presence,
+			);
 			break;
 		case "banner4":
 			if (icon) await drawIcon(ctx, 21, 136, 50, icon);
 			await drawText(ctx, 84, 156, "#FFFFFF", "13px Verdana", name, 27);
-			await drawText(ctx, 84, 171, "#C9D2F0FF", "thin 12px Verdana", presence);
+			await drawText(
+				ctx,
+				84,
+				171,
+				"#C9D2F0FF",
+				"thin 12px Verdana",
+				presence,
+			);
 			break;
 		default:
-			throw new HTTPError("Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", 400);
+			throw new HTTPError(
+				"Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').",
+				400,
+			);
 	}
 
 	// Return final image
@@ -83,7 +136,13 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	return res.send(buffer);
 });
 
-async function drawIcon(canvas: any, x: number, y: number, scale: number, icon: string) {
+async function drawIcon(
+	canvas: any,
+	x: number,
+	y: number,
+	scale: number,
+	icon: string,
+) {
 	// @ts-ignore
 	const img = new require("canvas").Image();
 	img.src = icon;
@@ -101,10 +160,19 @@ async function drawIcon(canvas: any, x: number, y: number, scale: number, icon:
 	canvas.restore();
 }
 
-async function drawText(canvas: any, x: number, y: number, color: string, font: string, text: string, maxcharacters?: number) {
+async function drawText(
+	canvas: any,
+	x: number,
+	y: number,
+	color: string,
+	font: string,
+	text: string,
+	maxcharacters?: number,
+) {
 	canvas.fillStyle = color;
 	canvas.font = font;
-	if (text.length > (maxcharacters || 0) && maxcharacters) text = text.slice(0, maxcharacters) + "...";
+	if (text.length > (maxcharacters || 0) && maxcharacters)
+		text = text.slice(0, maxcharacters) + "...";
 	canvas.fillText(text, x, y);
 }