listSong.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. //Always use all roots
  36. musicDis.push(thisRoot);
  37. }
  38. //Handle user request on listing
  39. if (typeof(listSong) != "undefined"){
  40. //List song given type
  41. if (listSong == "all"){
  42. //Load from cache first. If cache list > 1000 then deliver the cache then update the cache file
  43. newDBTableIfNotExists("AirMusic");
  44. var cacheListRaw = readDBItem("AirMusic", "cache");
  45. if (cacheListRaw == ""){
  46. //Cache list not generated yet. Continue
  47. }else if (cacheListRaw != ""){
  48. //There is something in the cache list. Try parse it
  49. try{
  50. //Try parse it.
  51. var cacheList = JSON.parse(cacheListRaw);
  52. if (cacheList.length > MINCACHESIZE){
  53. //Too many songs. Just use the cache list instead.
  54. sendJSONResp(JSON.stringify({
  55. cached: true,
  56. list: cacheList
  57. }));
  58. return
  59. }
  60. }catch(ex){
  61. }
  62. }
  63. //Do the scanning
  64. var songData = [];
  65. var musicFiles = [];
  66. for (var i = 0; i < musicDis.length; i++){
  67. var thisFileLib = musicDis[i];
  68. var allfilelist = filelib.walk(thisFileLib, "file");
  69. for (var k = 0; k < allfilelist.length; k++){
  70. var ext = allfilelist[k].split('.').pop();
  71. if (IsSupportExt(ext) == true){
  72. musicFiles.push(allfilelist[k]);
  73. }
  74. }
  75. }
  76. for (var i = 0; i < musicFiles.length; i++){
  77. var thisMusicFile = musicFiles[i];
  78. var thisSongData = [];
  79. //Access Path
  80. thisSongData.push("/media?file=" + thisMusicFile);
  81. //File Name only
  82. var filename = thisMusicFile.split("/").pop()
  83. filename = filename.split(".");
  84. var ext = filename.pop();
  85. filename = filename.join(".");
  86. thisSongData.push(filename);
  87. //File Extension
  88. thisSongData.push(ext);
  89. //File size
  90. var fileSize = bytesToSize(filelib.filesize(thisMusicFile))
  91. thisSongData.push(fileSize)
  92. songData.push(thisSongData);
  93. }
  94. //Return the parsed info
  95. //sendJSONResp(JSON.stringify(songData));
  96. //Write to cache and send
  97. writeDBItem("AirMusic", "cache", JSON.stringify(songData));
  98. sendJSONResp(JSON.stringify({
  99. cached: false,
  100. list: songData
  101. }));
  102. }else if (listSong.substr(0,7) == "search:"){
  103. //Search mode
  104. var keyword = listSong.substr(7)
  105. var songData = [];
  106. for (var i = 0; i < musicDis.length; i++){
  107. var thisFileLib = musicDis[i];
  108. //Load the music list from cache to reduce scanning time
  109. var cachedList = readDBItem("AirMusic", "cache");
  110. var allfilelist = [];
  111. if (cachedList == ""){
  112. allfilelist = filelib.walk(thisFileLib, "file");
  113. }else{
  114. //Try parse it. If parse failed fallback to realtime scanning
  115. try{
  116. cachedList = JSON.parse(cachedList);
  117. for (var j = 0; j < cachedList.length; j++){
  118. var thisCachedSong = cachedList[j];
  119. allfilelist.push(thisCachedSong[0].replace("/media?file=", ""));
  120. }
  121. }catch(ex){
  122. allfilelist = filelib.walk(thisFileLib, "file");
  123. }
  124. }
  125. for (var k = 0; k < allfilelist.length; k++){
  126. var thisFile = allfilelist[k];
  127. var ext = allfilelist[k].split('.').pop();
  128. var filename = allfilelist[k].split('/').pop();
  129. if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1){
  130. //This file match our ext req and keyword exists
  131. var thisSongData = [];
  132. //Access Path
  133. thisSongData.push("/media?file=" + thisFile);
  134. //File Name only
  135. var filename = thisFile.split("/").pop()
  136. filename = filename.split(".");
  137. var ext = filename.pop();
  138. filename = filename.join(".");
  139. thisSongData.push(filename);
  140. //File Extension
  141. thisSongData.push(ext);
  142. //File size
  143. var fileSize = bytesToSize(filelib.filesize(thisFile))
  144. thisSongData.push(fileSize)
  145. songData.push(thisSongData);
  146. }
  147. }
  148. }
  149. //Send resp
  150. sendJSONResp(JSON.stringify(songData));
  151. //Run build cache in background so to update any cache if exists
  152. execd("buildCache.js")
  153. }else{
  154. //WIP
  155. console.log("listSong type " + listSong + " work in progress")
  156. }
  157. }else if (typeof(listdir) != "undefined"){
  158. //List dir given path
  159. if (listdir == "root"){
  160. //List the information of the user roots that contain Music folder
  161. var rootInfo = [];
  162. for (var i =0; i < musicDis.length; i++){
  163. var thisRootInfo = [];
  164. var thisMusicDir = musicDis[i];
  165. var thisRoot = thisMusicDir.split("/").shift() + "/";
  166. var objcetsInRoot = [];
  167. if (thisRoot == "user:/"){
  168. objcetsInRoot = filelib.glob(thisRoot + "Music/*");
  169. }else{
  170. objcetsInRoot = filelib.glob(thisRoot + "*");
  171. thisMusicDir = thisRoot;
  172. }
  173. var rootName = filelib.rname(thisRoot);
  174. if (rootName == false){
  175. rootName = thisRoot
  176. }
  177. thisRootInfo.push(rootName);
  178. thisRootInfo.push(thisMusicDir);
  179. var files = [];
  180. var folders = [];
  181. for (var j = 0; j < objcetsInRoot.length; j++){
  182. if (filelib.isDir(objcetsInRoot[j])){
  183. folders.push(objcetsInRoot[j]);
  184. }else{
  185. files.push(objcetsInRoot[j]);
  186. }
  187. }
  188. thisRootInfo.push(files.length);
  189. thisRootInfo.push(folders.length);
  190. rootInfo.push(thisRootInfo);
  191. }
  192. sendJSONResp(JSON.stringify(rootInfo));
  193. }else{
  194. //List information about other folders
  195. var targetpath = decodeURIComponent(listdir)
  196. var filelist = filelib.aglob(targetpath + "*")
  197. var files = [];
  198. var folders = [];
  199. for (var j = 0; j < filelist.length; j++){
  200. if (filelib.isDir(filelist[j])){
  201. folders.push(filelist[j]);
  202. }else{
  203. var ext = filelist[j].split(".").pop();
  204. if (IsSupportExt(ext)){
  205. files.push(filelist[j]);
  206. }
  207. }
  208. }
  209. //For each folder, get its information
  210. var folderInfo = [];
  211. for (var i = 0; i < folders.length; i++){
  212. var thisFolderInfo = [];
  213. var folderName = folders[i].split("/").pop();
  214. var thisFolderSubfolder = [];
  215. var thisFolderSubfiles = [];
  216. var subFolderFileList = filelib.aglob(folders[i] + "/*")
  217. for (var j = 0; j < subFolderFileList.length; j++){
  218. if (filelib.isDir(subFolderFileList[j])){
  219. var thisFolderName = subFolderFileList[j].split("/").pop();
  220. if (thisFolderName.substring(0,1) != "."){
  221. thisFolderSubfolder.push(subFolderFileList[j]);
  222. }
  223. }else{
  224. var ext = subFolderFileList[j].split(".").pop();
  225. if (IsSupportExt(ext)){
  226. thisFolderSubfiles.push(subFolderFileList[j]);
  227. }
  228. }
  229. }
  230. thisFolderInfo.push(folderName);
  231. thisFolderInfo.push(folders[i] + "/");
  232. thisFolderInfo.push((thisFolderSubfiles).length + "");
  233. thisFolderInfo.push((thisFolderSubfolder).length + "");
  234. folderInfo.push(thisFolderInfo)
  235. }
  236. var fileInfo = [];
  237. for (var i = 0; i < files.length; i++){
  238. var thisFileInfo = [];
  239. var filename = files[i].split("/").pop();
  240. filename = filename.split('.')
  241. filename.pop();
  242. filename = filename.join(".");
  243. var ext = files[i].split(".").pop()
  244. var filesize = filelib.filesize(files[i]);
  245. filesize = bytesToSize(filesize);
  246. thisFileInfo.push("/media?file=" + files[i]);
  247. thisFileInfo.push(filename);
  248. thisFileInfo.push(ext);
  249. thisFileInfo.push(filesize);
  250. fileInfo.push(thisFileInfo);
  251. }
  252. var results = [];
  253. results.push(folderInfo);
  254. results.push(fileInfo);
  255. sendJSONResp(JSON.stringify(results));
  256. }
  257. }else if (typeof(listFolder) != "undefined"){
  258. //List folder giben filepath
  259. }
  260. }
  261. }
  262. //Execute Handler
  263. handleUserRequest();