playlist.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Playlist.js
  3. List and manage the playlist
  4. database schema
  5. AirMusic playlist/USERNAME/{playlistname} --> JSON Stringify Object
  6. Song Objects
  7. [playbackpath, name, type (ext), filesize(Human readable)]
  8. Require paramters:
  9. opr = {root / list / add / remove}
  10. under list mode:
  11. playlistname = "playlistname"
  12. under add / remove
  13. playlistname = "playlistname"
  14. musicpath = "songpath"
  15. */
  16. //Include common functions
  17. includes("common.js")
  18. requirelib("filelib");
  19. function sendErrorResp(msg){
  20. sendJSONResp(JSON.stringify({
  21. error: msg
  22. }));
  23. }
  24. function playlistExists(name){
  25. var targetPlaylist = readDBItem("AirMusic", name);
  26. if (targetPlaylist == ""){
  27. return false
  28. }
  29. return true
  30. }
  31. function handlePlaylistRequest(){
  32. //Create table if not exists
  33. newDBTableIfNotExists("AirMusic");
  34. if (opr == "root"){
  35. //List the root of all playlist
  36. var playlists = listDBTable("AirMusic");
  37. var keys = Object.keys(playlists);
  38. var playlistInfo = [];
  39. for (var i =0; i < keys.length; i++){
  40. var thiskey = keys[i];
  41. if (thiskey.indexOf("playlist/" + USERNAME + "/") > -1){
  42. //This key contains the keyword "playlist/", this is a playlist object
  43. playlistInfo.push({
  44. name: thiskey.split("/").pop(),
  45. count: JSON.parse(playlists[thiskey]).length
  46. });
  47. }
  48. }
  49. sendJSONResp(JSON.stringify(playlistInfo));
  50. }else if (opr == "list"){
  51. //List a playlist given its name
  52. if (playlistname == undefined){
  53. sendErrorResp("Playlist name undefined");
  54. return
  55. }
  56. var playlistFullKey = "playlist/" + USERNAME + "/" + playlistname;
  57. //Check if playlist name exists
  58. if (!playlistExists(playlistFullKey)){
  59. sendErrorResp("Playlist not exists");
  60. return
  61. }
  62. //OK. Load and display the playlist information
  63. var songsInList = [];
  64. var targetPlaylist = readDBItem("AirMusic", playlistFullKey);
  65. targetPlaylist = JSON.parse(targetPlaylist);
  66. //Prase the results
  67. for (var i = 0; i < targetPlaylist.length; i++){
  68. var thisSong = targetPlaylist[i];
  69. if (filelib.fileExists(thisSong)){
  70. //Add it to the list
  71. //This section is used to handle songs in unmounted storage ppols
  72. var songInfo = thisSong.split("/").pop();
  73. songInfo = songInfo.split(".");
  74. var songName = songInfo[0];
  75. var songExt = songInfo[1];
  76. songsInList.push([
  77. "/media?file=" + thisSong,
  78. songName,
  79. songExt,
  80. bytesToSize(filelib.filesize(thisSong))
  81. ]);
  82. }
  83. }
  84. sendJSONResp(JSON.stringify(songsInList));
  85. }else if (opr == "add"){
  86. //Adding a song base on its path from playlist
  87. if (playlistname == undefined || musicpath == undefined){
  88. sendErrorResp("Playlist name (playlistname) and music filepath (song) not defined");
  89. return
  90. }
  91. //Check if the path exists
  92. if (!filelib.fileExists(musicpath)){
  93. //File not exits. Reject
  94. sendErrorResp("Given filepath not exists")
  95. return
  96. }
  97. //OK! Add it to the playlist
  98. var playlistFullKey = "playlist/" + USERNAME + "/" + playlistname;
  99. if (!playlistExists(playlistFullKey)){
  100. //Playlist not exists. Creates it
  101. writeDBItem("AirMusic", playlistFullKey, JSON.stringify([musicpath]));
  102. }else{
  103. //Playlist already exists. Extract and append to it
  104. var targetPlaylist = readDBItem("AirMusic", playlistFullKey);
  105. //Convert it to an array
  106. targetPlaylist = JSON.parse(targetPlaylist);
  107. //Check if this song already in playlist
  108. for (var i = 0; i < targetPlaylist.length; i++){
  109. if (targetPlaylist[i] == musicpath){
  110. //Already in playlist. Return
  111. sendResp("OK");
  112. return;
  113. }
  114. }
  115. //Push the new filepath into it
  116. targetPlaylist.push(musicpath);
  117. //Convert it back to string
  118. targetPlaylist = JSON.stringify(targetPlaylist);
  119. //Write to database item
  120. writeDBItem("AirMusic", playlistFullKey, targetPlaylist);
  121. }
  122. //Reply OK
  123. sendResp("OK");
  124. }else if (opr == "remove"){
  125. //Removing a song from a playlist
  126. if (playlistname == undefined || musicpath == undefined){
  127. sendErrorResp("Playlist name (playlistname) and music filepath (song) not defined");
  128. return
  129. }
  130. var playlistFullKey = "playlist/" + USERNAME + "/" + playlistname;
  131. //Check if playlist exists
  132. if (!playlistExists(playlistFullKey)){
  133. sendErrorResp("Playlist not exists")
  134. return
  135. }
  136. //Remove this song from the playlist
  137. var targetPlaylist = readDBItem("AirMusic", playlistFullKey);
  138. targetPlaylist = JSON.parse(targetPlaylist);
  139. var newPlaylist = [];
  140. for (var i = 0; i < targetPlaylist.length; i++){
  141. var thisSongPath = targetPlaylist[i];
  142. if (thisSongPath != musicpath){
  143. //console.log(thisSongPath, musicpath);
  144. newPlaylist.push(thisSongPath);
  145. }
  146. }
  147. //Check if there are items in the playlist. IF not, remove it completely
  148. if (newPlaylist.length == 0){
  149. deleteDBItem("AirMusic", playlistFullKey);
  150. }else{
  151. //Write to database
  152. newPlaylist = JSON.stringify(newPlaylist);
  153. writeDBItem("AirMusic", playlistFullKey, newPlaylist);
  154. }
  155. sendResp("OK");
  156. }else{
  157. //Unknown operations
  158. sendErrorResp("Unknown operation type");
  159. }
  160. }
  161. //Execute main function
  162. handlePlaylistRequest();