From b5c20f71d2b22de550087bb4ddfb0f771f221c1b Mon Sep 17 00:00:00 2001 From: "CHIEFSOFT\\ameye" Date: Tue, 26 Mar 2024 11:56:50 -0400 Subject: [PATCH] fix load --- index.js | 82 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index ec9e598..1465664 100644 --- a/index.js +++ b/index.js @@ -68,51 +68,57 @@ app.get('/videos/:filename', (req, res)=>{ if ( result.rowCount === 1){ findFilename = result.rows[0].filename; } + + + //const filePath = videoFileMap[fileName] + const completeFile = process.env.COMMON_MEDIA_PATH + findFilename; + if(!completeFile){ + console.log("Finding File Not Found ", completeFile); + return res.status(404).send('File not found') + } + + console.log("Finding File Found ", completeFile); + + + + const stat = fs.statSync(completeFile); + const fileSize = stat.size; + const range = req.headers.range; + + if(range){ + const parts = range.replace(/bytes=/, '').split('-') + const start = parseInt(parts[0], 10); + const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1; + + const chunksize = end - start + 1; + const file = fs.createReadStream(completeFile, {start, end}); + const head = { + 'Content-Range': `bytes ${start}-${end}/${fileSize}`, + 'Accept-Ranges': 'bytes', + 'Content-Length': chunksize, + 'Content-Type': 'video/mp4' + }; + res.writeHead(206, head); + file.pipe(res); + } + else{ + const head = { + 'Content-Length': fileSize, + 'Content-Type': 'video/mp4' + }; + res.writeHead(200, head); + fs.createReadStream(completeFile).pipe(res) + } + + + } catch (e) { console.log("ERR->",e.message); } }); - //const filePath = videoFileMap[fileName] - const completeFile = process.env.COMMON_MEDIA_PATH + findFilename; - if(!completeFile){ - console.log("Finding File Not Found ", completeFile); - return res.status(404).send('File not found') - } - console.log("Finding File Found ", completeFile); - - - - const stat = fs.statSync(completeFile); - const fileSize = stat.size; - const range = req.headers.range; - - if(range){ - const parts = range.replace(/bytes=/, '').split('-') - const start = parseInt(parts[0], 10); - const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1; - - const chunksize = end - start + 1; - const file = fs.createReadStream(completeFile, {start, end}); - const head = { - 'Content-Range': `bytes ${start}-${end}/${fileSize}`, - 'Accept-Ranges': 'bytes', - 'Content-Length': chunksize, - 'Content-Type': 'video/mp4' - }; - res.writeHead(206, head); - file.pipe(res); - } - else{ - const head = { - 'Content-Length': fileSize, - 'Content-Type': 'video/mp4' - }; - res.writeHead(200, head); - fs.createReadStream(completeFile).pipe(res) - } }) app.listen(3036, ()=>{