From d595de57862b2a3564f32c5b8a4ccb7d55d0c96a Mon Sep 17 00:00:00 2001 From: Isaac Grynsztein Date: Sat, 15 Feb 2020 02:11:21 -0500 Subject: [PATCH] added functions to get info on a downloaded (or downloading) file bug fixed where videos with quotations would not properly stream --- backend/app.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/backend/app.js b/backend/app.js index 70a18d7..c7a836f 100644 --- a/backend/app.js +++ b/backend/app.js @@ -194,6 +194,40 @@ function deleteVideoFile(name) { fs.unlinkSync(jsonPath); } +function getAudioInfos(fileNames) { + let result = []; + for (let i = 0; i < fileNames.length; i++) { + let fileName = fileNames[i]; + let fileLocation = audioFolderPath+fileName+'.mp3.info.json'; + if (fs.existsSync(fileLocation)) { + let data = fs.readFileSync(fileLocation); + try { + result.push(JSON.parse(data)); + } catch { + console.log(`ERROR: Could not find info for file ${fileName}.mp3`); + } + } + } + return result; +} + +function getVideoInfos(fileNames) { + let result = []; + for (let i = 0; i < fileNames.length; i++) { + let fileName = fileNames[i]; + let fileLocation = videoFolderPath+fileName+'.info.json'; + if (fs.existsSync(fileLocation)) { + let data = fs.readFileSync(fileLocation); + try { + result.push(JSON.parse(data)); + } catch { + console.log(`ERROR: Could not find info for file ${fileName}.mp4`); + } + } + } + return result; +} + app.post('/tomp3', function(req, res) { var url = req.body.url; var date = Date.now(); @@ -490,7 +524,8 @@ app.get('/video/:id', function(req , res){ app.get('/audio/:id', function(req , res){ var head; - const path = "audio/" + req.params.id; + let path = "audio/" + req.params.id; + path = path.replace(/\"/g, '\''); const stat = fs.statSync(path) const fileSize = stat.size const range = req.headers.range @@ -519,7 +554,23 @@ app.get('/audio/:id', function(req , res){ fs.createReadStream(path).pipe(res) } }); - + + + app.post('/getVideoInfos', function(req, res) { + let fileNames = req.body.fileNames; + let type = req.body.type; + let result = null; + if (type === 'audio') { + result = getAudioInfos(fileNames) + } else if (type === 'video') { + result = getVideoInfos(fileNames); + } + res.send({ + result: result, + success: !!result + }) +}); +