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.

169 lines
5.0 KiB

  1. const configManager = require('./configManager');
  2. const recursive = require('./recursiveTraversal');
  3. const filepreview = require('filepreview');
  4. const fs = require('fs');
  5. var privateVideos = null;
  6. var publicVideos = null;
  7. function createIndex(filename, videos, templateKey)
  8. {
  9. return new Promise(function(resolve, reject)
  10. {
  11. console.log("Generating icon for " + filename);
  12. var splitArray = filename.split('/');
  13. var name = splitArray[splitArray.length -1];
  14. const icon = './icon/' + templateKey + '/' + name + ".png";
  15. if (!fs.existsSync(icon))
  16. {
  17. var options = {
  18. width: 200,
  19. quality: 50,
  20. previewTime: '00:05:00.000'
  21. };
  22. filepreview.generate(filename, icon, options, function (error)
  23. {
  24. if (error)
  25. {
  26. resolve();
  27. }
  28. console.log('File preview is located ' + icon);
  29. resolve();
  30. });
  31. }
  32. else
  33. {
  34. resolve();
  35. }
  36. })
  37. }
  38. async function runTasksSync(files, videos, templateKey)
  39. {
  40. for(var file of files)
  41. {
  42. await createIndex(file, videos, templateKey);
  43. }
  44. }
  45. module.exports =
  46. {
  47. indexVideos: function(rootDir, videos, templateKey)
  48. {
  49. return new Promise(function(resolve, reject)
  50. {
  51. recursive(rootDir, function (err, files)
  52. {
  53. files.forEach(file =>
  54. {
  55. videos.push({name: file.replace(rootDir, '')});
  56. });
  57. runTasksSync(files.splice(0, files.length/2), videos, templateKey);
  58. runTasksSync(files.splice(files.length/2, files.length), videos, templateKey);
  59. resolve();
  60. });
  61. }).catch(function(error)
  62. {
  63. //console.log(error);
  64. })
  65. },
  66. getVideosForTemplate: function(templateContext, templateKey)
  67. {
  68. return new Promise(function(resolve, reject)
  69. {
  70. var videos, rootDir;
  71. if(templateKey === "public")
  72. {
  73. videos = publicVideos;
  74. rootDir = configManager.getPublicDirectory();
  75. }
  76. else
  77. {
  78. videos = privateVideos;
  79. rootDir = configManager.getRootDirectory();
  80. }
  81. if(videos === null)
  82. {
  83. videos = [];
  84. module.exports.indexVideos(rootDir, videos, templateKey)
  85. .then(function()
  86. {
  87. templateContext[templateKey] = videos;
  88. if(templateKey === "public")
  89. publicVideos = videos;
  90. else
  91. privateVideos = videos;
  92. resolve();
  93. })
  94. }
  95. else
  96. {
  97. templateContext[templateKey] = videos;
  98. resolve();
  99. }
  100. })
  101. },
  102. isPublicVideo: function(videoName)
  103. {
  104. if(publicVideos == null)
  105. {
  106. publicVideos = [];
  107. rootDir = configManager.getPublicDirectory();
  108. module.exports.indexVideos(rootDir, publicVideos, "public").then(function()
  109. {
  110. for(var i = 0; i < publicVideos.length; i++)
  111. {
  112. const splitArray = publicVideos[i].name.split('/');
  113. const name = splitArray[splitArray.length -1];
  114. if(name === videoName)
  115. {
  116. return true;
  117. }
  118. }
  119. return false;
  120. });
  121. }
  122. else
  123. {
  124. for(var i = 0; i < publicVideos.length; i++)
  125. {
  126. const splitArray = publicVideos[i].name.split('/');
  127. const name = splitArray[splitArray.length -1];
  128. if(name === videoName)
  129. {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. },
  136. reIndexVideos: function()
  137. {
  138. publicVideos = [];
  139. privateVideos = [];
  140. module.exports.indexVideos(configManager.getPublicDirectory(), publicVideos, "public");
  141. module.exports.indexVideos(configManager.getRootDirectory(), privateVideos, "private");
  142. },
  143. getPublicVideoCount: function()
  144. {
  145. return (publicVideos === null) ? 0: publicVideos.length;
  146. },
  147. getPrivateVideoCount: function()
  148. {
  149. return (privateVideos === null) ? 0: privateVideos.length;
  150. }
  151. };