162 lines
5.9 KiB
JavaScript
162 lines
5.9 KiB
JavaScript
const express = require('express')
|
||
const fs = require('fs')
|
||
const db = require('./app/db')
|
||
const app = express()
|
||
const cors = require("cors");
|
||
const multer = require('multer')
|
||
|
||
const videoFileMap2={
|
||
'cdn':'videos/v1.mp4',
|
||
'generate-pass':'videos/v2.mp4',
|
||
'get-post':'videos/v3.mp4',
|
||
'tt1':'/opt/wrenchboard/TEST/COMMON/MEDIA/What Is Single Sign-on (SSO)? How It Works-(1080p25).mp4',
|
||
'tt2':'/opt/wrenchboard/TEST/COMMON/MEDIA/What is RabbitMQ?-(1080p30).mp4',
|
||
'tt3':'/opt/wrenchboard/TEST/COMMON/MEDIA/What is CORS?-(1080p50).mp4',
|
||
'tt4':'/opt/wrenchboard/TEST/COMMON/MEDIA/SAML vs OAuth vs OIDC (explained simply!)-(1080p25).mp4',
|
||
'tt5':'/opt/wrenchboard/TEST/COMMON/MEDIA/Running RabbitMQ Locally with Docker-(1080p60).mp4',
|
||
'tt6':'/opt/wrenchboard/TEST/COMMON/MEDIA/React Proxy | Easiest Fix to CORS Errors-(2160p24).mp4',
|
||
'tt7':'/opt/wrenchboard/TEST/COMMON/MEDIA/RabbitMQ Tutorial - Publisher and Consumer program with example in nodeJS-(686p30).mp4',
|
||
'tt8':'/opt/wrenchboard/TEST/COMMON/MEDIA/RabbitMQ - RPC with NodeJs (request-reply pattern)-(1080p60).mp4',
|
||
'tt9':'/opt/wrenchboard/TEST/COMMON/MEDIA/How to Install RabbitMQ Locally with Docker-(1080p60).mp4',
|
||
}
|
||
|
||
const videoFileMap={
|
||
'cdn':'videos/v1.mp4',
|
||
'generate-pass':'videos/v2.mp4',
|
||
'get-post':'videos/v3.mp4',
|
||
'tt1':'What Is Single Sign-on (SSO)? How It Works-(1080p25).mp4',
|
||
'tt2':'What is RabbitMQ?-(1080p30).mp4',
|
||
'tt3':'What is CORS?-(1080p50).mp4',
|
||
'tt4':'SAML vs OAuth vs OIDC (explained simply!)-(1080p25).mp4',
|
||
'tt5':'Running RabbitMQ Locally with Docker-(1080p60).mp4',
|
||
'tt6':'React Proxy | Easiest Fix to CORS Errors-(2160p24).mp4',
|
||
'tt7':'RabbitMQ Tutorial - Publisher and Consumer program with example in nodeJS-(686p30).mp4',
|
||
'tt8':'RabbitMQ - RPC with NodeJs (request-reply pattern)-(1080p60).mp4',
|
||
'tt9':'How to Install RabbitMQ Locally with Docker-(1080p60).mp4',
|
||
}
|
||
|
||
console.log("Finding File");
|
||
|
||
var corsOptions = {
|
||
origin: 'http://127.0.0.1:3000/',
|
||
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
|
||
}
|
||
|
||
app.use(cors(corsOptions));
|
||
// ,cors(corsOptions)
|
||
|
||
const storage = multer.diskStorage({
|
||
destination: function(req, file, cb) {
|
||
/*
|
||
let uid = req.body.uid;
|
||
let job_uid = req.body.job_uid;
|
||
console.log(" req.body -> ", req.body)
|
||
console.log(" req.body.job_uid -> ", job_uid)
|
||
|
||
const fs = require('fs');
|
||
const dir = process.env.COMMON_TASKFILE_PATH + "/DUMMY/" + uid;
|
||
if (!fs.existsSync(dir)){
|
||
fs.mkdirSync(dir);
|
||
}
|
||
|
||
const dir2 = process.env.COMMON_TASKFILE_PATH + "/DUMMY/" + uid+"/"+job_uid;
|
||
if (!fs.existsSync(dir2)){
|
||
fs.mkdirSync(dir2);
|
||
}
|
||
return cb(null, dir2)
|
||
/opt/wrenchboard/TEST/DUMMY
|
||
process.env.COMMON_TASKFILE_PATH +"/DUMMY"
|
||
*/
|
||
return cb(null, '/opt/wrenchboard/TEST/DUMMY')
|
||
},
|
||
filename: function (req, file, cb) {
|
||
console.log(" AAAA --> req.body.job_uid -> ", req.body.job_uid)
|
||
return cb(null, `${Date.now()}_${file.originalname}`)
|
||
}
|
||
})
|
||
|
||
const upload = multer({storage})
|
||
|
||
app.post('/upload/task', upload.single('file'), (req, res) => {
|
||
console.log(req.body)
|
||
console.log(req.file)
|
||
return res.status(200).send('Completed')
|
||
})
|
||
|
||
|
||
|
||
|
||
app.get('/videos/:filename', (req, res)=>{
|
||
const fileName = req.params.filename;
|
||
let findFilename = 'What Is Single Sign-on (SSO)? How It Works-(1080p25).mp4';
|
||
// var findFilename = '';
|
||
//c35c32bc-9ce4-4ca5-9afc-1ecddfd649f5
|
||
let Qstring = "SELECT * FROM common_media WHERE uid ='"+fileName+"'";
|
||
console.log("Qstring -> ", Qstring);
|
||
db.query(Qstring, function (err, result) {
|
||
try {
|
||
console.log("BEFORE ERR CHECK -> " , result);
|
||
if (err){
|
||
console.log("IN ERR CHECK -> " , err);
|
||
throw err;
|
||
}
|
||
// let resultItem = {
|
||
// "result": result.rows,
|
||
// "total_record": result.rowCount
|
||
// }
|
||
console.log("THIS IS RESULT --> ",result.rows[0].filename);
|
||
console.log("THIS IS RESULT COUNT --> ",result.rowCount);
|
||
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);
|
||
}
|
||
});
|
||
|
||
|
||
|
||
})
|
||
|
||
app.listen(3036, ()=>{
|
||
console.log('server is listening on post 3036')
|
||
}) |