Embed base images into binary

Include resource directory
Ran `deno fmt` with 4 space indent, that changed every file (!!!!!)
various changes
This commit is contained in:
2025-03-24 19:11:36 -04:00
parent 2207e389c9
commit 49c481aa0e
61 changed files with 2369 additions and 2031 deletions

106
src/routes/img.ts Normal file
View File

@@ -0,0 +1,106 @@
import { APIUtils, NoBody } from "../apiutils.ts";
import * as BaseImages from "../data/content/baseimages.ts";
import Logging from "@proxnet/undead-logging";
import express from "express";
import * as Images from "./../data/content/images.ts";
import { Image } from "https://deno.land/x/imagescript@1.3.0/mod.ts";
import { Buffer } from "node:buffer";
export const route = APIUtils.createRouter("/img");
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890. "
.split("");
function sanitizeString(input: string) {
return input.split("").filter((char) => chars.includes(char)).join("");
}
const baseImages = BaseImages.getAllBaseImages();
interface ImageQueryOptions {
cropSquare?: string;
width?: string;
height?: string;
}
route.router.get(
"*",
async (
rq: express.Request<NoBody, NoBody, NoBody, ImageQueryOptions>,
rs: express.Response,
nxt: express.NextFunction,
) => {
const filename = sanitizeString(
rq.path.substring(1, rq.path.length).replaceAll("%20", " "),
);
// why does it think it is never reassigned? line 39
// deno-lint-ignore prefer-const
let image: Image;
const imageSource = baseImages.includes(filename)
? BaseImages.getBaseImage(filename)
: await Images.getImage(filename);
if (imageSource == null) {
nxt();
return;
}
image = await Image.decode(imageSource);
let cropSquare: boolean = false;
if (typeof rq.query.cropSquare == "string") {
const d = JSON.parse(rq.query.cropSquare);
if (typeof d == "boolean" && d) cropSquare = true;
}
let width: number | null = null;
if (typeof rq.query.width == "string") {
const num = parseInt(rq.query.width);
if (isNaN(num)) width = null;
else width = num;
}
let height: number | null = null;
if (typeof rq.query.height == "string") {
const num = parseInt(rq.query.height);
if (isNaN(num)) height = null;
else height = num;
}
if (cropSquare) {
if (image.width > image.height) {
image.crop(
Math.round(image.width / 2) - Math.round(image.height / 2),
0,
image.height,
image.height,
);
} else {image.crop(
0,
Math.round(image.height / 2) - Math.round(image.width / 2),
image.width,
image.width,
);}
}
if (width && height) {
const targetWidth = width > image.width ? image.width : width;
const targetHeight = height > image.height ? image.height : height;
if (image.width > image.height) {
image.resize(Image.RESIZE_AUTO, height);
image.crop(
Math.round(image.width / 2) - Math.round(targetWidth / 2),
0,
targetWidth,
image.height,
);
} else {
image.resize(width, Image.RESIZE_AUTO);
image.crop(
0,
Math.round(image.height / 2) - Math.round(targetHeight / 2),
image.width,
targetHeight,
);
}
} else if (width) image.resize(width, Image.RESIZE_AUTO);
else if (height) image.resize(Image.RESIZE_AUTO, height);
rs.type("png").send(Buffer.from(await image.encode()));
},
);