Browse Source

Updated system to recursively read subdirectories to find files.

pull/6/head
jrtechs 5 years ago
parent
commit
5adecca31b
4 changed files with 138 additions and 7 deletions
  1. +1
    -1
      html/users.html
  2. +27
    -2
      html/watch.html
  3. +103
    -0
      recursiveTraversal.js
  4. +7
    -4
      server.js

+ 1
- 1
html/users.html View File

@ -18,7 +18,7 @@
<div class="card-body">
<form action="/edituser" method ="post" class="p-2">
<div class="form-group">
<input class="form-control" id="useridLabel" type="text" name="id" required disabled>
<input class="form-control" id="useridLabel" type="text" name="id" required>
<label>User ID</label>
</div>
<div class="form-group">

+ 27
- 2
html/watch.html View File

@ -1,3 +1,28 @@
<video id="videoPlayer" width="768" height="432" controls>
<video id="videoPlayer" width="768" height="432" controls onerror="failed(event)">
<source src="/video?v={videoURL}" type="video/mp4">
</video>
Your browser does not support the video tag.
</video>
<script>
function failed(e) {
// video playback failed - show a message saying why
switch (e.target.error.code) {
case e.target.error.MEDIA_ERR_ABORTED:
alert('You aborted the video playback.');
break;
case e.target.error.MEDIA_ERR_NETWORK:
alert('A network error caused the video download to fail part-way.');
break;
case e.target.error.MEDIA_ERR_DECODE:
alert('The video playback was aborted due to a corruption problem or because the video used features your browser did not support.');
break;
case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
alert('The video could not be loaded, either because the server or network failed or because the format is not supported.');
break;
default:
alert('An unknown error occurred.');
break;
}
}
</script>

+ 103
- 0
recursiveTraversal.js View File

@ -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;

+ 7
- 4
server.js View File

@ -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();

Loading…
Cancel
Save