2022-07-18 22:05:01 +00:00
|
|
|
import * as logger from "../utils/logger.js";
|
2022-09-21 05:05:03 +00:00
|
|
|
import { readdir, lstat, rm, writeFile, stat } from "fs/promises";
|
2022-07-18 22:05:01 +00:00
|
|
|
|
2022-09-21 05:05:03 +00:00
|
|
|
let dirSizeCache;
|
|
|
|
|
|
|
|
export async function upload(client, result, context, interaction = false) {
|
2022-07-18 22:05:01 +00:00
|
|
|
const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`;
|
2022-09-24 16:26:56 +00:00
|
|
|
await writeFile(`${process.env.TEMPDIR}/${filename}`, result.contents);
|
2022-12-28 06:20:17 +00:00
|
|
|
const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.esmbot.net"}/${filename}`;
|
2022-07-18 22:05:01 +00:00
|
|
|
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) {
|
2022-09-24 16:26:56 +00:00
|
|
|
await context[context.acknowledged ? "editOriginal" : "createMessage"](payload);
|
2022-07-18 22:05:01 +00:00
|
|
|
} else {
|
2022-09-27 19:46:07 +00:00
|
|
|
await client.rest.channels.createMessage(context.channelID, Object.assign(payload, {
|
2022-07-18 22:05:01 +00:00
|
|
|
messageReference: {
|
2022-09-27 19:46:07 +00:00
|
|
|
channelID: context.channelID,
|
2022-07-18 22:05:01 +00:00
|
|
|
messageID: context.id,
|
2022-09-27 19:46:07 +00:00
|
|
|
guildID: context.guildID ?? undefined,
|
2022-07-18 22:05:01 +00:00
|
|
|
failIfNotExists: false
|
|
|
|
},
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (process.env.THRESHOLD) {
|
2022-09-24 16:26:56 +00:00
|
|
|
const size = dirSizeCache + result.contents.length;
|
2022-09-21 05:05:03 +00:00
|
|
|
dirSizeCache = size;
|
|
|
|
await removeOldImages(size);
|
2022-07-18 22:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:05:03 +00:00
|
|
|
async function removeOldImages(size) {
|
2022-07-20 04:27:43 +00:00
|
|
|
if (size > process.env.THRESHOLD) {
|
2022-07-18 22:05:01 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
});
|
2022-07-25 06:00:31 +00:00
|
|
|
});
|
|
|
|
|
2022-07-18 22:05:01 +00:00
|
|
|
const resolvedFiles = await Promise.all(files);
|
2022-07-25 06:00:31 +00:00
|
|
|
const oldestFiles = resolvedFiles.filter(Boolean).sort((a, b) => a.ctime - b.ctime);
|
|
|
|
|
|
|
|
do {
|
2022-07-20 04:03:51 +00:00
|
|
|
if (!oldestFiles[0]) break;
|
2022-07-18 22:05:01 +00:00
|
|
|
await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
|
|
|
|
logger.log(`Removed oldest image file: ${oldestFiles[0].name}`);
|
2022-07-25 06:02:59 +00:00
|
|
|
size -= oldestFiles[0].size;
|
2022-07-18 22:05:01 +00:00
|
|
|
oldestFiles.shift();
|
2022-07-25 06:02:59 +00:00
|
|
|
} while (size > process.env.THRESHOLD);
|
2022-07-25 06:00:31 +00:00
|
|
|
|
2022-07-25 06:02:59 +00:00
|
|
|
const newSize = oldestFiles.reduce((a, b) => {
|
2022-07-25 06:00:31 +00:00
|
|
|
return a + b.size;
|
|
|
|
}, 0);
|
2022-09-21 05:05:03 +00:00
|
|
|
dirSizeCache = newSize;
|
2022-07-18 22:05:01 +00:00
|
|
|
}
|
2022-07-25 06:00:31 +00:00
|
|
|
}
|
2022-09-21 05:05:03 +00:00
|
|
|
|
|
|
|
export async function parseThreshold() {
|
|
|
|
const matched = process.env.THRESHOLD.match(/(\d+)([KMGT])/);
|
|
|
|
const sizes = {
|
|
|
|
K: 1024,
|
|
|
|
M: 1048576,
|
|
|
|
G: 1073741824,
|
|
|
|
T: 1099511627776
|
|
|
|
};
|
|
|
|
if (matched && matched[1] && matched[2]) {
|
|
|
|
process.env.THRESHOLD = matched[1] * sizes[matched[2]];
|
|
|
|
} else {
|
|
|
|
logger.error("Invalid THRESHOLD config.");
|
|
|
|
process.env.THRESHOLD = undefined;
|
|
|
|
}
|
|
|
|
const dirstat = (await readdir(process.env.TEMPDIR)).map((file) => {
|
|
|
|
return stat(`${process.env.TEMPDIR}/${file}`).then((stats) => stats.size);
|
|
|
|
});
|
|
|
|
const size = await Promise.all(dirstat);
|
|
|
|
const reduced = size.reduce((a, b) => {
|
|
|
|
return a + b;
|
|
|
|
}, 0);
|
|
|
|
dirSizeCache = reduced;
|
|
|
|
}
|