diff --git a/configManager.js b/configManager.js index df2500e..c0c0597 100644 --- a/configManager.js +++ b/configManager.js @@ -17,17 +17,33 @@ module.exports= getRootDirectory: function() { - return "/home/jeff/public/Shows/Rick And Morty/Season 1"; + return config.privateDir; }, getPublicDirectory: function() { - return "/home/jeff/work/aaSchool/Algo/online Lectures/"; + return config.publicDir; }, getServerURL: function() { - return "http://localhost:5000"; + return config.serverURL; + }, + + updateSystem: function(host, publicDir, privateDir) + { + config.serverURL = host; + config.privateDir = privateDir; + config.publicDir = publicDir; + + module.exports.syncToDisk(); + }, + + getUserCount: function() + { + return (config.hasOwnProperty('users')) ? config.users.length : 0; } + + }; \ No newline at end of file diff --git a/html/header.html b/html/header.html index a40c60a..9ca05a3 100644 --- a/html/header.html +++ b/html/header.html @@ -40,6 +40,9 @@ + diff --git a/html/system.html b/html/system.html new file mode 100644 index 0000000..c9e5a3a --- /dev/null +++ b/html/system.html @@ -0,0 +1,63 @@ +

System Controls

+
+
+
+
+
+

System Status

+
+
+

Public Videos Indexed: {publicVideoCount}

+

Private Videos Indexed: {privateVideoCount}

+

Users: {userCount}

+
+
+
+ +
+
+
+

Update System Settings

+
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+ +
+
+
+

Re-Index Video

+
+
+
+
+ +
+
+
+
+
+
\ No newline at end of file diff --git a/html/videos.html b/html/videos.html index 6ef6dcf..3c20dd0 100644 --- a/html/videos.html +++ b/html/videos.html @@ -14,7 +14,7 @@

{video.name}

-
+
Icon for {video.name} @@ -23,7 +23,7 @@
{/for} {for video in public} -
+

{video.name}

diff --git a/routes/index.js b/routes/index.js index 4d37cb6..4389ee8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -15,6 +15,8 @@ routes.use('/watch', watch); const user = require('./user'); routes.use('/user', user); +const syss = require('./system'); +routes.use('/system', syss); const utils = require("../utils"); diff --git a/routes/system/index.js b/routes/system/index.js new file mode 100644 index 0000000..f691faf --- /dev/null +++ b/routes/system/index.js @@ -0,0 +1,42 @@ +const routes = require('express').Router(); + +const utils = require("../../utils"); + +const indexVideos = require('./indexVideos'); +routes.use('/indexVideos', indexVideos); + +const updateSystem = require('./updateSystem'); +routes.use('/updateSystem', updateSystem); + +const configLoader = require("../../configManager"); + +const videoManager = require("../../videoManager"); + +function getSystemInformation(templateContext, request) +{ + templateContext.serverURL = configLoader.getServerURL(); + templateContext.privateDir = configLoader.getRootDirectory(); + templateContext.publicDir = configLoader.getPublicDirectory(); + templateContext.publicVideoCount = videoManager.getPublicVideoCount(); + templateContext.privateVideoCount = videoManager.getPrivateVideoCount(); + templateContext.userCount = configLoader.getUserCount(); +} + +routes.get('/', (request, result) => +{ + if(utils.checkPrivilege(request) >= utils.PRIVILEGE.ADMIN) + { + utils.renderHTML(request, result, "system.html", getSystemInformation); + } + else + { + utils.printError(result, "You need to be logged in"); + } +}); + +routes.get('*', (request, result) => +{ + utils.printError(result, "Page not found."); +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/system/indexVideos.js b/routes/system/indexVideos.js new file mode 100644 index 0000000..468ebba --- /dev/null +++ b/routes/system/indexVideos.js @@ -0,0 +1,20 @@ +const routes = require('express').Router(); + +const utils = require("../../utils"); + +const videoManager = require("../../videoManager"); + +routes.post('/', (request, result) => +{ + if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN) + { + videoManager.reIndexVideos(); + result.redirect('/system'); + } + else + { + utils.printError(result, "You need to be logged in"); + } +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/system/updateSystem.js b/routes/system/updateSystem.js new file mode 100644 index 0000000..a89a97c --- /dev/null +++ b/routes/system/updateSystem.js @@ -0,0 +1,22 @@ +const routes = require('express').Router(); + +const utils = require("../../utils"); + +const configManager = require("../../configManager"); + +routes.post('/', (request, result) => +{ + if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN) + { + configManager.updateSystem(request.body.baseURL, + request.body.publicDirectory, + request.body.privateDirectory); + result.redirect('/system'); + } + else + { + utils.printError(result, "You need to be logged in"); + } +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/user/index.js b/routes/user/index.js index 64b5ea4..55a073c 100644 --- a/routes/user/index.js +++ b/routes/user/index.js @@ -48,7 +48,11 @@ routes.get('/', (request, result) => { utils.printError(result, "You need to be logged in"); } +}); +routes.get('*', (request, result) => +{ + utils.printError(result, "Page not found."); }); module.exports = routes; \ No newline at end of file diff --git a/videoManager.js b/videoManager.js index 37cf0b3..de1e37f 100644 --- a/videoManager.js +++ b/videoManager.js @@ -10,6 +10,49 @@ var privateVideos = null; var publicVideos = null; +function createIndex(filename, videos, templateKey) +{ + return new Promise(function(resolve, reject) + { + console.log("Generating icon for " + filename); + var splitArray = filename.split('/'); + var name = splitArray[splitArray.length -1]; + const icon = './icon/' + templateKey + '/' + name + ".png"; + if (!fs.existsSync(icon)) + { + var options = { + width: 200, + quality: 50, + previewTime: '00:05:00.000' + }; + + filepreview.generate(filename, icon, options, function (error) + { + if (error) + { + resolve(); + } + console.log('File preview is located ' + icon); + resolve(); + }); + } + else + { + resolve(); + } + }) +} + +async function runTasksSync(files, videos, templateKey) +{ + for(var file of files) + { + await createIndex(file, videos, templateKey); + } +} + + + module.exports = { indexVideos: function(rootDir, videos, templateKey) @@ -20,26 +63,10 @@ module.exports = { files.forEach(file => { - var splitArray = file.split('/'); - var name = splitArray[splitArray.length -1]; - const icon = './icon/' + templateKey + '/' + name + ".png"; - if (!fs.existsSync(icon)) - { - var options = { - width: 200, - quality: 50, - previewTime: '00:05:00.000' - }; - - filepreview.generate(file, icon, options,function(error) { - if (error) { - return console.log(error); - } - console.log('File preview is located ' + icon); - }); - } videos.push({name: file.replace(rootDir, '')}); }); + runTasksSync(files.splice(0, files.length/2), videos, templateKey); + runTasksSync(files.splice(files.length/2, files.length), videos, templateKey); resolve(); }); }).catch(function(error) @@ -121,5 +148,23 @@ module.exports = } return false; } + }, + + reIndexVideos: function() + { + publicVideos = []; + privateVideos = []; + module.exports.indexVideos(configManager.getPublicDirectory(), publicVideos, "public"); + module.exports.indexVideos(configManager.getRootDirectory(), privateVideos, "private"); + }, + + getPublicVideoCount: function() + { + return (publicVideos === null) ? 0: publicVideos.length; + }, + + getPrivateVideoCount: function() + { + return (privateVideos === null) ? 0: privateVideos.length; } }; \ No newline at end of file