2019-09-17 16:49:23 +02:00
|
|
|
var express = require('express');
|
2019-09-17 17:07:08 +02:00
|
|
|
var YoutubeMp3Downloader = require("youtube-mp3-downloader");
|
2019-09-18 10:51:02 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2019-09-17 17:07:08 +02:00
|
|
|
|
2019-09-17 16:49:23 +02:00
|
|
|
var router = express.Router();
|
2019-09-17 17:07:08 +02:00
|
|
|
var YD = new YoutubeMp3Downloader({
|
|
|
|
"ffmpegPath": __dirname + "/../bin/ffmpeg",
|
|
|
|
"outputPath": __dirname + "/../music",
|
|
|
|
"youtubeVideoQuality": "highest",
|
|
|
|
"queueParallelism": 1,
|
|
|
|
"progressTimeout": 2000
|
|
|
|
});
|
2019-09-18 10:51:02 +02:00
|
|
|
const rootdir = path.join(__dirname, '/../');
|
2019-09-17 16:49:23 +02:00
|
|
|
|
|
|
|
/* GET home page. */
|
2019-09-18 10:51:02 +02:00
|
|
|
router.get('/', (_req, res, _next) => {
|
2019-09-17 16:49:23 +02:00
|
|
|
res.render('index', { title: 'Express' });
|
|
|
|
});
|
|
|
|
|
2019-09-18 10:51:02 +02:00
|
|
|
router.get('/download', (req, res, _next) => {
|
2019-09-17 17:07:08 +02:00
|
|
|
YD.download(req.query.url);
|
|
|
|
|
|
|
|
YD.on('finished', (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({ result: err });
|
|
|
|
} else {
|
|
|
|
res.json({ result: data });
|
|
|
|
}
|
|
|
|
});
|
2019-09-18 10:51:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/get/:type/:id', (req, res, _next) => {
|
|
|
|
const type = req.params.type;
|
|
|
|
const id = req.params.id;
|
|
|
|
let db = {};
|
|
|
|
|
|
|
|
if (type === 'song') {
|
2019-09-19 08:06:21 +02:00
|
|
|
db = require(rootdir + 'database/songs.json');
|
2019-09-18 10:51:02 +02:00
|
|
|
} else if (type === 'album') {
|
2019-09-19 08:06:21 +02:00
|
|
|
db = require(rootdir + "database/albums.json");
|
2019-09-18 10:51:02 +02:00
|
|
|
} else if (type === 'artist') {
|
2019-09-19 08:06:21 +02:00
|
|
|
db = require(rootdir + "database/artists.json");
|
2019-09-18 10:51:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 08:06:21 +02:00
|
|
|
if (id === 'all') {
|
|
|
|
res.json({result: db})
|
2019-09-18 10:51:02 +02:00
|
|
|
} else {
|
2019-09-19 08:06:21 +02:00
|
|
|
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] });
|
|
|
|
}
|
2019-09-18 10:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/play/:track', (req, res, _next) => {
|
|
|
|
var key = req.params.track;
|
|
|
|
|
2019-09-19 08:06:21 +02:00
|
|
|
var music = rootdir + "music/" + key + ".mp3";
|
2019-09-18 10:51:02 +02:00
|
|
|
|
|
|
|
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 = [];
|
|
|
|
|
2019-09-19 08:06:21 +02:00
|
|
|
fs.readdir(rootdir + 'database/', (err, files) => {
|
2019-09-18 10:51:02 +02:00
|
|
|
files.forEach(file => {
|
|
|
|
|
2019-09-19 08:06:21 +02:00
|
|
|
const content = require(rootdir + 'database/' + file);
|
2019-09-18 10:51:02 +02:00
|
|
|
|
|
|
|
content.forEach(element => {
|
|
|
|
if (element.name.match(query)) {
|
2019-09-19 08:06:21 +02:00
|
|
|
element.foundIn = file;
|
2019-09-18 10:51:02 +02:00
|
|
|
result.push(element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
res.json({result: result});
|
|
|
|
});
|
|
|
|
});
|
2019-09-17 17:07:08 +02:00
|
|
|
|
2019-09-17 16:49:23 +02:00
|
|
|
module.exports = router;
|