reformat with prettier

This commit is contained in:
Elijah R 2024-07-14 20:33:47 -04:00
parent 1d4a2673b0
commit 2de60a98a3
5 changed files with 95 additions and 100 deletions

View file

@ -181,8 +181,8 @@ export function wordballoonDrawText(ctx: CanvasRenderingContext2D, at: Point, te
export function wordballoonDrawImage(ctx: CanvasRenderingContext2D, at: Point, img: HTMLImageElement, hasTip: boolean = true): Rect { export function wordballoonDrawImage(ctx: CanvasRenderingContext2D, at: Point, img: HTMLImageElement, hasTip: boolean = true): Rect {
// Round the image size up to the nearest 12x12, plus 12px padding. // Round the image size up to the nearest 12x12, plus 12px padding.
let size = { let size = {
w: (Math.ceil(img.width / 12) * 12) + 6, w: Math.ceil(img.width / 12) * 12 + 6,
h: (Math.ceil(img.height / 12) * 12) + 6 h: Math.ceil(img.height / 12) * 12 + 6
}; };
// Draw the word balloon and get the inner rect // Draw the word balloon and get the inner rect
@ -195,6 +195,6 @@ export function wordballoonDrawImage(ctx: CanvasRenderingContext2D, at: Point, i
x: at.x, x: at.x,
y: at.y, y: at.y,
w: rectInner.w + 12 * 3 + 12, w: rectInner.w + 12 * 3 + 12,
h: rectInner.h + 13 * 3 + 18, h: rectInner.h + 13 * 3 + 18
}; };
} }

View file

@ -62,6 +62,6 @@ export interface DiscordConfig {
} }
export interface ImagesConfig { export interface ImagesConfig {
maxSize: { width: number, height: number }; maxSize: { width: number; height: number };
expirySeconds: number; expirySeconds: number;
} }

View file

@ -1,8 +1,8 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { fileTypeFromBuffer } from "file-type"; import { fileTypeFromBuffer } from 'file-type';
import * as crypto from "crypto"; import * as crypto from 'crypto';
import Sharp from 'sharp'; import Sharp from 'sharp';
import { ImagesConfig } from "./config"; import { ImagesConfig } from './config';
export interface image { export interface image {
img: Buffer; img: Buffer;
@ -10,12 +10,7 @@ export interface image {
timeout: NodeJS.Timeout; timeout: NodeJS.Timeout;
} }
const allowedTypes = [ const allowedTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
];
function randomString(length: number): Promise<string> { function randomString(length: number): Promise<string> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
@ -26,7 +21,7 @@ function randomString(length: number): Promise<string> {
rej(err); rej(err);
return; return;
} }
let out = buf.toString("hex"); let out = buf.toString('hex');
if (out.length !== length) out = out.substring(0, length); if (out.length !== length) out = out.substring(0, length);
res(out); res(out);
}); });
@ -41,10 +36,10 @@ export class ImageUploader {
this.config = config; this.config = config;
for (let type of allowedTypes) { for (let type of allowedTypes) {
// i kinda hate this // i kinda hate this
app.addContentTypeParser(type, {parseAs: "buffer"}, (req, body, done) => done(null, body)); app.addContentTypeParser(type, { parseAs: 'buffer' }, (req, body, done) => done(null, body));
} }
app.put("/api/upload", async (req, res) => await this.handleRequest(req, res)); app.put('/api/upload', async (req, res) => await this.handleRequest(req, res));
app.get("/api/image/:id", (req, res) => this.handleGet(req, res)); app.get('/api/image/:id', (req, res) => this.handleGet(req, res));
} }
private handleGet(req: FastifyRequest, res: FastifyReply) { private handleGet(req: FastifyRequest, res: FastifyReply) {
@ -52,17 +47,17 @@ export class ImageUploader {
let img = this.images.get(id); let img = this.images.get(id);
if (!img) { if (!img) {
res.status(404); res.status(404);
return { success: false, error: "Image not found" }; return { success: false, error: 'Image not found' };
} }
res.header("Content-Type", img.mime); res.header('Content-Type', img.mime);
return img.img; return img.img;
} }
private async handleRequest(req: FastifyRequest, res: FastifyReply) { private async handleRequest(req: FastifyRequest, res: FastifyReply) {
let contentType; let contentType;
if ((contentType = req.headers["content-type"]) === undefined || !allowedTypes.includes(contentType)) { if ((contentType = req.headers['content-type']) === undefined || !allowedTypes.includes(contentType)) {
res.status(400); res.status(400);
return { success: false, error: "Invalid Content-Type" }; return { success: false, error: 'Invalid Content-Type' };
} }
let data = req.body as Buffer; let data = req.body as Buffer;
@ -72,7 +67,7 @@ export class ImageUploader {
if (mime?.mime !== contentType) { if (mime?.mime !== contentType) {
res.status(400); res.status(400);
return { success: false, error: "Image is corrupt" }; return { success: false, error: 'Image is corrupt' };
} }
// Parse and resize if necessary // Parse and resize if necessary
@ -83,16 +78,16 @@ export class ImageUploader {
meta = await sharp.metadata(); meta = await sharp.metadata();
} catch { } catch {
res.status(400); res.status(400);
return { success: false, error: "Image is corrupt" }; return { success: false, error: 'Image is corrupt' };
} }
if (!meta.width || !meta.height) { if (!meta.width || !meta.height) {
res.status(400); res.status(400);
return { success: false, error: "Image is corrupt" }; return { success: false, error: 'Image is corrupt' };
} }
if (meta.width > this.config.maxSize.width || meta.height > this.config.maxSize.height) { if (meta.width > this.config.maxSize.width || meta.height > this.config.maxSize.height) {
sharp.resize(this.config.maxSize.width, this.config.maxSize.height, { fit: "inside" }); sharp.resize(this.config.maxSize.width, this.config.maxSize.height, { fit: 'inside' });
} }
let outputImg = await sharp.toBuffer(); let outputImg = await sharp.toBuffer();

View file

@ -43,7 +43,7 @@ const app = Fastify({
app.register(FastifyCors, { app.register(FastifyCors, {
origin: config.http.origins, origin: config.http.origins,
methods: ['GET', 'POST', 'PUT'], methods: ['GET', 'POST', 'PUT']
}); });
app.register(FastifyWS); app.register(FastifyWS);

View file

@ -114,7 +114,7 @@ export class MSAgentChatRoom {
for (const _client of this.getActiveClients()) { for (const _client of this.getActiveClients()) {
_client.send(msg); _client.send(msg);
} }
}) });
client.on('admin', () => { client.on('admin', () => {
let msg: MSAgentPromoteMessage = { let msg: MSAgentPromoteMessage = {
op: MSAgentProtocolMessageType.Promote, op: MSAgentProtocolMessageType.Promote,