static Stream HandleRequest (string request, Catalog catalog, long? rangeStart, long? rangeEnd) { LoggingService.LogInfo ("Handling request: {0}", request); if (request.StartsWith (Messages.FetchTrack, StringComparison.Ordinal)) { var message = Newtonsoft.Json.JsonConvert.DeserializeObject<FetchTrackMessage> (request.Substring (Messages.FetchTrack.Length)); var track = catalog.FindTrack (message.UUID); var stream = new MemoryStream (File.ReadAllBytes (track.FilePath), true); stream.Position = rangeStart ?? message.Offset; stream.SetLength (rangeEnd ?? stream.Length); return stream; } else if (request.StartsWith (Messages.FetchAlbumArt)) { var message = Newtonsoft.Json.JsonConvert.DeserializeObject<FetchAlbumArtMessage> (request.Substring (Messages.FetchAlbumArt.Length)); var track = catalog.First (t => t.AlbumArtist == message.AlbumArtist && t.Album == message.Album); return File.OpenRead (track.AlbumArt); } else if (request.StartsWith (Messages.FetchArtistArt)) { var message = Newtonsoft.Json.JsonConvert.DeserializeObject<FetchArtistArtMessage> (request.Substring (Messages.FetchArtistArt.Length)); var track = catalog.First (t => t.AlbumArtist == message.AlbumArtist && !string.IsNullOrEmpty (t.AlbumArt)); return File.OpenRead (track.AlbumArt); } else if (request.StartsWith (Messages.FetchCatalog)) { var message = Newtonsoft.Json.JsonConvert.DeserializeObject<FetchCatalogMessage> (request.Substring (Messages.FetchCatalog.Length)); if (message?.UUID == catalog.UUID) return new MemoryStream (new byte [0]); return new MemoryStream (catalog.ToJson ()); } else if (request.StartsWith (Messages.FetchGzipCompressedCatalog)) { var message = Newtonsoft.Json.JsonConvert.DeserializeObject<FetchCatalogMessage> (request.Substring (Messages.FetchGzipCompressedCatalog.Length)); if (message?.UUID == catalog.UUID) return new MemoryStream (new byte [0]); return new MemoryStream (catalog.ToGzipCompressedJson ()); } else { return Stream.Null; } }