buildPlaylist.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. requirelib("imagelib");
  8. //Create a function to build file data from filepath
  9. function buildFileObject(filepath){
  10. var fileObject = {};
  11. fileObject["Filename"] = filepath.split("/").pop();
  12. fileObject["Filepath"] = filepath;
  13. fileObject["Ext"] = "." + filepath.split(".").pop();
  14. return fileObject;
  15. }
  16. function ext(filepath){
  17. return filepath.split(".").pop();
  18. }
  19. function basename(filepath){
  20. return filepath.split("/").pop();
  21. }
  22. function scanPathForVideo(thisDir, thisStorageName){
  23. var playlistInThisStorage = [];
  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 video 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 thisThumbnail = imagelib.loadThumbString(thisFolder);
  47. var playlistFilelist = filelib.aglob(thisFolder + "/*.*")
  48. var playlistName = basename(thisFolder);
  49. var thisPlaylistObject = {};
  50. //If name parent is not Video, add a subfix
  51. var baseBasename = thisFolder.split("/");
  52. baseBasename.pop();
  53. baseBasename = baseBasename.pop();
  54. if (baseBasename != "Video"){
  55. playlistName = baseBasename + " / " + playlistName
  56. }
  57. thisPlaylistObject["Thumbnail"] = thisThumbnail;
  58. thisPlaylistObject["Folderpath"] = thisFolder;
  59. thisPlaylistObject["Name"] = playlistName;
  60. thisPlaylistObject["Files"] = [];
  61. for (var k =0; k < playlistFilelist.length; k++){
  62. //For each files in this folder
  63. if (!filelib.isDir(playlistFilelist[k]) && validVideoFormat.indexOf(ext(playlistFilelist[k])) > -1 ){
  64. //This is a video file extension file
  65. var filenameOnly = JSON.parse(JSON.stringify(playlistFilelist[k])).split("/").pop();
  66. if (filenameOnly.substr(0,2) == "._"){
  67. //MacOS caching files. Ignore this
  68. continue
  69. }
  70. thisPlaylistObject["Files"].push(buildFileObject(playlistFilelist[k]));
  71. }
  72. }
  73. playlistInThisStorage.push(thisPlaylistObject)
  74. }
  75. //Build the unsorted file list
  76. /*
  77. var unsortedFileList = [];
  78. for (var i = 0; i < validVideoFormat.length; i++){
  79. var unsortedFiles = filelib.aglob(walkPath + "/*." + validVideoFormat[i])
  80. for (var j = 0; j < unsortedFiles.length; j++){
  81. unsortedFileList.push(buildFileObject(unsortedFiles[j]));
  82. }
  83. }
  84. */
  85. //Push scan into results
  86. if (playlistInThisStorage.length > 0){
  87. thisStoragePlaylist["PlayLists"] = playlistInThisStorage;
  88. //thisStoragePlaylist["UnsortedVideos"] = unsortedFileList;
  89. playlist.push(thisStoragePlaylist);
  90. }
  91. }
  92. }
  93. function main(){
  94. //Craete the user Video folder
  95. filelib.mkdir("user:/Video");
  96. //Scan each of the storage devices for video files
  97. var vroots = filelib.glob("/");
  98. for (var i =0; i < vroots.length; i++){
  99. var thisRoot = vroots[i];
  100. var thisDir = thisRoot;
  101. var thisStorageName = filelib.rootName(thisRoot);
  102. scanPathForVideo(thisDir, thisStorageName)
  103. }
  104. sendJSONResp(JSON.stringify(playlist));
  105. }
  106. main();