summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-20 03:18:41 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-20 03:18:41 +0200
commitc0cdfd332c695d5aa48c150c5cb44fba9b445e84 (patch)
tree3dbe8e836b15af4204ea3b45f770c0241363b831 /src/api
parentAdd fallback to bcryptjs (diff)
downloadserver-c0cdfd332c695d5aa48c150c5cb44fba9b445e84.tar.xz
Make canvas optional
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/guilds/#guild_id/widget.png.ts16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/api/routes/guilds/#guild_id/widget.png.ts b/src/api/routes/guilds/#guild_id/widget.png.ts
index a61d938d..d064fa13 100644
--- a/src/api/routes/guilds/#guild_id/widget.png.ts
+++ b/src/api/routes/guilds/#guild_id/widget.png.ts
@@ -5,6 +5,16 @@ import { route } from "@fosscord/api";
 import fs from "fs";
 import path from "path";
 
+// Setup canvas
+let createCanvas: any, loadImage: any;
+try {
+	createCanvas = require("canvas").createCanvas;
+	loadImage = require("canvas").loadImage;
+} catch {
+	console.log("Canvas not found, disabling widgets!");
+}
+const sizeOf = require("image-size");
+
 const router: Router = Router();
 
 // TODO: use svg templates instead of node-canvas for improved performance and to change it easily
@@ -12,6 +22,7 @@ const router: Router = Router();
 // https://discord.com/developers/docs/resources/guild#get-guild-widget-image
 // TODO: Cache the response
 router.get("/", route({}), async (req: Request, res: Response) => {
+	if (!createCanvas) return res.status(404);
 	const { guild_id } = req.params;
 
 	const guild = await Guild.findOneOrFail({ where: { id: guild_id } });
@@ -28,11 +39,6 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 		throw new HTTPError("Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", 400);
 	}
 
-	// Setup canvas
-	const { createCanvas } = require("canvas");
-	const { loadImage } = require("canvas");
-	const sizeOf = require("image-size");
-
 	// TODO: Widget style templates need Fosscord branding
 	const source = path.join(__dirname, "..", "..", "..", "..", "..", "assets", "widget", `${style}.png`);
 	if (!fs.existsSync(source)) {