Kaynağa Gözat

Fixed bugs in AirMusic for duplicated filenames in search results

TC pushbot 5 4 yıl önce
ebeveyn
işleme
009e28a5cf
1 değiştirilmiş dosya ile 51 ekleme ve 39 silme
  1. 51 39
      web/Music/functions/listSong.js

+ 51 - 39
web/Music/functions/listSong.js

@@ -126,53 +126,65 @@ function handleUserRequest(){
                 //Search mode
                 var keyword = listSong.substr(7)
                 var songData = [];
-                for (var i = 0; i < musicDis.length; i++){
-                    var thisFileLib = musicDis[i];
 
-                    //Load the music list from cache to reduce scanning time
-                    var cachedList = readDBItem("AirMusic", "cache");
+                var cachedList = readDBItem("AirMusic", "cache");
+                var allfilelist = [];
+
+                function getRealtimeAllFleList(){
                     var allfilelist = [];
-                    if (cachedList == ""){
-                        allfilelist  = filelib.walk(thisFileLib, "file");
-                    }else{
-                        //Try parse it. If parse failed fallback to realtime scanning
-                        try{    
-                            cachedList = JSON.parse(cachedList);
-                            for (var j = 0; j < cachedList.length; j++){
-                                var thisCachedSong = cachedList[j];
-                                allfilelist.push(thisCachedSong[0].replace("/media?file=", ""));
-                            }
-                        }catch(ex){
-                            allfilelist  = filelib.walk(thisFileLib, "file");
+                    for (var i = 0; i < musicDis.length; i++){
+                        var thisFileLib = musicDis[i];
+                        thisDirList  = filelib.walk(thisFileLib, "file");
+                        for (var j = 0; j < thisDirList.length; j++){
+                            allfilelist.push(thisDirList[j]);
                         }
                     }
-                    
-                    for (var k = 0; k < allfilelist.length; k++){
-                        var thisFile = allfilelist[k];
-                        var ext = allfilelist[k].split('.').pop();
-                        var filename = allfilelist[k].split('/').pop();
-                        if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1){
-                            //This file match our ext req and keyword exists
-                            var thisSongData = [];
-                            //Access Path 
-                            thisSongData.push("/media?file=" + thisFile);
-                            //File Name only
-                            var filename = thisFile.split("/").pop()
-                            filename = filename.split(".");
-                            var ext = filename.pop();
-                            filename = filename.join(".");
-                            thisSongData.push(filename);
-                            //File Extension
-                            thisSongData.push(ext);
-                            //File size
-                            var fileSize = bytesToSize(filelib.filesize(thisFile))
-                            thisSongData.push(fileSize)
-            
-                            songData.push(thisSongData);
+                    return allfilelist;
+                }
 
+                if (cachedList == ""){
+                    //No cache. Do real time scanning
+                    allfilelist = getRealtimeAllFleList();
+                }else{
+                    //Try parse it. If parse failed fallback to realtime scanning
+                    try{    
+                        cachedList = JSON.parse(cachedList);
+                        for (var j = 0; j < cachedList.length; j++){
+                            var thisCachedSong = cachedList[j];
+                            allfilelist.push(thisCachedSong[0].replace("/media?file=", ""));
                         }
+                    }catch(ex){
+                        //Fallback
+                        allfilelist = getRealtimeAllFleList();
+                    }
+                }
+                    
+                for (var k = 0; k < allfilelist.length; k++){
+                    var thisFile = allfilelist[k];
+                    var ext = allfilelist[k].split('.').pop();
+                    var filename = allfilelist[k].split('/').pop();
+                    if (IsSupportExt(ext) == true && filename.indexOf(keyword) !== -1){
+                        //This file match our ext req and keyword exists
+                        var thisSongData = [];
+                        //Access Path 
+                        thisSongData.push("/media?file=" + thisFile);
+                        //File Name only
+                        var filename = thisFile.split("/").pop()
+                        filename = filename.split(".");
+                        var ext = filename.pop();
+                        filename = filename.join(".");
+                        thisSongData.push(filename);
+                        //File Extension
+                        thisSongData.push(ext);
+                        //File size
+                        var fileSize = bytesToSize(filelib.filesize(thisFile))
+                        thisSongData.push(fileSize)
+        
+                        songData.push(thisSongData);
+
                     }
                 }
+                
 
                 //Send resp
                 sendJSONResp(JSON.stringify(songData));