pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 1 | import * as fs from "fs"; |
| 2 | import * as crypto from "crypto"; |
| 3 | import client from "../client.js"; |
| 4 | import * as path from "path"; |
| 5 | import {fileURLToPath} from "url"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 6 | const __filename = fileURLToPath(import.meta.url); |
| 7 | const __dirname = path.dirname(__filename); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
| 9 | export default function generateFileName(ending: string): string { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 10 | let fileName = crypto.randomBytes(35).toString("hex"); |
| 11 | fileName = fileName.replace(/([a-zA-Z0-9]{8})/g, "$1-"); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 12 | if (fs.existsSync(`./${fileName}`)) { |
| 13 | fileName = generateFileName(ending); |
| 14 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 15 | client.database.eventScheduler.schedule("deleteFile", new Date().getTime() + (60 * 1000), {fileName: `${fileName}.${ending}`}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 16 | return path.join(__dirname, fileName + "." + ending); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 17 | } |