buildPlaylist.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  61. thisPlaylistObject["Files"].push(buildFileObject(playlistFilelist[k]));
  62. }
  63. }
  64. playlistInThisStorage.push(thisPlaylistObject)
  65. }
  66. //Build the unsorted file list
  67. var unsortedFileList = [];
  68. for (var i = 0; i < validVideoFormat.length; i++){
  69. var unsortedFiles = filelib.aglob(walkPath + "/*." + validVideoFormat[i])
  70. for (var j = 0; j < unsortedFiles.length; j++){
  71. unsortedFileList.push(buildFileObject(unsortedFiles[j]));
  72. }
  73. }
  74. //Push scan into results
  75. if (playlistInThisStorage.length > 0){
  76. thisStoragePlaylist["PlayLists"] = playlistInThisStorage;
  77. //thisStoragePlaylist["UnsortedVideos"] = unsortedFileList;
  78. playlist.push(thisStoragePlaylist);
  79. }
  80. }
  81. }
  82. function main(){
  83. //Craete the user Video folder
  84. filelib.mkdir("user:/Video");
  85. //Scan each of the storage devices for video files
  86. for (var i =0; i < storages.length; i++){
  87. var thisDir = storages[i].UUID + ":/";
  88. var thisStorageName = storages[i].Name;
  89. scanPathForVideo(thisDir, thisStorageName)
  90. }
  91. sendJSONResp(JSON.stringify(playlist));
  92. }
  93. main();