Lightweight node app to use in place of plex to self host your video and movie collections. Running on just under 50MB of ram this is ideal for people looking to host videos on minimal hardware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

  1. var fs = require("fs");
  2. var p = require("path");
  3. var minimatch = require("minimatch");
  4. function patternMatcher(pattern) {
  5. return function(path, stats) {
  6. var minimatcher = new minimatch.Minimatch(pattern, { matchBase: true });
  7. return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path);
  8. };
  9. }
  10. function toMatcherFunction(ignoreEntry) {
  11. if (typeof ignoreEntry == "function") {
  12. return ignoreEntry;
  13. } else {
  14. return patternMatcher(ignoreEntry);
  15. }
  16. }
  17. function readdir(path, ignores, callback) {
  18. if (typeof ignores == "function") {
  19. callback = ignores;
  20. ignores = [];
  21. }
  22. if (!callback) {
  23. return new Promise(function(resolve, reject) {
  24. readdir(path, ignores || [], function(err, data) {
  25. if (err) {
  26. reject(err);
  27. } else {
  28. resolve(data);
  29. }
  30. });
  31. });
  32. }
  33. ignores = ignores.map(toMatcherFunction);
  34. var list = [];
  35. fs.readdir(path, function(err, files) {
  36. if (err) {
  37. return callback(err);
  38. }
  39. var pending = files.length;
  40. if (!pending) {
  41. // we are done, woop woop
  42. return callback(null, list);
  43. }
  44. files.forEach(function(file)
  45. {
  46. var filePath = p.join(path, file);
  47. fs.stat(filePath, function(_err, stats)
  48. {
  49. if (_err)
  50. {
  51. //return callback(_err);
  52. return callback(null, list);
  53. }
  54. else
  55. {
  56. if (
  57. ignores.some(function(matcher) {
  58. return matcher(filePath, stats);
  59. })
  60. ) {
  61. pending -= 1;
  62. if (!pending) {
  63. return callback(null, list);
  64. }
  65. return null;
  66. }
  67. if (stats.isDirectory()) {
  68. readdir(filePath, ignores, function(__err, res) {
  69. if (__err) {
  70. return callback(__err);
  71. }
  72. list = list.concat(res);
  73. pending -= 1;
  74. if (!pending) {
  75. return callback(null, list);
  76. }
  77. });
  78. } else {
  79. list.push(filePath);
  80. pending -= 1;
  81. if (!pending) {
  82. return callback(null, list);
  83. }
  84. }
  85. }
  86. });
  87. });
  88. });
  89. }
  90. module.exports = readdir;