listSong.js 12 KB

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