🤖 Added insert, update & info route.

And fixed some other minor bugs.
This commit is contained in:
corner 2019-10-06 16:27:25 +02:00
parent 7deec267de
commit 03f52c0594
1 changed files with 29 additions and 3 deletions

View File

@ -11,6 +11,7 @@ var router = express.Router();
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Cache-Control', 'no-cache');
next();
});
const rootdir = path.join(__dirname, '/../');
@ -45,15 +46,22 @@ router.get('/download/:url', (req, res, _next) => {
});
});
router.get('/info/:id', (req, res, _next) => {
ytdl.getBasicInfo(req.params.id, (_err, info) => {
res.json({ result: info });
})
});
router.get('/downloadqueue', (req, res, _next) => {
res.json({ result: downloadQueue });
})
function dbGet(type, id) {
JSON.parse(fs.readFileSync(rootdir + "database/albums.json").toString());
let db = {
song: require(rootdir + 'database/songs.json'),
album: require(rootdir + "database/albums.json"),
artist: require(rootdir + "database/artists.json")
song: JSON.parse(fs.readFileSync(rootdir + "database/songs.json").toString()),
album: JSON.parse(fs.readFileSync(rootdir + "database/albums.json").toString()),
artist: JSON.parse(fs.readFileSync(rootdir + "database/artists.json").toString())
};
let result;
@ -198,4 +206,22 @@ router.get('/duration/:file', (req, res, _next) => {
getAudioDurationInSeconds(__dirname + '/../music/' + req.params.file).then(duration => res.json({result: duration}));
})
router.get('/update/:database/:index/:field/:value', (req, res, _next) => {
const path = rootdir + "database/" + req.params.database + ".json";
const db = require(path);
db[req.params.index][req.params.field] = req.params.value;
fs.writeFileSync(path, JSON.stringify(db));
res.json({ result: true });
});
router.get('/insert/:database/', (req, res, _next) => {
const path = rootdir + "database/" + req.params.database + ".json";
const db = JSON.parse(fs.readFileSync(path).toString());
const index = db.push({}) - 1;
db[index].id = index;
fs.writeFileSync(path, JSON.stringify(db));
res.json({ result: db[index] });
});
module.exports = router;