From d52f0696e56f47146b07b204e16e22284b556cdc Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 9 Feb 2019 15:02:52 -0500 Subject: [PATCH] Updated videos to work with public and private sets. --- configManager.js | 2 +- routes/icon.js | 4 +- routes/video.js | 9 +++- routes/videos.js | 57 ++------------------- videoManager.js | 125 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 58 deletions(-) create mode 100644 videoManager.js diff --git a/configManager.js b/configManager.js index 1218f7e..df2500e 100644 --- a/configManager.js +++ b/configManager.js @@ -17,7 +17,7 @@ module.exports= getRootDirectory: function() { - return "/home/jeff/work/aaSchool/Algo/online Lectures/"; + return "/home/jeff/public/Shows/Rick And Morty/Season 1"; }, getPublicDirectory: function() diff --git a/routes/icon.js b/routes/icon.js index 1cc0d2a..2c438ba 100644 --- a/routes/icon.js +++ b/routes/icon.js @@ -4,6 +4,8 @@ const utils = require("../utils"); const fs = require('fs'); +const videoManager = require('../videoManager'); + function isPublicVideo(videoURL) { return false; @@ -22,7 +24,7 @@ routes.get('/', (request, result) => var file=""; - if(!isPublicVideo(videoID)) + if(!videoManager.isPublicVideo(videoID)) { if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) { diff --git a/routes/video.js b/routes/video.js index 6140b50..fbb323b 100644 --- a/routes/video.js +++ b/routes/video.js @@ -8,14 +8,19 @@ const configManager = require("../configManager"); const fs = require('fs'); +const videoManager = require("../videoManager"); + routes.get('/', (request, result) => { + var videoID = request.query.v; + if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER || - userUtils.isValidAPI(request.query.api)) + userUtils.isValidAPI(request.query.api) || + videoManager.isPublicVideo(videoID)) { const rootDir = configManager.getRootDirectory(); - var videoID = request.query.v; + const path = rootDir + videoID; const stat = fs.statSync(path); const fileSize = stat.size; diff --git a/routes/videos.js b/routes/videos.js index 764da4f..f863e1b 100644 --- a/routes/videos.js +++ b/routes/videos.js @@ -2,74 +2,23 @@ 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 privateVideos = null; - -var publicVideos = null; - -function getVideosForTemplate(templateContext, rootDir, templateKey, videos) -{ - 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/' + templateKey + '/' + 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, '')}); - }); - templateContext[templateKey] = videos; - resolve(); - }); - }) - } - else - { - templateContext[templateKey] = videos; - } -} +const videoManager = require("../videoManager"); 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)); + promises.push(videoManager.getVideosForTemplate(templateContext, "private")); } else { templateContext["private"] = []; } - promises.push(getVideosForTemplate(templateContext, rootPublicDir, "public", publicVideos)); + promises.push(videoManager.getVideosForTemplate(templateContext, "public")); return Promise.all(promises); } diff --git a/videoManager.js b/videoManager.js new file mode 100644 index 0000000..b98b49f --- /dev/null +++ b/videoManager.js @@ -0,0 +1,125 @@ +const configManager = require('./configManager'); + +const recursive = require('./recursiveTraversal'); + +const filepreview = require('filepreview'); + +const fs = require('fs'); + +var privateVideos = null; + +var publicVideos = null; + +module.exports = + { + indexVideos: function(rootDir, videos, templateKey) + { + 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 = './icon/' + templateKey + '/' + name + ".png"; + if (!fs.existsSync(icon)) + { + var options = { + width: 200, + quality: 50 + }; + + 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, '')}); + }); + resolve(); + }); + }).catch(function(error) + { + console.log(error); + }) + }, + + getVideosForTemplate: function(templateContext, templateKey) + { + return new Promise(function(resolve, reject) + { + var videos, rootDir; + if(templateKey === "public") + { + videos = publicVideos; + rootDir = configManager.getPublicDirectory(); + } + else + { + videos = privateVideos; + rootDir = configManager.getRootDirectory(); + } + + if(videos === null) + { + videos = []; + module.exports.indexVideos(rootDir, videos, templateKey) + .then(function() + { + templateContext[templateKey] = videos; + if(templateKey === "public") + publicVideos = videos; + else + privateVideos = videos; + resolve(); + }) + } + else + { + templateContext[templateKey] = videos; + resolve(); + } + }) + }, + + isPublicVideo: function(videoName) + { + if(publicVideos == null) + { + publicVideos = []; + rootDir = configManager.getPublicDirectory(); + module.exports.indexVideos(rootDir, publicVideos, "public").then(function() + { + for(var i = 0; i < publicVideos.length; i++) + { + const splitArray = publicVideos[i].name.split('/'); + const name = splitArray[splitArray.length -1]; + + if(name === videoName) + { + return true; + } + } + return false; + }); + } + else + { + for(var i = 0; i < publicVideos.length; i++) + { + const splitArray = publicVideos[i].name.split('/'); + const name = splitArray[splitArray.length -1]; + + if(name === videoName) + { + return true; + } + } + return false; + } + } + }; \ No newline at end of file