Browse Source

Fixed videos, watch and icons to work with express routes.

pull/6/head
jrtechs 5 years ago
parent
commit
580661951f
8 changed files with 294 additions and 255 deletions
  1. +10
    -0
      configManager.js
  2. +45
    -0
      routes/icon.js
  3. +4
    -4
      routes/index.js
  4. +54
    -0
      routes/video.js
  5. +47
    -0
      routes/videos.js
  6. +10
    -1
      routes/watch.js
  7. +124
    -249
      server.js
  8. +0
    -1
      user.js

+ 10
- 0
configManager.js View File

@ -15,6 +15,16 @@ module.exports=
fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// console.log(config);
// console.log("Config Updated");
},
getRootDirectory: function()
{
return "/home/jeff/work/aaSchool/Algo/online Lectures/";
},
getServerURL: function()
{
return "http://localhost:5000";
}
};

+ 45
- 0
routes/icon.js View File

@ -1,10 +1,55 @@
const routes = require('express').Router();
const utils = require("../utils");
const fs = require('fs');
function isPublicVideo(videoURL)
{
return false;
}
routes.get('/', (request, result) =>
{
try
{
const videoID = request.query.v;
const splitArray = videoID.split('/');
const name = splitArray[splitArray.length -1] + ".png";
console.log(name);
var file="";
if(!isPublicVideo(videoID))
{
if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER)
{
file = fs.readFileSync("./img/private/" + name);
}
else
{
throw "Not logged in";
}
}
else
{
file = fs.readFileSync("./img/public/" + name);
}
result.writeHead(200, {'Content-Type': 'image/png',
'Vary': 'Accept-Encoding'});
result.write(file);
result.end();
}
catch(error)
{
result.writeHead(404, {'Content-Type': 'text/html',
'Vary': 'Accept-Encoding'});
result.write("Nada");
result.end();
}
});
module.exports = routes;

+ 4
- 4
routes/index.js View File

@ -1,16 +1,16 @@
const routes = require('express').Router();
const icon = require('./icon');
routes.get('/icon', icon);
routes.use('/icon', icon);
const video = require('./video');
routes.get('/video', video);
routes.use('/video', video);
const videos = require('./videos');
routes.get('/videos', videos);
routes.use('/videos', videos);
const watch = require('./watch');
routes.get('/watch', watch);
routes.use('/watch', watch);
const user = require('./user');
routes.use('/user', user);

+ 54
- 0
routes/video.js View File

@ -1,10 +1,64 @@
const routes = require('express').Router();
const utils = require("../utils");
const userUtils = require("../user");
const configManager = require("../configManager");
const fs = require('fs');
routes.get('/', (request, result) =>
{
if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER ||
userUtils.isValidAPI(request.query.api))
{
const rootDir = configManager.getRootDirectory();
var videoID = request.query.v;
const path = rootDir + videoID;
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = request.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(path, {start, end});
const head =
{
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
result.writeHead(206, head);
file.pipe(result);
}
else
{
const head =
{
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
result.writeHead(200, head);
fs.createReadStream(path).pipe(result);
}
}
else
{
console.log("invalid attempt to view video");
result.status(401);
result.send('None shall pass');
}
});
module.exports = routes;

+ 47
- 0
routes/videos.js View File

