Change GetItem to return null to handle nullable LibraryManager (#137)

This commit is contained in:
rlauuzo 2024-05-01 13:45:57 +02:00 committed by GitHub
parent c4fbfd43ab
commit 920fa34a43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 5 deletions

View File

@ -154,7 +154,7 @@ public class SkipIntroController : ControllerBase
{
// Get the details of the item from Jellyfin
var rawItem = Plugin.Instance.GetItem(intro.Key);
if (rawItem is not Episode episode)
if (rawItem == null || rawItem is not Episode episode)
{
throw new InvalidCastException("Unable to cast item id " + intro.Key + " to an Episode");
}

View File

@ -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();

View File

@ -351,7 +351,7 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
return commit;
}
internal BaseItem GetItem(Guid id)
internal BaseItem? GetItem(Guid id)
{
return _libraryManager.GetItemById(id);
}
@ -363,7 +363,15 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
/// <returns>Full path to item.</returns>
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;
}
/// <summary>
@ -373,7 +381,15 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
/// <returns>List of chapters.</returns>
internal List<ChapterInfo> 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<ChapterInfo>();
}
return _itemRepository.GetChapters(item);
}
internal void UpdateTimestamps(Dictionary<Guid, Intro> newTimestamps, AnalysisMode mode)

View File

@ -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)