blob: 109478de9469f2e5cda22aefe7ee316cff99c3b2 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import * as fs from "fs";
2import * as crypto from "crypto";
3import client from "../client.js";
4import * as path from "path";
Skyler Grey75ea9172022-08-06 10:22:23 +01005import { fileURLToPath } from "url";
pineafan73a7c4a2022-07-24 10:38:04 +01006const __filename = fileURLToPath(import.meta.url);
7const __dirname = path.dirname(__filename);
pineafan32767212022-03-14 21:27:39 +00008
9export default function generateFileName(ending: string): string {
pineafan63fc5e22022-08-04 22:04:10 +010010 let fileName = crypto.randomBytes(35).toString("hex");
11 fileName = fileName.replace(/([a-zA-Z0-9]{8})/g, "$1-");
pineafan32767212022-03-14 21:27:39 +000012 if (fs.existsSync(`./${fileName}`)) {
13 fileName = generateFileName(ending);
14 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050015 client.database.eventScheduler.schedule("deleteFile", (Date.now() + 60 * 1000).toString(), {
Skyler Grey11236ba2022-08-08 21:13:33 +010016 fileName: `${fileName}.${ending}`
17 });
pineafan63fc5e22022-08-04 22:04:10 +010018 return path.join(__dirname, fileName + "." + ending);
pineafan73a7c4a2022-07-24 10:38:04 +010019}