From 6a3f9253db8b7886e020d84ed82d9ad45f3d3350 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Sun, 5 Apr 2020 12:26:11 -0400 Subject: [PATCH] Convert using statements to using declarations where applicable --- MediaBrowser.Api/Images/ImageService.cs | 24 ++++----- MediaBrowser.Api/Images/RemoteImageService.cs | 26 +++++----- MediaBrowser.Api/LiveTv/LiveTvService.cs | 9 ++-- .../Playback/Hls/BaseHlsService.cs | 51 ++++++++----------- MediaBrowser.Api/Subtitles/SubtitleService.cs | 15 +++--- 5 files changed, 55 insertions(+), 70 deletions(-) diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index af455987bc..7b94ffae4e 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -743,24 +743,22 @@ namespace MediaBrowser.Api.Images /// Task. public async Task PostImage(BaseItem entity, Stream inputStream, ImageType imageType, string mimeType) { - using (var reader = new StreamReader(inputStream)) - { - var text = await reader.ReadToEndAsync().ConfigureAwait(false); + using var reader = new StreamReader(inputStream); + var text = await reader.ReadToEndAsync().ConfigureAwait(false); - var bytes = Convert.FromBase64String(text); + var bytes = Convert.FromBase64String(text); - var memoryStream = new MemoryStream(bytes) - { - Position = 0 - }; + var memoryStream = new MemoryStream(bytes) + { + Position = 0 + }; - // Handle image/png; charset=utf-8 - mimeType = mimeType.Split(';').FirstOrDefault(); + // Handle image/png; charset=utf-8 + mimeType = mimeType.Split(';').FirstOrDefault(); - await _providerManager.SaveImage(entity, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false); + await _providerManager.SaveImage(entity, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false); - entity.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None); - } + entity.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None); } } } diff --git a/MediaBrowser.Api/Images/RemoteImageService.cs b/MediaBrowser.Api/Images/RemoteImageService.cs index f03f5efd8d..222bb34d31 100644 --- a/MediaBrowser.Api/Images/RemoteImageService.cs +++ b/MediaBrowser.Api/Images/RemoteImageService.cs @@ -261,27 +261,25 @@ namespace MediaBrowser.Api.Images /// Task. private async Task DownloadImage(string url, Guid urlHash, string pointerCachePath) { - using (var result = await _httpClient.GetResponse(new HttpRequestOptions + using var result = await _httpClient.GetResponse(new HttpRequestOptions { Url = url, BufferContent = false - }).ConfigureAwait(false)) - { - var ext = result.ContentType.Split('/').Last(); - - var fullCachePath = GetFullCachePath(urlHash + "." + ext); + }).ConfigureAwait(false); + var ext = result.ContentType.Split('/').Last(); - Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath)); - using (var stream = result.Content) - using (var filestream = new FileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true)) - { - await stream.CopyToAsync(filestream).ConfigureAwait(false); - } + var fullCachePath = GetFullCachePath(urlHash + "." + ext); - Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath)); - File.WriteAllText(pointerCachePath, fullCachePath); + Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath)); + using (var stream = result.Content) + { + using var filestream = new FileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true); + await stream.CopyToAsync(filestream).ConfigureAwait(false); } + + Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath)); + File.WriteAllText(pointerCachePath, fullCachePath); } /// diff --git a/MediaBrowser.Api/LiveTv/LiveTvService.cs b/MediaBrowser.Api/LiveTv/LiveTvService.cs index c8bbbde469..5fe4c0cca3 100644 --- a/MediaBrowser.Api/LiveTv/LiveTvService.cs +++ b/MediaBrowser.Api/LiveTv/LiveTvService.cs @@ -885,11 +885,10 @@ namespace MediaBrowser.Api.LiveTv { // SchedulesDirect requires a SHA1 hash of the user's password // https://github.com/SchedulesDirect/JSON-Service/wiki/API-20141201#obtain-a-token - using (SHA1 sha = SHA1.Create()) - { - return Hex.Encode( - sha.ComputeHash(Encoding.UTF8.GetBytes(str))); - } + using SHA1 sha = SHA1.Create(); + + return Hex.Encode( + sha.ComputeHash(Encoding.UTF8.GetBytes(str))); } public void Delete(DeleteListingProvider request) diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs index 0cbfe4bdfa..af0097682a 100644 --- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -168,22 +168,19 @@ namespace MediaBrowser.Api.Playback.Hls private string GetLivePlaylistText(string path, int segmentLength) { - using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - using (var reader = new StreamReader(stream)) - { - var text = reader.ReadToEnd(); + using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = new StreamReader(stream); - text = text.Replace("#EXTM3U", "#EXTM3U\n#EXT-X-PLAYLIST-TYPE:EVENT"); + var text = reader.ReadToEnd(); - var newDuration = "#EXT-X-TARGETDURATION:" + segmentLength.ToString(CultureInfo.InvariantCulture); + text = text.Replace("#EXTM3U", "#EXTM3U\n#EXT-X-PLAYLIST-TYPE:EVENT"); - text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength - 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase); - //text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength + 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase); + var newDuration = "#EXT-X-TARGETDURATION:" + segmentLength.ToString(CultureInfo.InvariantCulture); - return text; - } - } + text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength - 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase); + //text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength + 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase); + + return text; } private string GetMasterPlaylistFileText(string firstPlaylist, int bitrate, int baselineStreamBitrate) @@ -212,29 +209,25 @@ namespace MediaBrowser.Api.Playback.Hls try { // Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written - using (var fileStream = GetPlaylistFileStream(playlist)) + using var fileStream = GetPlaylistFileStream(playlist); + using var reader = new StreamReader(fileStream); + var count = 0; + + while (!reader.EndOfStream) { - using (var reader = new StreamReader(fileStream)) - { - var count = 0; + var line = reader.ReadLine(); - while (!reader.EndOfStream) + if (line.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1) + { + count++; + if (count >= segmentCount) { - var line = reader.ReadLine(); - - if (line.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1) - { - count++; - if (count >= segmentCount) - { - Logger.LogDebug("Finished waiting for {0} segments in {1}", segmentCount, playlist); - return; - } - } + Logger.LogDebug("Finished waiting for {0} segments in {1}", segmentCount, playlist); + return; } - await Task.Delay(100, cancellationToken).ConfigureAwait(false); } } + await Task.Delay(100, cancellationToken).ConfigureAwait(false); } catch (IOException) { diff --git a/MediaBrowser.Api/Subtitles/SubtitleService.cs b/MediaBrowser.Api/Subtitles/SubtitleService.cs index c4a7ae78e7..f2968c6b5c 100644 --- a/MediaBrowser.Api/Subtitles/SubtitleService.cs +++ b/MediaBrowser.Api/Subtitles/SubtitleService.cs @@ -230,17 +230,14 @@ namespace MediaBrowser.Api.Subtitles if (string.Equals(request.Format, "vtt", StringComparison.OrdinalIgnoreCase) && request.AddVttTimeMap) { - using (var stream = await GetSubtitles(request).ConfigureAwait(false)) - { - using (var reader = new StreamReader(stream)) - { - var text = reader.ReadToEnd(); + using var stream = await GetSubtitles(request).ConfigureAwait(false); + using var reader = new StreamReader(stream); - text = text.Replace("WEBVTT", "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000"); + var text = reader.ReadToEnd(); - return ResultFactory.GetResult(Request, text, MimeTypes.GetMimeType("file." + request.Format)); - } - } + text = text.Replace("WEBVTT", "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000"); + + return ResultFactory.GetResult(Request, text, MimeTypes.GetMimeType("file." + request.Format)); } return ResultFactory.GetResult(Request, await GetSubtitles(request).ConfigureAwait(false), MimeTypes.GetMimeType("file." + request.Format));