Updated naming

This commit is contained in:
2023-10-19 02:41:51 +02:00
committed by GitHub
parent 1fae676a9c
commit 668119d6e0

View File

@@ -14,7 +14,7 @@ const app = express();
const storage = diskStorage({ const storage = diskStorage({
destination: 'sounds/', destination: 'sounds/',
filename: function (_req, file, cb) { filename: function (_req, file, cb) {
cb(null, Date.now() + '-' + file.originalname); cb(null, generateFileName(file.originalname));
} }
}); });
@@ -189,14 +189,16 @@ export function startServer() {
* @param name - The name to generate a file name for. * @param name - The name to generate a file name for.
* @returns string - The generated file name. * @returns string - The generated file name.
*/ */
function generateFileName(name: string): string { function generateFileName(name: string) {
const genRanHex = [...Array(3)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); const randomHex = [...Array(3)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
const newName = name const formattedName = name
.replace(/[^a-zA-Z ]/g, "") .replace(/\(.*?\)|\[.*?\]/g, '')
.split(' ')
.filter(word => /^[a-zA-Z0-9]/.test(word))
.join(' ')
.replace(/\s+/g, '-') .replace(/\s+/g, '-')
.toLowerCase() .toLowerCase();
.replace(/\(.*?\)/g, '').replace(/#.*?\s/g, '');
return `${newName}-${genRanHex}.mp3`; return `${formattedName}-${randomHex}.mp3`;
} }