pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 1 | import * as fs from 'fs'; |
| 2 | import * as crypto from 'crypto'; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 3 | import client from '../client.js'; |
| 4 | import * as path from 'path' |
| 5 | import {fileURLToPath} from 'url'; |
| 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 { |
| 10 | let fileName = crypto.randomBytes(35).toString('hex'); |
| 11 | fileName = fileName.replace(/([a-zA-Z0-9]{8})/g, '$1-'); |
| 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}`}); |
| 16 | return path.join(__dirname, fileName + '.' + ending); |
| 17 | } |