From d1a4cacb8b41d38304f8ed02626e1634ef487181 Mon Sep 17 00:00:00 2001 From: rlauuzo <46294892+rlauuzo@users.noreply.github.com> Date: Wed, 1 May 2024 13:45:57 +0200 Subject: [PATCH] Change GetItem to return null to handle nullable LibraryManager (#137) --- .../Controllers/SkipIntroController.cs | 4 ++-- .../Controllers/VisualizationController.cs | 1 + .../Plugin.cs | 22 ++++++++++++++++--- .../QueueManager.cs | 10 ++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs index 2c261d0..4b59efa 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs @@ -153,8 +153,8 @@ public class SkipIntroController : ControllerBase foreach (var intro in timestamps) { // Get the details of the item from Jellyfin - var rawItem = Plugin.Instance!.GetItem(intro.Key); - if (rawItem is not Episode episode) + var rawItem = Plugin.Instance.GetItem(intro.Key); + if (rawItem == null || rawItem is not Episode episode) { throw new InvalidCastException("Unable to cast item id " + intro.Key + " to an Episode"); } diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs index daef672..959add8 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs @@ -140,6 +140,7 @@ public class VisualizationController : ControllerBase foreach (var e in episodes) { Plugin.Instance!.Intros.Remove(e.EpisodeId); + Plugin.Instance!.Credits.Remove(e.EpisodeId); } Plugin.Instance!.SaveTimestamps(); diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs index fcdd4aa..f39b1c9 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs @@ -306,7 +306,7 @@ public class Plugin : BasePlugin, IHasWebPages return commit; } - internal BaseItem GetItem(Guid id) + internal BaseItem? GetItem(Guid id) { return _libraryManager.GetItemById(id); } @@ -318,7 +318,15 @@ public class Plugin : BasePlugin, IHasWebPages /// Full path to item. internal string GetItemPath(Guid id) { - return GetItem(id).Path; + var item = GetItem(id); + if (item == null) + { + // Handle the case where the item is not found + _logger.LogWarning("Item with ID {Id} not found.", id); + return string.Empty; + } + + return item.Path; } /// @@ -328,7 +336,15 @@ public class Plugin : BasePlugin, IHasWebPages /// List of chapters. internal List GetChapters(Guid id) { - return _itemRepository.GetChapters(GetItem(id)); + var item = GetItem(id); + if (item == null) + { + // Handle the case where the item is not found + _logger.LogWarning("Item with ID {Id} not found.", id); + return new List(); + } + + return _itemRepository.GetChapters(item); } internal void UpdateTimestamps(Dictionary newTimestamps, AnalysisMode mode) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs b/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs index 4c123df..a462fd7 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs @@ -63,7 +63,15 @@ public class QueueManager { foreach (var location in folder.Locations) { - QueueLibraryContents(_libraryManager.FindByPath(location, true).Id); + var item = _libraryManager.FindByPath(location, true); + + if (item is null) + { + _logger.LogWarning("Unable to find linked item at path {0}", location); + continue; + } + + QueueLibraryContents(item.Id); } } catch (Exception ex)