listSong.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 && !IsMetaFile(allfilelist[k])){
  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. keyword = keyword.toLowerCase();
  106. var songData = [];
  107. var cachedList = readDBItem("AirMusic", "cache");
  108. var allfilelist = [];
  109. function getRealtimeAllFleList(){
  110. var allfilelist = [];
  111. for (var i = 0; i < musicDis.length; i++){
  112. var thisFileLib = musicDis[i];
  113. thisDirList = filelib.walk(thisFileLib, "file");
  114. for (var j = 0; j < thisDirList.length; j++){
  115. allfilelist.push(thisDirList[j]);
  116. }
  117. }
  118. return allfilelist;
  119. }
  120. if (cachedList == ""){
  121. //No cache. Do real time scanning
  122. allfilelist = getRealtimeAllFleList();
  123. }else{
  124. //Try parse it. If parse failed fallback to realtime scanning
  125. try{
  126. cachedList = JSON.parse(cachedList);
  127. for (var j = 0; j < cachedList.length; j++){
  128. var thisCachedSong = cachedList[j];
  129. allfilelist.push(thisCachedSong[0].replace("/media?file=", ""));
  130. }
  131. }catch(ex){
  132. //Fallback
  133. allfilelist = getRealtimeAllFleList();
  134. }
  135. }
  136. for (var k = 0; k < allfilelist.length; k++){
  137. var thisFile = allfilelist[k];
  138. var ext = allfilelist[k].split('.').pop();
  139. var filename = allfilelist[k].split('/').pop();
  140. filename = filename.toLowerCase();
  141. if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1 && !IsMetaFile(allfilelist[k])){
  142. //This file match our ext req and keyword exists
  143. var thisSongData = [];
  144. //Access Path
  145. thisSongData.push("/media?file=" + thisFile);
  146. //File Name only
  147. var filename = thisFile.split("/").pop()
  148. filename = filename.split(".");
  149. var ext = filename.pop();
  150. filename = filename.join(".");
  151. thisSongData.push(filename);
  152. //File Extension
  153. thisSongData.push(ext);
  154. //File size
  155. var fileSize = bytesToSize(filelib.filesize(thisFile))
  156. thisSongData.push(fileSize)
  157. songData.push(thisSongData);
  158. }
  159. }
  160. //Send resp
  161. sendJSONResp(JSON.stringify(songData));
  162. //Run build cache in background so to update any cache if exists
  163. execd("buildCache.js")
  164. }else{
  165. //WIP
  166. console.log("listSong type " + listSong + " work in progress")
  167. }
  168. }else if (typeof(listdir) != "undefined"){
  169. //List dir given path
  170. if (listdir == "root"){
  171. //List the information of the user roots that contain Music folder
  172. var rootInfo = [];
  173. for (var i =0; i < musicDis.length; i++){
  174. var thisRootInfo = [];
  175. var thisMusicDir = musicDis[i];
  176. var thisRoot = thisMusicDir.split("/").shift() + "/";
  177. var objcetsInRoot = [];
  178. if (thisRoot == "user:/"){
  179. objcetsInRoot = filelib.glob(thisRoot + "Music/*");
  180. }else{
  181. objcetsInRoot = filelib.glob(thisRoot + "*");
  182. thisMusicDir = thisRoot;
  183. }
  184. var rootName = filelib.rname(thisRoot);
  185. if (rootName == false){
  186. rootName = thisRoot
  187. }
  188. thisRootInfo.push(rootName);
  189. thisRootInfo.push(thisMusicDir);
  190. var files = [];
  191. var folders = [];
  192. for (var j = 0; j < objcetsInRoot.length; j++){
  193. if (filelib.isDir(objcetsInRoot[j])){
  194. folders.push(objcetsInRoot[j]);
  195. }else{
  196. files.push(objcetsInRoot[j]);
  197. }
  198. }
  199. thisRootInfo.push(files.length);
  200. thisRootInfo.push(folders.length);
  201. rootInfo.push(thisRootInfo);
  202. }
  203. sendJSONResp(JSON.stringify(rootInfo));
  204. }else{
  205. //List information about other folders
  206. var targetpath = decodeURIComponent(listdir)
  207. var filelist = filelib.aglob(targetpath + "*")
  208. var files = [];
  209. var folders = [];
  210. for (var j = 0; j < filelist.length; j++){
  211. if (filelib.isDir(filelist[j])){
  212. folders.push(filelist[j]);
  213. }else{
  214. var ext = filelist[j].split(".").pop();
  215. if (IsSupportExt(ext) && !IsMetaFile(filelist[j])){
  216. files.push(filelist[j]);
  217. }
  218. }
  219. }
  220. //For each folder, get its information
  221. var folderInfo = [];
  222. for (var i = 0; i < folders.length; i++){
  223. var thisFolderInfo = [];
  224. var folderName = folders[i].split("/").pop();
  225. var thisFolderSubfolder = [];
  226. var thisFolderSubfiles = [];
  227. var subFolderFileList = filelib.aglob(folders[i] + "/*")
  228. for (var j = 0; j < subFolderFileList.length; j++){
  229. if (filelib.isDir(subFolderFileList[j])){
  230. var thisFolderName = subFolderFileList[j].split("/").pop();
  231. if (thisFolderName.substring(0,1) != "."){
  232. thisFolderSubfolder.push(subFolderFileList[j]);
  233. }
  234. }else{
  235. var ext = subFolderFileList[j].split(".").pop();
  236. if (IsSupportExt(ext) && !IsMetaFile(subFolderFileList[j])){
  237. thisFolderSubfiles.push(subFolderFileList[j]);
  238. }
  239. }
  240. }
  241. thisFolderInfo.push(folderName);
  242. thisFolderInfo.push(folders[i] + "/");
  243. thisFolderInfo.push((thisFolderSubfiles).length + "");
  244. thisFolderInfo.push((thisFolderSubfolder).length + "");
  245. folderInfo.push(thisFolderInfo)
  246. }
  247. var fileInfo = [];
  248. for (var i = 0; i < files.length; i++){
  249. var thisFileInfo = [];
  250. var filename = files[i].split("/").pop();
  251. filename = filename.split('.')
  252. filename.pop();
  253. filename = filename.join(".");
  254. var ext = files[i].split(".").pop()
  255. var filesize = filelib.filesize(files[i]);
  256. filesize = bytesToSize(filesize);
  257. thisFileInfo.push("/media?file=" + files[i]);
  258. thisFileInfo.push(filename);
  259. thisFileInfo.push(ext);
  260. thisFileInfo.push(filesize);
  261. fileInfo.push(thisFileInfo);
  262. }
  263. var results = [];
  264. results.push(folderInfo);
  265. results.push(fileInfo);
  266. sendJSONResp(JSON.stringify(results));
  267. }
  268. }else if (typeof(listFolder) != "undefined"){
  269. //List folder giben filepath
  270. }
  271. }
  272. }
  273. //Execute Handler
  274. handleUserRequest();