jormungandr-bite/packages/backend/src/misc/emoji-meta.ts
ThatOneCalculator 466ef220da Revert "Merge pull request '[PR]: fix: use redis-based mutex to prevent memory leak' (#10389) from nmkj/calckey:fix-cache-leak into develop"
This reverts commit 224b1d585bb1cb95531a9213cf4a69fc2f049e7d, reversing
changes made to b5907fb86db2b9eacea633bb5d69547982326afa.
2023-07-02 10:10:25 -07:00

50 lines
1.3 KiB
TypeScript

import probeImageSize from "probe-image-size";
import { Mutex, withTimeout } from "async-mutex";
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
import Logger from "@/services/logger.js";
import { Cache } from "./cache.js";
export type Size = {
width: number;
height: number;
};
const cache = new Cache<boolean>(1000 * 60 * 10); // once every 10 minutes for the same url
const mutex = withTimeout(new Mutex(), 1000);
export async function getEmojiSize(url: string): Promise<Size> {
const logger = new Logger("emoji");
await mutex.runExclusive(() => {
const attempted = cache.get(url);
if (!attempted) {
cache.set(url, true);
} else {
logger.warn(`Attempt limit exceeded: ${url}`);
throw new Error("Too many attempts");
}
});
try {
logger.info(`Retrieving emoji size from ${url}`);
const { width, height, mime } = await probeImageSize(url, {
timeout: 5000,
});
if (!(mime.startsWith("image/") && FILE_TYPE_BROWSERSAFE.includes(mime))) {
throw new Error("Unsupported image type");
}
return { width, height };
} catch (e) {
throw new Error(`Unable to retrieve metadata: ${e}`);
}
}
export function getNormalSize(
{ width, height }: Size,
orientation?: number,
): Size {
return (orientation || 0) >= 5
? { width: height, height: width }
: { width, height };
}