private static void HelpMeSmall(HttpListenerResponse response, string tempId, ISongPlayer songPlayer) { if (thumbnailBuffer.ContainsKey(tempId)) { WriteImage(response, thumbnailBuffer[tempId]); return; } // get from player MemoryStream imageStream = null; if (!string.IsNullOrEmpty(tempId)) { try { // this is locked in function imageStream = songPlayer.GetImageStream(tempId, false); } catch (Exception) { imageStream = null; } using (MemoryStream streamFromSongPlayer = imageStream) { if (streamFromSongPlayer != null) { if (!thumbnailBuffer.ContainsKey(tempId)) { thumbnailBuffer.Add(tempId, streamFromSongPlayer.ToArray()); } WriteImage(response, streamFromSongPlayer); return; } } } // cache small if not present if (_emptyImageSmall == null) { using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("SongRequest.Static.empty_small.png")) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); _emptyImageSmall = memoryStream.ToArray(); } } if (!thumbnailBuffer.ContainsKey(tempId)) { thumbnailBuffer.Add(tempId, _emptyImageSmall); } WriteImage(response, _emptyImageSmall); }
private static void HelpMeLarge(HttpListenerResponse response, string tempId, ISongPlayer songPlayer) { // use cached image if possible if (tempId.Equals(_lastId, StringComparison.OrdinalIgnoreCase) && _lastImage != null) { WriteImage(response, _lastImage); return; } // get from player MemoryStream imageStream = null; if (!string.IsNullOrEmpty(tempId)) { try { // this is locked in function imageStream = songPlayer.GetImageStream(tempId, true); } catch (Exception) { imageStream = null; } using (MemoryStream streamFromSongPlayer = imageStream) { if (streamFromSongPlayer != null) { // set last id _lastId = tempId; _lastImage = streamFromSongPlayer.ToArray(); WriteImage(response, _lastImage); return; } } } // cache large if not present if (_emptyImageLarge == null) { using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("SongRequest.Static.empty.png")) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); _emptyImageLarge = memoryStream.ToArray(); } } WriteImage(response, _emptyImageLarge); }
private static void HelpMeSmall(HttpListenerResponse response, string tempId, ISongPlayer songPlayer) { if (thumbnailBuffer.ContainsKey(tempId)) { WriteImage(response, thumbnailBuffer[tempId]); return; } // get from player MemoryStream imageStream = null; if (!string.IsNullOrEmpty(tempId)) { try { // this is locked in function imageStream = songPlayer.GetImageStream(tempId, false); } catch (Exception) { imageStream = null; } using (MemoryStream streamFromSongPlayer = imageStream) { if (streamFromSongPlayer != null) { if (!thumbnailBuffer.ContainsKey(tempId)) thumbnailBuffer.Add(tempId, streamFromSongPlayer.ToArray()); WriteImage(response, streamFromSongPlayer); return; } } } // cache small if not present if (_emptyImageSmall == null) { using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("SongRequest.Static.empty_small.png")) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); _emptyImageSmall = memoryStream.ToArray(); } } if (!thumbnailBuffer.ContainsKey(tempId)) thumbnailBuffer.Add(tempId, _emptyImageSmall); WriteImage(response, _emptyImageSmall); }
private static void HelpMeLarge(HttpListenerResponse response, string tempId, ISongPlayer songPlayer) { int size = 300; string cacheFolder = Path.Combine(_imageCachePath, tempId.Length > 2 ? tempId.Substring(0, 2) : tempId); string cacheKey = Path.Combine(cacheFolder, tempId + "_" + size + ".png"); if (File.Exists(cacheKey)) { using (FileStream fileStream = File.OpenRead(cacheKey)) { WriteImage(response, fileStream); return; } } lock (_lockObject) { // get from player MemoryStream imageStream = null; if (!string.IsNullOrEmpty(tempId)) { try { // this is locked in function imageStream = songPlayer.GetImageStream(tempId, size); } catch (Exception) { imageStream = null; } using (MemoryStream streamFromSongPlayer = imageStream) { if (streamFromSongPlayer != null) { if (!Directory.Exists(cacheFolder)) Directory.CreateDirectory(cacheFolder); using (FileStream fileStream = File.Create(cacheKey)) { streamFromSongPlayer.CopyTo(fileStream); } WriteImage(response, streamFromSongPlayer); return; } } } // cache large if not present if (_emptyImageLarge == null) { using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("SongRequest.Static.empty.png")) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); _emptyImageLarge = memoryStream.ToArray(); } } WriteImage(response, _emptyImageLarge); } }
private static void HelpMeLarge(HttpListenerResponse response, string tempId, ISongPlayer songPlayer) { int size = 300; string cacheFolder = Path.Combine(_imageCachePath, tempId.Length > 2 ? tempId.Substring(0, 2) : tempId); string cacheKey = Path.Combine(cacheFolder, tempId + "_" + size + ".png"); if (File.Exists(cacheKey)) { using (FileStream fileStream = File.OpenRead(cacheKey)) { WriteImage(response, fileStream); return; } } lock (_lockObject) { // get from player MemoryStream imageStream = null; if (!string.IsNullOrEmpty(tempId)) { try { // this is locked in function imageStream = songPlayer.GetImageStream(tempId, size); } catch (Exception) { imageStream = null; } using (MemoryStream streamFromSongPlayer = imageStream) { if (streamFromSongPlayer != null) { if (!Directory.Exists(cacheFolder)) { Directory.CreateDirectory(cacheFolder); } using (FileStream fileStream = File.Create(cacheKey)) { streamFromSongPlayer.CopyTo(fileStream); } WriteImage(response, streamFromSongPlayer); return; } } } // cache large if not present if (_emptyImageLarge == null) { using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("SongRequest.Static.empty.png")) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); _emptyImageLarge = memoryStream.ToArray(); } } WriteImage(response, _emptyImageLarge); } }