From e1958e3902ae81931c82a31f355cefc2b02a8a2d Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Sun, 5 Apr 2020 15:56:08 -0400 Subject: [PATCH] Miscellaneous code cleanup --- MediaBrowser.Api/ItemLookupService.cs | 10 ++- MediaBrowser.Api/Library/LibraryService.cs | 75 +++++-------------- .../Playback/Hls/BaseHlsService.cs | 17 ++--- .../UserLibrary/BaseItemsByNameService.cs | 31 +++----- MediaBrowser.Api/VideosService.cs | 6 +- 5 files changed, 44 insertions(+), 95 deletions(-) diff --git a/MediaBrowser.Api/ItemLookupService.cs b/MediaBrowser.Api/ItemLookupService.cs index a76369a157..5a7b1f37ae 100644 --- a/MediaBrowser.Api/ItemLookupService.cs +++ b/MediaBrowser.Api/ItemLookupService.cs @@ -305,9 +305,15 @@ namespace MediaBrowser.Api 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); + 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)); diff --git a/MediaBrowser.Api/Library/LibraryService.cs b/MediaBrowser.Api/Library/LibraryService.cs index 291242e458..9251343e8b 100644 --- a/MediaBrowser.Api/Library/LibraryService.cs +++ b/MediaBrowser.Api/Library/LibraryService.cs @@ -544,8 +544,7 @@ namespace MediaBrowser.Api.Library foreach (var type in types) { - ImageOption[] defaultImageOptions = null; - TypeOptions.DefaultImageOptions.TryGetValue(type, out defaultImageOptions); + TypeOptions.DefaultImageOptions.TryGetValue(type, out var defaultImageOptions); typeOptions.Add(new LibraryTypeOptions { @@ -592,8 +591,6 @@ namespace MediaBrowser.Api.Library public object Get(GetSimilarItems request) { - var user = !request.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(request.UserId) : null; - var item = string.IsNullOrEmpty(request.Id) ? (!request.UserId.Equals(Guid.Empty) ? _libraryManager.GetUserRootFolder() : _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id); @@ -651,7 +648,7 @@ namespace MediaBrowser.Api.Library // ExcludeArtistIds if (!string.IsNullOrEmpty(request.ExcludeArtistIds)) { - query.ExcludeArtistIds = BaseApiService.GetGuids(request.ExcludeArtistIds); + query.ExcludeArtistIds = GetGuids(request.ExcludeArtistIds); } List itemsResult; @@ -672,7 +669,6 @@ namespace MediaBrowser.Api.Library var result = new QueryResult { Items = returnList, - TotalRecordCount = itemsResult.Count }; @@ -1075,36 +1071,18 @@ namespace MediaBrowser.Api.Library throw new ResourceNotFoundException("Item not found."); } - BaseItem[] themeItems = Array.Empty(); + IEnumerable themeItems = item.GetThemeSongs(); - while (true) + while (!themeItems.Any() && request.InheritFromParent && item.GetParent() != null) { - themeItems = item.GetThemeSongs().ToArray(); - - if (themeItems.Length > 0) - { - break; - } - - if (!request.InheritFromParent) - { - break; - } - - var parent = item.GetParent(); - if (parent == null) - { - break; - } - item = parent; + item = item.GetParent(); + themeItems = item.GetThemeSongs(); } var dtoOptions = GetDtoOptions(_authContext, request); - - var dtos = themeItems - .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)); - - var items = dtos.ToArray(); + var items = themeItems + .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) + .ToArray(); return new ThemeMediaResult { @@ -1121,9 +1099,7 @@ namespace MediaBrowser.Api.Library /// System.Object. public object Get(GetThemeVideos request) { - var result = GetThemeVideos(request); - - return ToOptimizedResult(result); + return ToOptimizedResult(GetThemeVideos(request)); } public ThemeMediaResult GetThemeVideos(GetThemeVideos request) @@ -1141,36 +1117,19 @@ namespace MediaBrowser.Api.Library throw new ResourceNotFoundException("Item not found."); } - BaseItem[] themeItems = Array.Empty(); + IEnumerable themeItems = item.GetThemeVideos(); - while (true) + while (!themeItems.Any() && request.InheritFromParent && item.GetParent() != null) { - themeItems = item.GetThemeVideos().ToArray(); - - if (themeItems.Length > 0) - { - break; - } - - if (!request.InheritFromParent) - { - break; - } - - var parent = item.GetParent(); - if (parent == null) - { - break; - } - item = parent; + item = item.GetParent(); + themeItems = item.GetThemeVideos(); } var dtoOptions = GetDtoOptions(_authContext, request); - var dtos = themeItems - .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)); - - var items = dtos.ToArray(); + var items = themeItems + .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) + .ToArray(); return new ThemeMediaResult { diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs index 566eaf86ea..8d5d2279ea 100644 --- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -240,17 +240,12 @@ namespace MediaBrowser.Api.Playback.Hls protected Stream GetPlaylistFileStream(string path) { - var tmpPath = path + ".tmp"; - tmpPath = path; - - try - { - return new FileStream(tmpPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.SequentialScan); - } - catch (IOException) - { - return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.SequentialScan); - } + return new FileStream(path, + FileMode.Open, + FileAccess.Read, + FileShare.ReadWrite, + IODefaults.FileStreamBufferSize, + FileOptions.SequentialScan); } protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding) diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 7e4e589ea0..c4a52d5f52 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -273,7 +273,7 @@ namespace MediaBrowser.Api.UserLibrary DtoOptions = dtoOptions }; - Func filter = i => FilterItem(request, i, excludeItemTypes, includeItemTypes, mediaTypes); + bool Filter(BaseItem i) => FilterItem(request, i, excludeItemTypes, includeItemTypes, mediaTypes); if (parentItem.IsFolder) { @@ -283,18 +283,18 @@ namespace MediaBrowser.Api.UserLibrary { items = request.Recursive ? folder.GetRecursiveChildren(user, query).ToList() : - folder.GetChildren(user, true).Where(filter).ToList(); + folder.GetChildren(user, true).Where(Filter).ToList(); } else { items = request.Recursive ? - folder.GetRecursiveChildren(filter) : - folder.Children.Where(filter).ToList(); + folder.GetRecursiveChildren(Filter) : + folder.Children.Where(Filter).ToList(); } } else { - items = new[] { parentItem }.Where(filter).ToList(); + items = new[] { parentItem }.Where(Filter).ToList(); } var extractedItems = GetAllItems(request, items); @@ -345,30 +345,21 @@ namespace MediaBrowser.Api.UserLibrary private bool FilterItem(GetItemsByName request, BaseItem f, string[] excludeItemTypes, string[] includeItemTypes, string[] mediaTypes) { // Exclude item types - if (excludeItemTypes.Length > 0) + if (excludeItemTypes.Length > 0 && excludeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase)) { - if (excludeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase)) - { - return false; - } + return false; } // Include item types - if (includeItemTypes.Length > 0) + if (includeItemTypes.Length > 0 && !includeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase)) { - if (!includeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase)) - { - return false; - } + return false; } // Include MediaTypes - if (mediaTypes.Length > 0) + if (mediaTypes.Length > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase)) { - if (!mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase)) - { - return false; - } + return false; } return true; diff --git a/MediaBrowser.Api/VideosService.cs b/MediaBrowser.Api/VideosService.cs index 9d7dc2dfc6..76e9f421d0 100644 --- a/MediaBrowser.Api/VideosService.cs +++ b/MediaBrowser.Api/VideosService.cs @@ -138,10 +138,8 @@ namespace MediaBrowser.Api var videosWithVersions = items.Where(i => i.MediaSourceCount > 1) .ToList(); - var primaryVersion = videosWithVersions.FirstOrDefault() ?? items.OrderBy(i => - { - return (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile) ? 1 : 0; - }) + var primaryVersion = videosWithVersions.FirstOrDefault() ?? items.OrderBy(i + => (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile) ? 1 : 0) .ThenByDescending(i => { var stream = i.GetDefaultVideoStream();