From 36477ed2531c52add22fceed4c37bfdaa00e87bf Mon Sep 17 00:00:00 2001 From: Essem Date: Mon, 18 Jul 2022 17:05:01 -0500 Subject: [PATCH] Refactor/unify temp image handling --- events/interactionCreate.js | 44 ++----------------------- events/messageCreate.js | 44 ++----------------------- utils/tempimages.js | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 84 deletions(-) create mode 100644 utils/tempimages.js diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 513ce7c..c5ee35f 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,9 +1,9 @@ -import { promises } from "fs"; import database from "../utils/database.js"; import * as logger from "../utils/logger.js"; import { commands } from "../utils/collections.js"; import { CommandInteraction } from "eris"; import { clean } from "../utils/misc.js"; +import { upload } from "../utils/tempimages.js"; // run when a slash command is executed export default async (client, cluster, worker, ipc, interaction) => { @@ -39,47 +39,7 @@ export default async (client, cluster, worker, ipc, interaction) => { } if (result.file.length > fileSize) { if (process.env.TEMPDIR && process.env.TEMPDIR !== "") { - const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; - await promises.writeFile(`${process.env.TEMPDIR}/${filename}`, result.file); - const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`; - await interaction[interaction.acknowledged ? "editOriginalMessage" : "createMessage"]({ - embeds: [{ - color: 16711680, - title: "Here's your image!", - url: imageURL, - image: { - url: imageURL - }, - footer: { - text: "The result image was more than 8MB in size, so it was uploaded to an external site instead." - }, - }] - }); - if (process.env.THRESHOLD) { - process.env.DIRSIZECACHE += result.file.length; - if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { - const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => { - return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => { - return { - name: file, - size: stats.size, - ctime: stats.ctime - }; - }); - }); - const resolvedFiles = await Promise.all(files); - process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{ - return a + b.size; - }, 0); - const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime); - while (process.env.DIRSIZECACHE > process.env.THRESHOLD) { - await promises.rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`); - process.env.DIRSIZECACHE -= oldestFiles[0].size; - logger.log(`Removed oldest image file: ${oldestFiles[0].name}`); - oldestFiles.shift(); - } - } - } + await upload(client, result, interaction, true); } else { await interaction[interaction.acknowledged ? "editOriginalMessage" : "createMessage"]("The resulting image was more than 8MB in size, so I can't upload it."); } diff --git a/events/messageCreate.js b/events/messageCreate.js index 8c47844..8e96e30 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -1,9 +1,9 @@ -import { promises } from "fs"; import database from "../utils/database.js"; import { log, error as _error } from "../utils/logger.js"; import { prefixCache, aliases, disabledCache, disabledCmdCache, commands } from "../utils/collections.js"; import parseCommand from "../utils/parseCommand.js"; import { clean } from "../utils/misc.js"; +import { upload } from "../utils/tempimages.js"; // run when someone sends a message export default async (client, cluster, worker, ipc, message) => { @@ -129,47 +129,7 @@ export default async (client, cluster, worker, ipc, message) => { } if (result.file.length > fileSize) { if (process.env.TEMPDIR && process.env.TEMPDIR !== "") { - const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; - await promises.writeFile(`${process.env.TEMPDIR}/${filename}`, result.file); - const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`; - await client.createMessage(message.channel.id, Object.assign({ - embeds: [{ - color: 16711680, - title: "Here's your image!", - url: imageURL, - image: { - url: imageURL - }, - footer: { - text: "The result image was more than 8MB in size, so it was uploaded to an external site instead." - }, - }] - }, reference)); - if (process.env.THRESHOLD) { - process.env.DIRSIZECACHE += result.file.length; - if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { - const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => { - return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => { - return { - name: file, - size: stats.size, - ctime: stats.ctime - }; - }); - }); - const resolvedFiles = await Promise.all(files); - process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{ - return a + b.size; - }, 0); - const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime); - while (process.env.DIRSIZECACHE > process.env.THRESHOLD) { - await promises.rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`); - process.env.DIRSIZECACHE -= oldestFiles[0].size; - log(`Removed oldest image file: ${oldestFiles[0].name}`); - oldestFiles.shift(); - } - } - } + await upload(client, result, message); } else { await client.createMessage(message.channel.id, "The resulting image was more than 8MB in size, so I can't upload it."); } diff --git a/utils/tempimages.js b/utils/tempimages.js new file mode 100644 index 0000000..eba6603 --- /dev/null +++ b/utils/tempimages.js @@ -0,0 +1,66 @@ +import * as logger from "../utils/logger.js"; +import { readdir, lstat, rm, writeFile } from "fs/promises"; + +export async function upload(client, result, context, interaction = false) { + const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; + await writeFile(`${process.env.TEMPDIR}/${filename}`, result.file); + const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`; + const payload = { + embeds: [{ + color: 16711680, + title: "Here's your image!", + url: imageURL, + image: { + url: imageURL + }, + footer: { + text: "The result image was more than 8MB in size, so it was uploaded to an external site instead." + }, + }] + }; + if (interaction) { + await context[context.acknowledged ? "editOriginalMessage" : "createMessage"](payload); + } else { + await client.createMessage(context.channel.id, Object.assign(payload, { + messageReference: { + channelID: context.channel.id, + messageID: context.id, + guildID: context.channel.guild ? context.channel.guild.id : undefined, + failIfNotExists: false + }, + allowedMentions: { + repliedUser: false + } + })); + } + if (process.env.THRESHOLD) { + process.env.DIRSIZECACHE += result.file.length; + await removeOldImages(); + } +} + +export async function removeOldImages() { + if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { + const files = (await readdir(process.env.TEMPDIR)).map((file) => { + return lstat(`${process.env.TEMPDIR}/${file}`).then((stats) => { + if (stats.isSymbolicLink()) return; + return { + name: file, + size: stats.size, + ctime: stats.ctime + }; + }); + }).filter(Boolean); + const resolvedFiles = await Promise.all(files); + process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{ + return a + b.size; + }, 0); + const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime); + while (process.env.DIRSIZECACHE > process.env.THRESHOLD) { + await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`); + process.env.DIRSIZECACHE -= oldestFiles[0].size; + logger.log(`Removed oldest image file: ${oldestFiles[0].name}`); + oldestFiles.shift(); + } + } +} \ No newline at end of file