diff --git a/database/albums.json b/database/albums.json new file mode 100644 index 0000000..ae78b7f --- /dev/null +++ b/database/albums.json @@ -0,0 +1,11 @@ +[ + { + "id": 0, + "name": "The Razors Edge", + "artist": 0, + "image": "ACDC/Razorsedge.jpg", + "songs": [ + 0, 1 + ] + } +] \ No newline at end of file diff --git a/database/artists.json b/database/artists.json new file mode 100644 index 0000000..30ddb6c --- /dev/null +++ b/database/artists.json @@ -0,0 +1,12 @@ +[ + { + "id": 0, + "name": "AC/DC", + "albums": [ + 0 + ], + "songs": [ + 0, 1 + ] + } +] \ No newline at end of file diff --git a/database/songs.json b/database/songs.json new file mode 100644 index 0000000..31a7e0d --- /dev/null +++ b/database/songs.json @@ -0,0 +1,18 @@ +[ + { + "id": 0, + "name": "Thunderstruck", + "artist": 0, + "album": 0, + "path": "ACDC/ACDC - Thunderstruck.mp3", + "liked": true + }, + { + "id": 1, + "name": "Moneytalks", + "artist": 0, + "album": 0, + "path": "ACDC/ACDC - Moneytalks.mp3", + "liked": false + } +] \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 36901ce..7971670 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,5 +1,7 @@ var express = require('express'); var YoutubeMp3Downloader = require("youtube-mp3-downloader"); +var fs = require('fs'); +var path = require('path'); var router = express.Router(); var YD = new YoutubeMp3Downloader({ @@ -9,13 +11,14 @@ var YD = new YoutubeMp3Downloader({ "queueParallelism": 1, "progressTimeout": 2000 }); +const rootdir = path.join(__dirname, '/../'); /* GET home page. */ -router.get('/', (req, res, next) => { +router.get('/', (_req, res, _next) => { res.render('index', { title: 'Express' }); }); -router.get('/download', (req, res, next) => { +router.get('/download', (req, res, _next) => { YD.download(req.query.url); YD.on('finished', (err, data) => { @@ -25,6 +28,103 @@ router.get('/download', (req, res, next) => { res.json({ result: data }); } }); -}) +}); + +router.get('/get/:type/:id', (req, res, _next) => { + const type = req.params.type; + const id = req.params.id; + let db = {}; + + if (type === 'song') { + db = require(rootdir + '/songs.json'); + } else if (type === 'album') { + db = require(rootdir + '/albums.json'); + } else if (type === 'artist') { + db = require(rootdir + '/artists.json'); + } + + const arg = id.split(','); + + if (arg.length > 1) { + let result = []; + arg.forEach(element => { + result.push(db[Number(element)]); + }); + res.json({ result: result }); + } else { + res.json({ result: db[id] }); + } + +}); + +router.get('/play/:track', (req, res, _next) => { + var key = req.params.track; + + var music = rootdir + "/music/" + key + ".mp3"; + + var stat = fs.statSync(music); + range = req.headers.range; + var readStream; + + if (range !== undefined) { + var parts = range.replace(/bytes=/, "").split("-"); + + var partial_start = parts[0]; + var partial_end = parts[1]; + + if ( + (isNaN(partial_start) && partial_start.length > 1) || + (isNaN(partial_end) && partial_end.length > 1) + ) { + return res.sendStatus(500); //ERR_INCOMPLETE_CHUNKED_ENCODING + } + + var start = parseInt(partial_start, 10); + var end = partial_end ? parseInt(partial_end, 10) : stat.size - 1; + var content_length = end - start + 1; + + res.status(206).header({ + "Content-Type": "audio/mpeg", + "Content-Length": content_length, + "Content-Range": "bytes " + start + "-" + end + "/" + stat.size + }); + + readStream = fs.createReadStream(music, { start: start, end: end }); + } else { + res.header({ + "Content-Type": "audio/mpeg", + "Content-Length": stat.size + }); + readStream = fs.createReadStream(music); + } + readStream.pipe(res); +}); + +router.get('/image/:path', (req, res, _next) => { + const path = '/music/' + req.params.path + + res.sendFile(path, { root: rootdir }); +}); + +router.get('/search/:query', (req, res, _next) => { + const query = req.params.query; + let result = []; + + fs.readdir(rootdir + '/database/', (err, files) => { + files.forEach(file => { + + const content = require(rootdir + '/database/' + file); + + content.forEach(element => { + if (element.name.match(query)) { + result.push(element); + } + }); + + }); + + res.json({result: result}); + }); +}); module.exports = router;