From ce66df2c927c8c4d8fd40a01b8d42db9adcf29d9 Mon Sep 17 00:00:00 2001 From: Joe Rogers <1337joe@gmail.com> Date: Mon, 3 Jan 2022 22:51:28 +0100 Subject: [PATCH] Clean up warnings, simplify --- .../Manager/ItemImageProvider.cs | 94 +++++++++---------- 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/MediaBrowser.Providers/Manager/ItemImageProvider.cs b/MediaBrowser.Providers/Manager/ItemImageProvider.cs index b1d73c4c4..5f18578e4 100644 --- a/MediaBrowser.Providers/Manager/ItemImageProvider.cs +++ b/MediaBrowser.Providers/Manager/ItemImageProvider.cs @@ -99,7 +99,7 @@ namespace MediaBrowser.Providers.Manager public async Task RefreshImages( BaseItem item, LibraryOptions libraryOptions, - List providers, + IEnumerable providers, ImageRefreshOptions refreshOptions, CancellationToken cancellationToken) { @@ -159,14 +159,14 @@ namespace MediaBrowser.Providers.Manager foreach (var imageType in images) { - if (!IsEnabled(savedOptions, imageType)) + if (!savedOptions.IsEnabled(imageType)) { continue; } - if (!HasImage(item, imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType))) + if (!item.HasImage(imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType))) { - _logger.LogDebug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name); + _logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, item.Path ?? item.Name); var response = await provider.GetImage(item, imageType, cancellationToken).ConfigureAwait(false); @@ -176,7 +176,7 @@ namespace MediaBrowser.Providers.Manager { if (response.Protocol == MediaProtocol.Http) { - _logger.LogDebug("Setting image url into item {0}", item.Id); + _logger.LogDebug("Setting image url into item {Item}", item.Id); var index = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0; item.SetImage( new ItemImageInfo @@ -219,39 +219,6 @@ namespace MediaBrowser.Providers.Manager } } - private bool HasImage(BaseItem item, ImageType type) - { - return item.HasImage(type); - } - - /// - /// Determines if an item already contains the given images. - /// - /// The item. - /// The images. - /// The saved options. - /// The backdrop limit. - /// true if the specified item contains images; otherwise, false. - private bool ContainsImages(BaseItem item, List images, TypeOptions savedOptions, int backdropLimit) - { - // Using .Any causes the creation of a DisplayClass aka. variable capture - for (var i = 0; i < _singularImages.Length; i++) - { - var type = _singularImages[i]; - if (images.Contains(type) && !HasImage(item, type) && savedOptions.GetLimit(type) > 0) - { - return false; - } - } - - if (images.Contains(ImageType.Backdrop) && item.GetImages(ImageType.Backdrop).Count() < backdropLimit) - { - return false; - } - - return true; - } - /// /// Refreshes from a remote provider. /// @@ -288,7 +255,7 @@ namespace MediaBrowser.Providers.Manager return; } - _logger.LogDebug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name); + _logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, item.Path ?? item.Name); var images = await _providerManager.GetAvailableRemoteImages( item, @@ -304,12 +271,12 @@ namespace MediaBrowser.Providers.Manager foreach (var imageType in _singularImages) { - if (!IsEnabled(savedOptions, imageType)) + if (!savedOptions.IsEnabled(imageType)) { continue; } - if (!HasImage(item, imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType))) + if (!item.HasImage(imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType))) { minWidth = savedOptions.GetMinWidth(imageType); var downloaded = await DownloadImage(item, provider, result, list, minWidth, imageType, cancellationToken).ConfigureAwait(false); @@ -335,9 +302,32 @@ namespace MediaBrowser.Providers.Manager } } - private bool IsEnabled(TypeOptions options, ImageType type) + /// + /// Determines if an item already contains the given images. + /// + /// The item. + /// The images. + /// The saved options. + /// The backdrop limit. + /// true if the specified item contains images; otherwise, false. + private bool ContainsImages(BaseItem item, List images, TypeOptions savedOptions, int backdropLimit) { - return options.IsEnabled(type); + // Using .Any causes the creation of a DisplayClass aka. variable capture + for (var i = 0; i < _singularImages.Length; i++) + { + var type = _singularImages[i]; + if (images.Contains(type) && !item.HasImage(type) && savedOptions.GetLimit(type) > 0) + { + return false; + } + } + + if (images.Contains(ImageType.Backdrop) && item.GetImages(ImageType.Backdrop).Count() < backdropLimit) + { + return false; + } + + return true; } private void PruneImages(BaseItem item, ItemImageInfo[] images) @@ -354,6 +344,11 @@ namespace MediaBrowser.Providers.Manager } catch (FileNotFoundException) { + // nothing to do, already gone + } + catch (UnauthorizedAccessException ex) + { + _logger.LogWarning(ex, "Unable to delete {Image}", image.Path); } } } @@ -380,12 +375,7 @@ namespace MediaBrowser.Providers.Manager { var currentImage = item.GetImageInfo(type, 0); - if (currentImage == null) - { - item.SetImagePath(type, image.FileInfo); - changed = true; - } - else if (!string.Equals(currentImage.Path, image.FileInfo.FullName, StringComparison.OrdinalIgnoreCase)) + if (currentImage == null || !string.Equals(currentImage.Path, image.FileInfo.FullName, StringComparison.OrdinalIgnoreCase)) { item.SetImagePath(type, image.FileInfo); changed = true; @@ -493,7 +483,7 @@ namespace MediaBrowser.Providers.Manager await _providerManager.SaveImage( item, stream, - response.Content.Headers.ContentType.MediaType, + response.Content.Headers.ContentType?.MediaType, type, null, cancellationToken).ConfigureAwait(false); @@ -616,11 +606,11 @@ namespace MediaBrowser.Providers.Manager await _providerManager.SaveImage( item, stream, - response.Content.Headers.ContentType.MediaType, + response.Content.Headers.ContentType?.MediaType, imageType, null, cancellationToken).ConfigureAwait(false); - result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate; + result.UpdateType |= ItemUpdateType.ImageUpdate; } catch (HttpRequestException) {