Forráskód Böngészése

Added automatic expire cache clearning

tobychui 2 éve
szülő
commit
3095a3c8ed
1 módosított fájl, 40 hozzáadás és 1 törlés
  1. 40 1
      web/Music/index.html

+ 40 - 1
web/Music/index.html

@@ -1509,7 +1509,8 @@
 					$("#mainAudioPlayer").attr("src", playbackURL);
 					startPlaybackAfterAudioLoaded(audio, playAfterLoad);
 
-					if (ext == "mp3"){
+					//Only cache non video audio tracks
+					if (ext == "mp3" || ext == "flac" || ext == "aac" || ext == "wav" || ext == "ogg"){
 						loadAudioFileURLAsBlob(playbackURL, function(fileBlob){
 							//Store the blob into indexDB
 							let cacheDbTx2 = cacheDb.transaction("files","readwrite");
@@ -2299,7 +2300,42 @@
 		clearReq.onsuccess = function(e){
 			console.log("All cache file cleared");
 		}
+	}
+
+	function removeCacheByFilename(filename, callback=undefined){
+		let dbtx = cacheDb.transaction("files","readwrite");
+		let fstore = dbtx.objectStore("files");
+		let resp = fstore.delete(filename);
+		resp.onsuccess = function(evt){
+			if (callback != undefined){
+				callback(evt);
+			}
+		}
+	}
+
+	//Clear all expired cache
+	function clearExpiredCache(){
+		var expireTime = 604800; //1 week
 		
+		getAllItems(function(allCachedTracks){
+			var expiredFilenames = [];
+
+			//Check all the cache to see which is expired
+			allCachedTracks.forEach(function(track){
+				if (track.cachetime + expireTime < Date.now()/1000){
+					//Expired. Clear this cache
+					expiredFilenames.push(track.filename);
+					console.log("Expired track cleared: ", track);
+				}
+				
+			});
+
+			
+			//Delete those cache that have expired
+			expiredFilenames.forEach(function(filenameToBeDeleted){
+				removeCacheByFilename(filenameToBeDeleted);
+			})l
+		});
 	}
 
 	function getAllItems(callback) {
@@ -2726,6 +2762,9 @@
     }
     window.addEventListener('popstate', handleBackButton);
     window.history.pushState({}, '');
+
+	//Clear expired cache
+	clearExpiredCache();
 </script>
 </body>
 </html>