diff --git a/configManager.js b/configManager.js index 4db79a3..1218f7e 100644 --- a/configManager.js +++ b/configManager.js @@ -20,6 +20,11 @@ module.exports= return "/home/jeff/work/aaSchool/Algo/online Lectures/"; }, + getPublicDirectory: function() + { + return "/home/jeff/work/aaSchool/Algo/online Lectures/"; + }, + getServerURL: function() { return "http://localhost:5000"; diff --git a/html/error.html b/html/error.html new file mode 100644 index 0000000..1549c1f --- /dev/null +++ b/html/error.html @@ -0,0 +1,4 @@ +

{errorMessage}

+

+Page not found +

\ No newline at end of file diff --git a/html/home.html b/html/home.html index 4f5e8fd..aa1beab 100644 --- a/html/home.html +++ b/html/home.html @@ -1,19 +1,24 @@
-
-
-

Profile

-
-
-

Welcome {username}.

-
-
-
- -
+ {if loggedIn} +
+ +
+

Profile

+
+
+

Welcome {username}.


- +
+
+ +
+
+
+
-
+ {else} + {>login} + {/if}
\ No newline at end of file diff --git a/html/mainTemplate.html b/html/mainTemplate.html index c26cb1f..a128c5f 100644 --- a/html/mainTemplate.html +++ b/html/mainTemplate.html @@ -1,12 +1,7 @@ {>header}
- {if loggedIn} - {>main} - {else} - {>login} - {/if} - + {>main}
{>footer} \ No newline at end of file diff --git a/html/videos.html b/html/videos.html index dac14f0..6786e57 100644 --- a/html/videos.html +++ b/html/videos.html @@ -8,7 +8,21 @@
- {for video in videos} + {for video in private} +
+
+
+

{video.name}

+
+
+ + Icon for {video.name} + +
+
+
+ {/for} + {for video in public}
diff --git a/routes/icon.js b/routes/icon.js index dc86d20..1cc0d2a 100644 --- a/routes/icon.js +++ b/routes/icon.js @@ -26,16 +26,17 @@ routes.get('/', (request, result) => { if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) { - file = fs.readFileSync("./img/private/" + name); + file = fs.readFileSync("./icon/private/" + name); } else { + utils.printError(result, "You need to be logged in"); throw "Not logged in"; } } else { - file = fs.readFileSync("./img/public/" + name); + file = fs.readFileSync("./icon/public/" + name); } result.writeHead(200, {'Content-Type': 'image/png', @@ -45,10 +46,8 @@ routes.get('/', (request, result) => } catch(error) { - result.writeHead(404, {'Content-Type': 'text/html', - 'Vary': 'Accept-Encoding'}); - result.write("Nada"); - result.end(); + utils.printError(result, "Invalid Icon"); + console.log(error); } }); diff --git a/routes/user/addUser.js b/routes/user/addUser.js index 8f35a09..0f0e28a 100644 --- a/routes/user/addUser.js +++ b/routes/user/addUser.js @@ -17,8 +17,7 @@ routes.post('/', (request, result) => } else { - result.status(401); - result.send('None shall pass'); + utils.printError(result, "You need to be logged in"); } }); diff --git a/routes/user/edituser.js b/routes/user/edituser.js index 5ae4c28..3146ae9 100644 --- a/routes/user/edituser.js +++ b/routes/user/edituser.js @@ -17,8 +17,7 @@ routes.post('/', (request, result) => } else { - result.status(401); - result.send('None shall pass'); + utils.printError(result, "You need to be logged in"); } }); diff --git a/routes/user/index.js b/routes/user/index.js index 106801e..64b5ea4 100644 --- a/routes/user/index.js +++ b/routes/user/index.js @@ -40,7 +40,15 @@ function getUserInformation(templateContext, request) routes.get('/', (request, result) => { - utils.renderHTML(request, result, "users.html", getUserInformation); + if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) + { + utils.renderHTML(request, result, "users.html", getUserInformation); + } + else + { + utils.printError(result, "You need to be logged in"); + } + }); module.exports = routes; \ No newline at end of file diff --git a/routes/user/removeuser.js b/routes/user/removeuser.js index c04a2ed..69cb961 100644 --- a/routes/user/removeuser.js +++ b/routes/user/removeuser.js @@ -13,8 +13,7 @@ routes.post('/', (request, result) => } else { - result.status(401); - result.send('None shall pass'); + utils.printError(result, "You need to be logged in"); } }); diff --git a/routes/user/revokeAPI.js b/routes/user/revokeAPI.js index 02b50e5..956d205 100644 --- a/routes/user/revokeAPI.js +++ b/routes/user/revokeAPI.js @@ -16,6 +16,10 @@ routes.post('/', (request, result) => userUtils.revokeAPI(request.session.username); request.session.API = userUtils.getAPIKEY(request.session.username); } + else + { + utils.printError(result, "You need to be logged in"); + } result.redirect('/user'); }); diff --git a/routes/user/updateUser.js b/routes/user/updateUser.js index fa177f4..19dc356 100644 --- a/routes/user/updateUser.js +++ b/routes/user/updateUser.js @@ -14,8 +14,7 @@ routes.post('/', (request, result) => } else { - result.status(401); - result.send('None shall pass'); + utils.printError(result, "You need to be logged in"); } }); diff --git a/routes/video.js b/routes/video.js index 45190b8..6140b50 100644 --- a/routes/video.js +++ b/routes/video.js @@ -55,9 +55,7 @@ routes.get('/', (request, result) => } else { - console.log("invalid attempt to view video"); - result.status(401); - result.send('None shall pass'); + utils.printError(result, "You need to be logged in"); } }); diff --git a/routes/videos.js b/routes/videos.js index 15094d4..764da4f 100644 --- a/routes/videos.js +++ b/routes/videos.js @@ -10,13 +10,14 @@ const filepreview = require('filepreview'); const fs = require('fs'); -var videos = null; +var privateVideos = null; -function getVideosTemplateInformation(templateContext, request) +var publicVideos = null; + +function getVideosForTemplate(templateContext, rootDir, templateKey, videos) { if(videos === null) { - const rootDir = configManager.getRootDirectory(); videos = []; return new Promise(function(resolve, reject) { @@ -27,7 +28,7 @@ function getVideosTemplateInformation(templateContext, request) { var splitArray = file.split('/'); var name = splitArray[splitArray.length -1]; - const icon = 'img/private/' + name + ".png"; + const icon = 'img/' + templateKey + '/' + name + ".png"; if (!fs.existsSync(icon)) { filepreview.generate(file, icon, function(error) { @@ -37,19 +38,42 @@ function getVideosTemplateInformation(templateContext, request) console.log('File preview is located ' + icon); }); } - videos.push({name: file.replace(rootDir, ''), length: "n/a"}); + videos.push({name: file.replace(rootDir, '')}); }); - templateContext.videos = videos; + templateContext[templateKey] = videos; resolve(); }); }) } else { - templateContext.videos = videos; + templateContext[templateKey] = videos; } } + +function getVideosTemplateInformation(templateContext, request) +{ + var promises = []; + + const rootDir = configManager.getRootDirectory(); + + const rootPublicDir = configManager.getPublicDirectory(); + + if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) + { + promises.push(getVideosForTemplate(templateContext, rootDir, "private", privateVideos)); + } + else + { + templateContext["private"] = []; + } + + promises.push(getVideosForTemplate(templateContext, rootPublicDir, "public", publicVideos)); + + return Promise.all(promises); +} + routes.get('/', (request, result) => { utils.renderHTML(request, result, "videos.html", getVideosTemplateInformation) diff --git a/utils.js b/utils.js index 860a2bf..117ef2c 100644 --- a/utils.js +++ b/utils.js @@ -28,15 +28,16 @@ module.exports = templateContext.loggedIn = true; if(module.exports.checkPrivilege(request) === PRIVILEGE.ADMIN) templateContext.admin = true; - if(templateDependencyFunction !== null) - prom.push(templateDependencyFunction(templateContext, request)); - prom.push(fetchInTemplate(templateContext, "main","./html/" + templateFile)); } else { prom.push(fetchInTemplate(templateContext, "login","./html/login.html")); } + if(templateDependencyFunction !== null) + prom.push(templateDependencyFunction(templateContext, request)); + prom.push(fetchInTemplate(templateContext, "main","./html/" + templateFile)); + Promise.all(prom).then(function(content) { result.write(whiskers.render(content[0], templateContext)); @@ -59,5 +60,23 @@ module.exports = else if(request.session.admin === true) return module.exports.PRIVILEGE.ADMIN; return module.exports.RIVILEGE.MEMBER; + }, + + printError: function(result, errorMessage) + { + var templateContext = Object(); + var prom = []; + + prom.push(fileIO.getFile("./html/mainTemplate.html")); + prom.push(fetchInTemplate(templateContext, "header", "./html/header.html")); + prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html")); + prom.push(fetchInTemplate(templateContext, "main", "./html/error.html")); + templateContext.errorMessage = errorMessage; + + Promise.all(prom).then(function(content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }); } }; \ No newline at end of file