diff options
Diffstat (limited to 'src/api/routes/guilds/#guild_id/widget.png.ts')
-rw-r--r-- | src/api/routes/guilds/#guild_id/widget.png.ts | 16 |
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)) { |