hippofish/packages/backend/src/misc/emoji-meta.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-05-20 04:26:13 +02:00
import probeImageSize from "probe-image-size";
import { Mutex } from "redis-semaphore";
2023-05-20 04:26:13 +02:00
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
import Logger from "@/services/logger.js";
import { redisClient } from "@/db/redis.js";
2023-05-20 04:26:13 +02:00
export type Size = {
width: number;
height: number;
};
const logger = new Logger("emoji");
2023-05-20 04:26:13 +02:00
export async function getEmojiSize(url: string): Promise<Size> {
let attempted = true;
2023-05-20 04:26:13 +02:00
const lock = new Mutex(redisClient, "getEmojiSize");
await lock.acquire();
try {
const key = `getEmojiSize:${url}`;
attempted = await redisClient.get(key) !== null;
2023-05-20 04:26:13 +02:00
if (!attempted) {
await redisClient.set(key, "done", "EX", 60 * 10);
2023-05-20 04:26:13 +02:00
}
} finally {
await lock.release();
}
if (attempted) {
logger.warn(`Attempt limit exceeded: ${url}`);
throw new Error("attempt limit exceeded");
}
2023-05-20 04:26:13 +02:00
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");
2023-05-20 04:26:13 +02:00
}
return { width, height };
} catch (e) {
throw new Error(`unable to retrieve metadata: ${e}`);
2023-05-20 04:26:13 +02:00
}
}
export function getNormalSize(
{ width, height }: Size,
orientation?: number,
): Size {
return (orientation || 0) >= 5
? { width: height, height: width }
: { width, height };
}