blob: f9662ad74525d0288ea97439ad9f74fa46cdd9b3 [file] [log] [blame]
pineafan32767212022-03-14 21:27:39 +00001import * as fs from 'fs';
2import * as crypto from 'crypto';
pineafan73a7c4a2022-07-24 10:38:04 +01003import client from '../client.js';
4import * as path from 'path'
5import {fileURLToPath} from 'url';
6const __filename = fileURLToPath(import.meta.url);
7const __dirname = path.dirname(__filename);
pineafan32767212022-03-14 21:27:39 +00008
9export 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 }
pineafan73a7c4a2022-07-24 10:38:04 +010015 client.database.eventScheduler.schedule("deleteFile", new Date().getTime() + (60 * 1000), {fileName: `${fileName}.${ending}`});
16 return path.join(__dirname, fileName + '.' + ending);
17}