listSong.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. List Song
  3. This function will list all song with the given directories.
  4. Type of operations
  5. listSong=(type)
  6. listSong=search:(keyword)
  7. listdir=root
  8. listdir= (Target path)
  9. listfolder=(target folder)
  10. This module is a direct lanuage translation from the original module.Musi.go
  11. */
  12. //Global variables
  13. var MINCACHESIZE = 300; //Min song list length for start using cache method
  14. //Include the common library
  15. var succ = includes("common.js")
  16. if (succ == false){
  17. console.log("Failed to load common.js");
  18. }
  19. //Handle user request
  20. function handleUserRequest(){
  21. if (requirelib("filelib") == false){
  22. sendJSONResp(JSON.stringify({
  23. error: "Unable to load filelib"
  24. }));
  25. }else{
  26. var musicDis = [];
  27. //Make the user's root music folder if not exists
  28. if (filelib.fileExists("user:/Music/") == false){
  29. filelib.mkdir("user:/Music/");
  30. }
  31. //Scan all music directories
  32. var rootDirs = filelib.glob("/");
  33. for (var i = 0; i < rootDirs.length; i++){
  34. var thisRoot = rootDirs[i];
  35. /*
  36. if (filelib.fileExists(thisRoot + "Music/")){
  37. musicDis.push(thisRoot + "Music/");
  38. }
  39. */
  40. //Always use all roots
  41. musicDis.push(thisRoot);
  42. }
  43. //Handle user request on listing
  44. if (typeof(listSong) != "undefined"){
  45. //List song given type
  46. if (listSong == "all"){
  47. //Load from cache first. If cache list > 1000 then deliver the cache then update the cache file
  48. newDBTableIfNotExists("AirMusic");
  49. var cacheListRaw = readDBItem("AirMusic", "cache");
  50. if (cacheListRaw == ""){
  51. //Cache list not generated yet. Continue
  52. }else if (cacheListRaw != ""){
  53. //There is something in the cache list. Try parse it
  54. try{
  55. //Try parse it.
  56. var cacheList = JSON.parse(cacheListRaw);
  57. if (cacheList.length > MINCACHESIZE){
  58. //Too many songs. Just use the cache list instead.
  59. sendJSONResp(JSON.stringify({
  60. cached: true,
  61. list: cacheList
  62. }));
  63. return
  64. }
  65. }catch(ex){
  66. }
  67. }
  68. //Do the scanning
  69. var songData = [];
  70. var musicFiles = [];
  71. for (var i = 0; i < musicDis.length; i++){
  72. var thisFileLib = musicDis[i];
  73. var allfilelist = filelib.walk(thisFileLib, "file");
  74. for (var k = 0; k < allfilelist.length; k++){
  75. var ext = allfilelist[k].split('.').pop();
  76. if (IsSupportExt(ext) == true){
  77. musicFiles.push(allfilelist[k]);
  78. }
  79. }
  80. }
  81. for (var i = 0; i < musicFiles.length; i++){
  82. var thisMusicFile = musicFiles[i];
  83. var thisSongData = [];
  84. //Access Path
  85. thisSongData.push("/media?file=" + thisMusicFile);
  86. //File Name only
  87. var filename = thisMusicFile.split("/").pop()
  88. filename = filename.split(".");
  89. var ext = filename.pop();
  90. filename = filename.join(".");
  91. thisSongData.push(filename);
  92. //File Extension
  93. thisSongData.push(ext);
  94. //File size
  95. var fileSize = bytesToSize(filelib.filesize(thisMusicFile))
  96. thisSongData.push(fileSize)
  97. songData.push(thisSongData);
  98. }
  99. //Return the parsed info
  100. //sendJSONResp(JSON.stringify(songData));
  101. //Write to cache and send
  102. writeDBItem("AirMusic", "cache", JSON.stringify(songData));
  103. sendJSONResp(JSON.stringify({
  104. cached: false,
  105. list: songData
  106. }));
  107. }else if (listSong.substr(0,7) == "search:"){
  108. var keyword = listSong.substr(7)
  109. var songData = [];
  110. for (var i = 0; i < musicDis.length; i++){
  111. var thisFileLib = musicDis[i];
  112. var allfilelist = filelib.walk(thisFileLib, "file");
  113. for (var k = 0; k < allfilelist.length; k++){
  114. var thisFile = allfilelist[k];
  115. var ext = allfilelist[k].split('.').pop();
  116. var filename = allfilelist[k].split('/').pop();
  117. if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1){
  118. //This file match our ext req and keyword exists
  119. var thisSongData = [];
  120. //Access Path
  121. thisSongData.push("/media?file=" + thisFile);
  122. //File Name only
  123. var filename = thisFile.split("/").pop()
  124. filename = filename.split(".");
  125. var ext = filename.pop();
  126. filename = filename.join(".");
  127. thisSongData.push(filename);
  128. //File Extension
  129. thisSongData.push(ext);
  130. //File size
  131. var fileSize = bytesToSize(filelib.filesize(thisFile))
  132. thisSongData.push(fileSize)
  133. songData.push(thisSongData);
  134. }
  135. }
  136. }
  137. //Send resp
  138. sendJSONResp(JSON.stringify(songData));
  139. }else{
  140. //WIP
  141. console.log("listSong type " + listSong + " work in progress")
  142. }
  143. }else if (typeof(listdir) != "undefined"){
  144. //List dir given path
  145. if (listdir == "root"){
  146. //List the information of the user roots that contain Music folder
  147. var rootInfo = [];
  148. for (var i =0; i < musicDis.length; i++){
  149. var thisRootInfo = [];
  150. var thisMusicDir = musicDis[i];
  151. var thisRoot = thisMusicDir.split("/").shift() + "/";
  152. var objcetsInRoot = [];
  153. if (thisRoot == "user:/"){
  154. objcetsInRoot = filelib.glob(thisRoot + "Music/*");
  155. }else{
  156. objcetsInRoot = filelib.glob(thisRoot + "*");
  157. thisMusicDir = thisRoot;
  158. }
  159. var rootName = filelib.rname(thisRoot);
  160. if (rootName == false){
  161. rootName = thisRoot
  162. }
  163. thisRootInfo.push(rootName);
  164. thisRootInfo.push(thisMusicDir);
  165. var files = [];
  166. var folders = [];
  167. for (var j = 0; j < objcetsInRoot.length; j++){
  168. if (filelib.isDir(objcetsInRoot[j])){
  169. folders.push(objcetsInRoot[j]);
  170. }else{
  171. files.push(objcetsInRoot[j]);
  172. }
  173. }
  174. thisRootInfo.push(files.length);
  175. thisRootInfo.push(folders.length);
  176. rootInfo.push(thisRootInfo);
  177. }
  178. sendJSONResp(JSON.stringify(rootInfo));
  179. }else{
  180. //List information about other folders
  181. var targetpath = decodeURIComponent(listdir)
  182. var filelist = filelib.aglob(targetpath + "*")
  183. var files = [];
  184. var folders = [];
  185. for (var j = 0; j < filelist.length; j++){
  186. if (filelib.isDir(filelist[j])){
  187. folders.push(filelist[j]);
  188. }else{
  189. var ext = filelist[j].split(".").pop();
  190. if (IsSupportExt(ext)){
  191. files.push(filelist[j]);
  192. }
  193. }
  194. }
  195. //For each folder, get its information
  196. var folderInfo = [];
  197. for (var i = 0; i < folders.length; i++){
  198. var thisFolderInfo = [];
  199. var folderName = folders[i].split("/").pop();
  200. var thisFolderSubfolder = [];
  201. var thisFolderSubfiles = [];
  202. var subFolderFileList = filelib.aglob(folders[i] + "/*")
  203. for (var j = 0; j < subFolderFileList.length; j++){
  204. if (filelib.isDir(subFolderFileList[j])){
  205. var thisFolderName = subFolderFileList[j].split("/").pop();
  206. if (thisFolderName.substring(0,1) != "."){
  207. thisFolderSubfolder.push(subFolderFileList[j]);
  208. }
  209. }else{
  210. var ext = subFolderFileList[j].split(".").pop();
  211. if (IsSupportExt(ext)){
  212. thisFolderSubfiles.push(subFolderFileList[j]);
  213. }
  214. }
  215. }
  216. thisFolderInfo.push(folderName);
  217. thisFolderInfo.push(folders[i] + "/");
  218. thisFolderInfo.push((thisFolderSubfiles).length + "");
  219. thisFolderInfo.push((thisFolderSubfolder).length + "");
  220. folderInfo.push(thisFolderInfo)
  221. }
  222. var fileInfo = [];
  223. for (var i = 0; i < files.length; i++){
  224. var thisFileInfo = [];
  225. var filename = files[i].split("/").pop();
  226. filename = filename.split('.')
  227. filename.pop();
  228. filename = filename.join(".");
  229. var ext = files[i].split(".").pop()
  230. var filesize = filelib.filesize(files[i]);
  231. filesize = bytesToSize(filesize);
  232. thisFileInfo.push("/media?file=" + files[i]);
  233. thisFileInfo.push(filename);
  234. thisFileInfo.push(ext);
  235. thisFileInfo.push(filesize);
  236. fileInfo.push(thisFileInfo);
  237. }
  238. var results = [];
  239. results.push(folderInfo);
  240. results.push(fileInfo);
  241. sendJSONResp(JSON.stringify(results));
  242. }
  243. }else if (typeof(listFolder) != "undefined"){
  244. //List folder giben filepath
  245. }
  246. }
  247. }
  248. //Execute Handler
  249. handleUserRequest();