@ -2,6 +2,53 @@ const routes = require('express').Router();
const utils = require("../utils");
const recursive = require('../recursiveTraversal');
const configManager = require("../configManager");
const filepreview = require('filepreview');
const fs = require('fs');
var videos = null;
function getVideosTemplateInformation(templateContext, request)
{
if(videos === null)
{
const rootDir = configManager.getRootDirectory();
videos = [];
return new Promise(function(resolve, reject)
{
recursive(rootDir, function (err, files)
{
console.log(files);
files.forEach(file =>
{
var splitArray = file.split('/');
var name = splitArray[splitArray.length -1];
const icon = 'img/private/' + name + ".png";
if (!fs.existsSync(icon))
{
filepreview.generate(file, icon, function(error) {
if (error) {
return console.log(error);
}
console.log('File preview is located ' + icon);
});
}
videos.push({name: file.replace(rootDir, ''), length: "n/a"});
});
templateContext.videos = videos;
resolve();
});
})
}
else
{
templateContext.videos = videos;
}
}
routes.get('/', (request, result) =>
{

+ 10
- 1
routes/watch.js View File

@ -2,9 +2,18 @@ const routes = require('express').Router();
const utils = require("../utils");
const configManager = require("../configManager");
function getVideoTemplateInfo(templateContext, request)
{
templateContext.api = request.session.API;
templateContext.serverURL = configManager.getServerURL();
templateContext.videoURL = request.query.v.split(" ").join("%20");
}
routes.get('/', (request, result) =>
{
utils.renderHTML(req, res, "watch.html", getVideoTemplateInfo)
utils.renderHTML(request, result, "watch.html", getVideoTemplateInfo)
});
module.exports = routes;

+ 124
- 249
server.js View File

@ -10,9 +10,9 @@ const userUtils = require('./user.js');
const configLoader = require('./configManager.js');
const recursive = require('./recursiveTraversal');
const filepreview = require('filepreview');
// const recursive = require('./recursiveTraversal');
//
// const filepreview = require('filepreview');
const fs = require('fs');
@ -42,9 +42,9 @@ app.use('/', routes);
// /** Template engine */
// const whiskers = require('whiskers');
var rootDir = '/home/jeff/work/aaSchool/Algo/online Lectures/';
// var rootDir = '/home/jeff/work/aaSchool/Algo/online Lectures/';
var serverURL = "http://localhost:5000";
// var serverURL = "http://localhost:5000";
// function fetchInTemplate(templateContext, templateKey, filename)
// {
@ -102,277 +102,152 @@ app.use(express.static('img'));
var videos = null;
function getVideosTemplateInformation(templateContext, request)
{
if(videos === null)
{
videos = [];
return new Promise(function(resolve, reject)
{
recursive(rootDir, function (err, files)
{
console.log(files);
files.forEach(file =>
{
var splitArray = file.split('/');
var name = splitArray[splitArray.length -1];
const icon = 'img/private/' + name + ".png";
if (!fs.existsSync(icon))
{
filepreview.generate(file, icon, function(error) {
if (error) {
return console.log(error);
}
console.log('File preview is located ' + icon);
});
}
videos.push({name: file.replace(rootDir, ''), length: "n/a"});
});
templateContext.videos = videos;
resolve();
});
})
}
else
{
templateContext.videos = videos;
}
}
function getVideoTemplateInfo(templateContext, request)
{
templateContext.api = request.session.API;
templateContext.serverURL = serverURL;
templateContext.videoURL = request.query.v.split(" ").join("%20");
}
// app.get('/videos', (req, res) => renderHTML(req, res, "videos.html", getVideosTemplateInformation));
// app.get('/watch', (req, res) => renderHTML(req, res, "watch.html", getVideoTemplateInfo));
function isPublicVideo(videoURL)
{
return false;
}
app.get('/icon/', function(request, result)
{
try
{
const videoID = request.query.v;
const splitArray = videoID.split('/');
const name = splitArray[splitArray.length -1] + ".png";
var file="";
if(!isPublicVideo(videoID))
{
if(checkPrivilege(request) >= PRIVILEGE.MEMBER)
{
file = fs.readFileSync("img/private/" + name);
}
else
{
throw "Not logged in";
}
}
else
{
file = fs.readFileSync("img/public/" + name);
}
result.writeHead(200, {'Content-Type': 'image/png',
'Vary': 'Accept-Encoding'});
result.write(file);
result.end();
}
catch(error)
{
result.writeHead(404, {'Content-Type': 'text/html',
'Vary': 'Accept-Encoding'});
result.write("Nada");
result.end();
}
});
app.get('/video/', function(request, result)
{
if(checkPrivilege(request) >= PRIVILEGE.MEMBER || userUtils.isValidAPI(request.query.api, config))
{
var videoID = request.query.v;
const path = rootDir + videoID;
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = request.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(path, {start, end});
const head =
{
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
result.writeHead(206, head);
file.pipe(result);
}
else
{
const head =
{
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
result.writeHead(200, head);
fs.createReadStream(path).pipe(result);
}
}
else
{
console.log("invalid attempt to view video");
result.status(401);
result.send('None shall pass');
}
});
// app.post('/revokeAPI', function(request, result)
// var videos = null;
//
// function getVideosTemplateInformation(templateContext, request)
// {
// if(checkPrivilege(request) === PRIVILEGE.ADMIN)
// if(videos === null)
// {
// userUtils.revokeAPI(request.body.username, config);
// request.session.API = userUtils.getAPIKEY(request.session.username, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// videos = [];
// return new Promise(function(resolve, reject)
// {
// recursive(rootDir, function (err, files)
// {
// console.log(files);
// files.forEach(file =>
// {
// var splitArray = file.split('/');
// var name = splitArray[splitArray.length -1];
// const icon = 'img/private/' + name + ".png";
// if (!fs.existsSync(icon))
// {
// filepreview.generate(file, icon, function(error) {
// if (error) {
// return console.log(error);
// }
// console.log('File preview is located ' + icon);
// });
// }
// videos.push({name: file.replace(rootDir, ''), length: "n/a"});
// });
// templateContext.videos = videos;
// resolve();
// });
// })
// }
// else if (checkPrivilege(request) === PRIVILEGE.MEMBER)
// else
// {
// userUtils.revokeAPI(request.session.username, config);
// request.session.API = userUtils.getAPIKEY(request.session.username, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// templateContext.videos = videos;
// }
// result.redirect('/users');
// });
// }
// app.post('/addUser', function(request, result)
// function getVideoTemplateInfo(templateContext, request)
// {
// if(checkPrivilege(request) === PRIVILEGE.ADMIN)
// {
// console.log(request.body);
// var admin = false;
// if(request.body.admin === 'on')
// admin = true;
// userUtils.addUser(request.body.username, request.body.password,admin, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// result.redirect('/users');
// }
// else
// {
// result.status(401);
// result.send('None shall pass');
// }
// });
// templateContext.api = request.session.API;
// templateContext.serverURL = serverURL;
// templateContext.videoURL = request.query.v.split(" ").join("%20");
// }
// app.get('/videos', (req, res) => renderHTML(req, res, "videos.html", getVideosTemplateInformation));
// app.get('/watch', (req, res) => renderHTML(req, res, "watch.html", getVideoTemplateInfo));
// app.post('/edituser', function(request, result)
// {
// if(checkPrivilege(request) === PRIVILEGE.ADMIN)
// {
// var admin = false;
// if(request.body.admin === 'on')
// admin = true;
// userUtils.editUser(request.body.id, request.body.username, request.body.password,admin, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// result.redirect('/users');
// }
// else
// {
// result.status(401);
// result.send('None shall pass');
// }
// });
// function isPublicVideo(videoURL)
// {
// return false;
// }
// app.post('/updateUser', function(request, result)
//
// app.get('/icon/', function(request, result)
// {
// if(checkPrivilege(request) >= PRIVILEGE.MEMBER)
// try
// {
// console.log(request.session.userID);
// var admin = false;
// userUtils.editUser(request.session.userID, request.body.username, request.body.password,admin, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// result.redirect('/users');
// const videoID = request.query.v;
//
// const splitArray = videoID.split('/');
// const name = splitArray[splitArray.length -1] + ".png";
//
// var file="";
//
// if(!isPublicVideo(videoID))
// {
// if(checkPrivilege(request) >= PRIVILEGE.MEMBER)
// {
// file = fs.readFileSync("img/private/" + name);
// }
// else
// {
// throw "Not logged in";
// }
// }
// else
// {
// file = fs.readFileSync("img/public/" + name);
// }
//
// result.writeHead(200, {'Content-Type': 'image/png',
// 'Vary': 'Accept-Encoding'});
// result.write(file);
// result.end();
// }
// else
// catch(error)
// {
// result.status(401);
// result.send('None shall pass');
// result.writeHead(404, {'Content-Type': 'text/html',
// 'Vary': 'Accept-Encoding'});
// result.write("Nada");
// result.end();
// }
// });
// const PRIVILEGE = {NOBODY: 0, MEMBER: 1, ADMIN: 2};
// const checkPrivilege = function(request)
// {
// if(request.session.login !== true)
// return PRIVILEGE.NOBODY;
// else if(request.session.admin === true)
// return PRIVILEGE.ADMIN;
// return PRIVILEGE.MEMBER;
// };
// app.post('/removeuser', function(request, result)
// app.get('/video/', function(request, result)
// {
// if(checkPrivilege(request) === PRIVILEGE.ADMIN)
// if(checkPrivilege(request) >= PRIVILEGE.MEMBER || userUtils.isValidAPI(request.query.api, config))
// {
// userUtils.removeUser(request.body.id, config);
// fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// result.redirect('/users');
// var videoID = request.query.v;
// const path = rootDir + videoID;
// const stat = fs.statSync(path);
// const fileSize = stat.size;
// const range = request.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(path, {start, end});
// const head =
// {
// 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
// 'Accept-Ranges': 'bytes',
// 'Content-Length': chunksize,
// 'Content-Type': 'video/mp4',
// };
// result.writeHead(206, head);
// file.pipe(result);
// }
// else
// {
// const head =
// {
// 'Content-Length': fileSize,
// 'Content-Type': 'video/mp4',
// };
//
// result.writeHead(200, head);
// fs.createReadStream(path).pipe(result);
// }
// }
// else
// {
// console.log("invalid attempt to view video");
// result.status(401);
// result.send('None shall pass');
// }
// });
// app.post('/logout', function(request, result)
// {
// request.session.login = false;
// request.session.admin = false;
// result.redirect('/');
// });
// app.post('/login', function(request, result)
// {
// if(userUtils.checkLogin(request.body.username, request.body.password, config))
// {
// request.session.login = true;
// request.session.username = request.body.username;
// request.session.userID = userUtils.getID(request.body.username, config);
// request.session.API = userUtils.getAPIKEY(request.body.username, config);
// if(userUtils.isAdmin(request.body.username, config))
// {
// request.session.admin = true;
// }
// }
// result.redirect('/');
// });
app.listen(configLoader.getConfiguration().port, () => console.log(`App listening on port ${configLoader.getConfiguration().port}!`));

+ 0
- 1
user.js View File

@ -116,7 +116,6 @@ module.exports =
{
users[index].api = generateRandomAPIKey();
}
console.log(users);
configManager.getConfiguration().users = users;
configManager.syncToDisk();
},

Loading…
Cancel
Save