buildPlaylist.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //Build the playlist for Video modules
  2. var storages = USER_VROOTS;
  3. var validVideoFormat = ["ogg","webm","mp4"];
  4. var playlist = [];
  5. //Require libraries
  6. requirelib("filelib");
  7. //Create a function to build file data from filepath
  8. function buildFileObject(filepath){
  9. var fileObject = {};
  10. fileObject["Filename"] = filepath.split("/").pop();
  11. fileObject["Filepath"] = filepath;
  12. fileObject["Ext"] = "." + filepath.split(".").pop();
  13. return fileObject;
  14. }
  15. function ext(filepath){
  16. return filepath.split(".").pop();
  17. }
  18. function basename(filepath){
  19. return filepath.split("/").pop();
  20. }
  21. function scanPathForVideo(thisDir, thisStorageName){
  22. var playlistInThisStorage = [];
  23. var unsortedVideoInThisStorage = [];
  24. var thisStoragePlaylist = {};
  25. if (filelib.fileExists(thisDir + "Video/")){
  26. var walkPath = thisDir + "Video/";
  27. var folderList = filelib.walk(walkPath, "folder")
  28. //Build the folder list base on the discovered mp4 files
  29. var foldersThatContainsVideoFile = [];
  30. for (var i = 0; i < folderList.length; i++){
  31. var thisFolderPath = folderList[i];
  32. var validFilesInThisPath = 0;
  33. for (var j = 0; j < validVideoFormat.length; j++){
  34. var videoFile = filelib.aglob(thisFolderPath + "/*." + validVideoFormat[j])
  35. validFilesInThisPath += videoFile.length;
  36. }
  37. if (validFilesInThisPath > 0){
  38. //This folder contain video file
  39. foldersThatContainsVideoFile.push(thisFolderPath);
  40. }
  41. }
  42. thisStoragePlaylist["StorageName"] = thisStorageName;
  43. for (var i = 0; i < foldersThatContainsVideoFile.length; i++){
  44. //Generate playlist
  45. var thisFolder = foldersThatContainsVideoFile[i];
  46. var playlistFilelist = filelib.aglob(thisFolder + "/*.*")
  47. var playlistName = basename(thisFolder);
  48. var thisPlaylistObject = {};
  49. //If name parent is not Video, add a subfix
  50. var baseBasename = thisFolder.split("/");
  51. baseBasename.pop();
  52. baseBasename = baseBasename.pop();
  53. if (baseBasename != "Video"){
  54. playlistName = baseBasename + " / " + playlistName
  55. }
  56. thisPlaylistObject["Name"] = playlistName;
  57. thisPlaylistObject["Files"] = [];
  58. for (var k =0; k < playlistFilelist.length; k++){
  59. //For each files in this folder
  60. if (!filelib.isDir(playlistFilelist[k]) && validVideoFormat.indexOf(ext(playlistFilelist[k])) > -1 ){
  61. //This is a video file
  62. thisPlaylistObject["Files"].push(buildFileObject(playlistFilelist[k]));
  63. }
  64. }
  65. playlistInThisStorage.push(thisPlaylistObject)
  66. }
  67. //Build the unsorted file list
  68. var unsortedFileList = [];
  69. for (var i = 0; i < validVideoFormat.length; i++){
  70. var unsortedFiles = filelib.aglob(walkPath + "/*." + validVideoFormat[i])
  71. for (var j = 0; j < unsortedFiles.length; j++){
  72. unsortedFileList.push(buildFileObject(unsortedFiles[j]));
  73. }
  74. }
  75. //Push scan into results
  76. if (playlistInThisStorage.length > 0){
  77. thisStoragePlaylist["PlayLists"] = playlistInThisStorage;
  78. thisStoragePlaylist["UnsortedVideos"] = unsortedFileList;
  79. playlist.push(thisStoragePlaylist);
  80. }
  81. //
  82. /*
  83. //Video folder exists in this directory
  84. thisStoragePlaylist["StorageName"] = thisStorageName;
  85. var fileList = filelib.glob(thisDir + "Video/*");
  86. for (var j =0; j < fileList.length; j++){
  87. if (filelib.isDir(fileList[j])){
  88. //This is a directory. Scan this playlist content
  89. var playlistFilelist = filelib.aglob(fileList[j] + "/*.*")
  90. var playlistName = basename(fileList[j]);
  91. var thisPlaylistObject = {};
  92. thisPlaylistObject["Name"] = playlistName;
  93. thisPlaylistObject["Files"] = [];
  94. for (var k =0; k < playlistFilelist.length; k++){
  95. //For each files in this folder
  96. if (!filelib.isDir(playlistFilelist[k]) && validVideoFormat.indexOf(ext(playlistFilelist[k])) > -1 ){
  97. //This is a video file
  98. thisPlaylistObject["Files"].push(buildFileObject(playlistFilelist[k]));
  99. }
  100. }
  101. if (thisPlaylistObject["Files"].length > 0){
  102. playlistInThisStorage.push(thisPlaylistObject)
  103. }
  104. }else{
  105. //This is just a normal file. Add to unsorted files
  106. if (validVideoFormat.indexOf(ext(fileList[j])) > -1 ){
  107. unsortedVideoInThisStorage.push(buildFileObject(fileList[j]))
  108. }
  109. }
  110. }
  111. //Push scan into results
  112. thisStoragePlaylist["PlayLists"] = playlistInThisStorage;
  113. thisStoragePlaylist["UnsortedVideos"] = unsortedVideoInThisStorage;
  114. playlist.push(thisStoragePlaylist);
  115. */
  116. }
  117. }
  118. function main(){
  119. //Craete the user Video folder
  120. filelib.mkdir("user:/Video");
  121. //Scan each of the storage devices for video files
  122. for (var i =0; i < storages.length; i++){
  123. var thisDir = storages[i].UUID + ":/";
  124. var thisStorageName = storages[i].Name;
  125. scanPathForVideo(thisDir, thisStorageName)
  126. }
  127. sendJSONResp(JSON.stringify(playlist));
  128. }
  129. main();