buildCache.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. AirMusic
  3. Cache builder
  4. This script build the music bank ALL SONG caching and store it in db
  5. */
  6. //Include the common library
  7. var succ = includes("common.js")
  8. if (succ == false){
  9. console.log("Failed to load common.js");
  10. }
  11. //Require the filelib
  12. requirelib("filelib");
  13. //Generate the Music Directory
  14. var musicDis = [];
  15. //Make the user's root music folder if not exists
  16. if (filelib.fileExists("user:/Music/") == false){
  17. filelib.mkdir("user:/Music/");
  18. }
  19. //Scan all music directories
  20. var rootDirs = filelib.glob("/");
  21. for (var i = 0; i < rootDirs.length; i++){
  22. var thisRoot = rootDirs[i];
  23. musicDis.push(thisRoot);
  24. }
  25. function buildCache(){
  26. //Create db if not exists
  27. newDBTableIfNotExists("AirMusic");
  28. //Do the scanning
  29. var songData = [];
  30. var musicFiles = [];
  31. for (var i = 0; i < musicDis.length; i++){
  32. var thisFileLib = musicDis[i];
  33. var allfilelist = filelib.walk(thisFileLib, "file");
  34. for (var k = 0; k < allfilelist.length; k++){
  35. var ext = allfilelist[k].split('.').pop();
  36. if (IsSupportExt(ext) == true){
  37. musicFiles.push(allfilelist[k]);
  38. }
  39. }
  40. }
  41. for (var i = 0; i < musicFiles.length; i++){
  42. var thisMusicFile = musicFiles[i];
  43. var thisSongData = [];
  44. //Access Path
  45. thisSongData.push("/media?file=" + thisMusicFile);
  46. //File Name only
  47. var filename = thisMusicFile.split("/").pop()
  48. filename = filename.split(".");
  49. var ext = filename.pop();
  50. filename = filename.join(".");
  51. thisSongData.push(filename);
  52. //File Extension
  53. thisSongData.push(ext);
  54. //File size
  55. var fileSize = bytesToSize(filelib.filesize(thisMusicFile))
  56. thisSongData.push(fileSize)
  57. songData.push(thisSongData);
  58. }
  59. //Save it as cache
  60. writeDBItem("AirMusic", "cache", JSON.stringify(songData));
  61. sendResp("OK");
  62. }
  63. buildCache();