diff --git a/html/users.html b/html/users.html index b0f3b04..ca18f35 100644 --- a/html/users.html +++ b/html/users.html @@ -18,7 +18,7 @@
- +
diff --git a/html/watch.html b/html/watch.html index 4bf567c..881a296 100644 --- a/html/watch.html +++ b/html/watch.html @@ -1,3 +1,28 @@ - + + + \ No newline at end of file diff --git a/recursiveTraversal.js b/recursiveTraversal.js new file mode 100644 index 0000000..fba3d38 --- /dev/null +++ b/recursiveTraversal.js @@ -0,0 +1,103 @@ +var fs = require("fs"); +var p = require("path"); +var minimatch = require("minimatch"); + +function patternMatcher(pattern) { + return function(path, stats) { + var minimatcher = new minimatch.Minimatch(pattern, { matchBase: true }); + return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path); + }; +} + +function toMatcherFunction(ignoreEntry) { + if (typeof ignoreEntry == "function") { + return ignoreEntry; + } else { + return patternMatcher(ignoreEntry); + } +} + +function readdir(path, ignores, callback) { + if (typeof ignores == "function") { + callback = ignores; + ignores = []; + } + + if (!callback) { + return new Promise(function(resolve, reject) { + readdir(path, ignores || [], function(err, data) { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + ignores = ignores.map(toMatcherFunction); + + var list = []; + + fs.readdir(path, function(err, files) { + if (err) { + return callback(err); + } + + var pending = files.length; + if (!pending) { + // we are done, woop woop + return callback(null, list); + } + + files.forEach(function(file) + { + var filePath = p.join(path, file); + fs.stat(filePath, function(_err, stats) + { + if (_err) + { + //return callback(_err); + return callback(null, list); + } + else + { + + if ( + ignores.some(function(matcher) { + return matcher(filePath, stats); + }) + ) { + pending -= 1; + if (!pending) { + return callback(null, list); + } + return null; + } + + if (stats.isDirectory()) { + readdir(filePath, ignores, function(__err, res) { + if (__err) { + return callback(__err); + } + + list = list.concat(res); + pending -= 1; + if (!pending) { + return callback(null, list); + } + }); + } else { + list.push(filePath); + pending -= 1; + if (!pending) { + return callback(null, list); + } + } + } + }); + }); + }); +} + +module.exports = readdir; \ No newline at end of file diff --git a/server.js b/server.js index 8df30a3..331ebe2 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,9 @@ const fileIO = require('./fileIO'); const userUtils = require('./user.js'); + +const recursive = require('./recursiveTraversal'); + const fs = require('fs'); const app = express(); @@ -25,8 +28,7 @@ app.use(session({ secret: config.sessionSecret, cookie: { maxAge: 6000000 }})); /** Template engine */ const whiskers = require('whiskers'); -const rootDir = '/home/jeff/public/Movies/'; - +var rootDir = '/home/jeff/public/Movies/'; function fetchInTemplate(templateContext, templateKey, filename) { @@ -96,11 +98,12 @@ function getVideosTemplateInformation(templateContext, request) videos = []; return new Promise(function(resolve, reject) { - fs.readdir(rootDir, (err, files) => + recursive(rootDir, function (err, files) { + console.log(files); files.forEach(file => { - videos.push({name: file, length: "n/a"}); + videos.push({name: file.replace(rootDir, ''), length: "n/a"}); }); templateContext.videos = videos; resolve();