import { lstat, readdir, rm, stat, writeFile } from "node:fs/promises"; import { type Client, CommandInteraction, Constants, type File, type InteractionContent, type Message, } from "oceanic.js"; import { selectedImages } from "./collections.ts"; import { getString } from "./i18n.ts"; import logger from "./logger.ts"; import { getEnv } from "./runtime.ts"; type SizeSuffix = "K" | "M" | "G" | "T"; type FileStats = { name: string; size: number; ctime: Date; }; let dirSizeCache: number; let threshold: number | undefined; export async function upload( client: Client, result: File & { flags?: number }, context: CommandInteraction | Message, tempdir: string, ) { const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; await writeFile(`${tempdir}/${filename}`, result.contents); if (threshold && tempdir) { const size = dirSizeCache + result.contents.length; dirSizeCache = size; await removeOldImages(size, tempdir); } const imageURL = `${getEnv("TMP_DOMAIN") || "https://tmp.esmbot.net"}/${filename}`; const payload: InteractionContent = { components: [ { type: Constants.ComponentTypes.MEDIA_GALLERY, items: [ { media: { url: imageURL }, spoiler: result.name.startsWith("SPOILER_"), }, ], }, { type: Constants.ComponentTypes.TEXT_DISPLAY, content: `-# ${getString("image.tempSite", { locale: context instanceof CommandInteraction ? context.locale : undefined, })}`, }, ], flags: (result.flags ?? 0) | Constants.MessageFlags.IS_COMPONENTS_V2, }; let authorId: string; if (context instanceof CommandInteraction) { authorId = context.user.id; await context.createFollowup(payload); // save media if used outside of guild/dms if (context.authorizingIntegrationOwners[0] === undefined) { selectedImages.set(authorId, { path: imageURL, spoiler: result.name.startsWith("SPOILER_"), result: true, }); } } else { authorId = context.author.id; await client.rest.channels.createMessage( context.channelID, Object.assign(payload, { messageReference: { channelID: context.channelID, messageID: context.id, guildID: context.guildID ?? undefined, failIfNotExists: false, }, allowedMentions: { repliedUser: false, }, }), ); } } async function removeOldImages(s: number, tempdir: string) { if (!threshold) return; let size = s; if (size > threshold) { const files = (await readdir(tempdir)).map(async (file) => { const stats = await lstat(`${tempdir}/${file}`); if (stats.isSymbolicLink()) return; return { name: file, size: stats.size, ctime: stats.ctime, } as FileStats; }); const resolvedFiles = await Promise.all(files); const oldestFiles = resolvedFiles .filter((item): item is FileStats => !!item) .sort((a, b) => a.ctime.getTime() - b.ctime.getTime()); do { if (!oldestFiles[0]) break; await rm(`${tempdir}/${oldestFiles[0].name}`); logger.log(`Removed oldest image file: ${oldestFiles[0].name}`); size -= oldestFiles[0].size; oldestFiles.shift(); } while (size > threshold); const newSize = oldestFiles.reduce((a, b) => { return a + b.size; }, 0); dirSizeCache = newSize; } } export async function parseThreshold(tempdir: string, inThreshold: string) { const matched = inThreshold.match(/(\d+)([KMGT])/); const sizes = { K: 1024, M: 1048576, G: 1073741824, T: 1099511627776, }; if (matched?.[1] && matched[2]) { threshold = Number(matched[1]) * sizes[matched[2] as SizeSuffix]; } else { logger.error("Invalid THRESHOLD config."); threshold = undefined; } const dirstat = (await readdir(tempdir)).map((file) => { return stat(`${tempdir}/${file}`).then((stats) => stats.size); }); const size = await Promise.all(dirstat); const reduced = size.reduce((a, b) => { return a + b; }, 0); dirSizeCache = reduced; }