Make canvas optional

This commit is contained in:
TheArcaneBrony
2022-08-20 03:18:41 +02:00
parent 3795c67723
commit c0cdfd332c
2 changed files with 323 additions and 1092 deletions

View File

@@ -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)) {

1399
yarn.lock

File diff suppressed because it is too large Load Diff