buildPlaylist.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 thisStoragePlaylist = {};
  24. if (filelib.fileExists(thisDir + "Video/")){
  25. var walkPath = thisDir + "Video/";
  26. var folderList = filelib.walk(walkPath, "folder")
  27. //Build the folder list base on the discovered mp4 files
  28. var foldersThatContainsVideoFile = [];
  29. for (var i = 0; i < folderList.length; i++){
  30. var thisFolderPath = folderList[i];
  31. var validFilesInThisPath = 0;
  32. for (var j = 0; j < validVideoFormat.length; j++){
  33. var videoFile = filelib.aglob(thisFolderPath + "/*." + validVideoFormat[j])
  34. validFilesInThisPath += videoFile.length;
  35. }
  36. if (validFilesInThisPath > 0){
  37. //This folder contain video file
  38. foldersThatContainsVideoFile.push(thisFolderPath);
  39. }
  40. }
  41. thisStoragePlaylist["StorageName"] = thisStorageName;
  42. for (var i = 0; i < foldersThatContainsVideoFile.length; i++){
  43. //Generate playlist
  44. var thisFolder = foldersThatContainsVideoFile[i];
  45. var playlistFilelist = filelib.aglob(thisFolder + "/*.*")
  46. var playlistName = basename(thisFolder);
  47. var thisPlaylistObject = {};
  48. //If name parent is not Video, add a subfix
  49. var baseBasename = thisFolder.split("/");
  50. baseBasename.pop();
  51. baseBasename = baseBasename.pop();
  52. if (baseBasename != "Video"){
  53. playlistName = baseBasename + " / " + playlistName
  54. }
  55. thisPlaylistObject["Name"] = playlistName;
  56. thisPlaylistObject["Files"] = [];
  57. for (var k =0; k < playlistFilelist.length; k++){
  58. //For each files in this folder
  59. if (!filelib.isDir(playlistFilelist[k]) && validVideoFormat.indexOf(ext(playlistFilelist[k])) > -1 ){
  60. //This is a video file extension file
  61. var filenameOnly = JSON.parse(JSON.stringify(playlistFilelist[k])).split("/").pop();
  62. if (filenameOnly.substr(0,2) == "._"){
  63. //MacOS caching files. Ignore this
  64. continue
  65. }
  66. thisPlaylistObject["Files"].push(buildFileObject(playlistFilelist[k]));
  67. }
  68. }
  69. playlistInThisStorage.push(thisPlaylistObject)
  70. }
  71. //Build the unsorted file list
  72. /*
  73. var unsortedFileList = [];
  74. for (var i = 0; i < validVideoFormat.length; i++){
  75. var unsortedFiles = filelib.aglob(walkPath + "/*." + validVideoFormat[i])
  76. for (var j = 0; j < unsortedFiles.length; j++){
  77. unsortedFileList.push(buildFileObject(unsortedFiles[j]));
  78. }
  79. }
  80. */
  81. //Push scan into results
  82. if (playlistInThisStorage.length > 0){
  83. thisStoragePlaylist["PlayLists"] = playlistInThisStorage;
  84. //thisStoragePlaylist["UnsortedVideos"] = unsortedFileList;
  85. playlist.push(thisStoragePlaylist);
  86. }
  87. }
  88. }
  89. function main(){
  90. //Craete the user Video folder
  91. filelib.mkdir("user:/Video");
  92. //Scan each of the storage devices for video files
  93. for (var i =0; i < storages.length; i++){
  94. if (storages[i].Filesystem == "virtual" || storages[i].Hierarchy == "backup"){
  95. continue;
  96. }
  97. var thisDir = storages[i].UUID + ":/";
  98. var thisStorageName = storages[i].Name;
  99. scanPathForVideo(thisDir, thisStorageName)
  100. }
  101. sendJSONResp(JSON.stringify(playlist));
  102. }
  103. main();