Removed unnecessary code & added element types

This commit is contained in:
Arne van Iterson 2019-09-26 22:04:41 +02:00
parent fd29ace545
commit 41c46a664c

View File

@ -49,11 +49,15 @@ function dbGet(type, id) {
if (id === 'all') { if (id === 'all') {
result = db[type]; result = db[type];
result.forEach(element => {
element.type = type;
});
} else { } else {
id = parseInt(id); id = parseInt(id);
db[type].forEach(element => { db[type].forEach(element => {
if (id == element.id) { if (id == element.id) {
result = element; result = element;
result.type = type;
} }
}); });
} }
@ -74,39 +78,11 @@ router.get('/get/:type/:id', (req, res, _next) => {
const arg = id.split(","); const arg = id.split(",");
resultArray = []; resultArray = [];
// TODO
// OK, for some reason this script works absolutely fine when run the fist time,
// However, if you run any other get url or refresh the page it will either throw a circulation error or return a bunch of null values
// I have no idea what is causing this but i am absolutely fucking done with it right now
// If you read this and you know how to fix it, please change it
// For each array in argument, make object in resultArray // For each array in argument, make object in resultArray
for (let index = 0; index < arg.length; index++) { for (let index = 0; index < arg.length; index++) {
resultArray[index] = dbGet(type, arg[index]); resultArray[index] = dbGet(type, arg[index]);
// Add song info if id is given
if (typeof resultArray[index].song !== 'undefined') {
for (let song = 0; song < resultArray[index].song.length; song++) {
resultArray[index].song[song] = dbGet('song', resultArray[index].song[song]);
}
}
// Add artist info if id is given
if (typeof resultArray[index].artist !== 'undefined') {
for (let artist = 0; artist < resultArray[index].artist.length; artist++) {
resultArray[index].artist[artist] = dbGet('artist', resultArray[index].artist[artist]);
}
}
// Add album info if id is given
if (typeof resultArray[index].album !== 'undefined') {
for (let album = 0; album < resultArray[index].album.length; album++) {
resultArray[index].album[album] = dbGet('album', resultArray[index].album[album]);
}
}
} }
// Print result // Print result
res.json({ result: resultArray }); res.json({ result: resultArray });
} }
@ -186,13 +162,14 @@ router.get('/search/:query', (req, res, _next) => {
}); });
}); });
router.use(urlParse.urlencoded({extended : true})); router.use(urlParse.urlencoded({extended : true}));
// Analyse route to scan .mp3 ID3 tags // Analyse route to scan .mp3 ID3 tags
router.get('/analyse/*', (req, res, _next) => { router.get('/analyse/*', (req, res, _next) => {
let file = '/' + req.params[0]; let file = req.params[0];
if (fs.existsSync(file)) { if (fs.existsSync(file)) {
var result = id3.read(file); var result = id3.read(file);
if (result.image.imageBuffer) { if (typeof result.image.imageBuffer !== 'undefined') {
result.image.base64 = result.image.imageBuffer.toString('base64'); result.image.base64 = result.image.imageBuffer.toString('base64');
} }
} else { } else {