mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-06 01:55:56 +00:00
Make canvas optional
This commit is contained in:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user