diff --git a/CinemaJellyfin/CinemaFilterFolder.cs b/CinemaJellyfin/CinemaFilterFolder.cs index 5992a28..b0d35d5 100644 --- a/CinemaJellyfin/CinemaFilterFolder.cs +++ b/CinemaJellyfin/CinemaFilterFolder.cs @@ -7,6 +7,7 @@ using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; using CinemaLib.API; +using CinemaJellyfin; namespace Jellyfin.Plugin.Cinema; @@ -165,11 +166,11 @@ public abstract class CinemaFilterFolder : Folder private static T CreateFilterFolderInternal(CinemaFilterFolder? parent, string localizedName) where T : CinemaFilterFolder, new() { - Guid folderId = CinemaHost.LibraryManager.GetNewItemId("folder", typeof(T)); - string folderPath = GetInternalMetadataPath(CinemaHost.InternalMetadataPath!, folderId); + Guid folderId = CinemaGlobals.LibraryManager.GetNewItemId("folder", typeof(T)); + string folderPath = GetInternalMetadataPath(CinemaGlobals.InternalMetadataPath!, folderId); Directory.CreateDirectory(folderPath); - T? folder = CinemaHost.LibraryManager.GetItemById(folderId) as T; + T? folder = CinemaGlobals.LibraryManager.GetItemById(folderId) as T; bool isNew; bool forceUpdate = false; if (isNew = folder == null) @@ -177,8 +178,8 @@ public abstract class CinemaFilterFolder : Folder folder = new T { Id = folderId, - DateCreated = CinemaHost.FileSystem.GetCreationTimeUtc(folderPath), - DateModified = CinemaHost.FileSystem.GetLastWriteTimeUtc(folderPath) + DateCreated = CinemaGlobals.FileSystem.GetCreationTimeUtc(folderPath), + DateModified = CinemaGlobals.FileSystem.GetLastWriteTimeUtc(folderPath) }; } @@ -200,11 +201,11 @@ public abstract class CinemaFilterFolder : Folder if (isNew) { folder.OnMetadataChanged(); - CinemaHost.LibraryManager.CreateItem(folder, parent); + CinemaGlobals.LibraryManager.CreateItem(folder, parent); } folder.RefreshMetadata( - new MetadataRefreshOptions(new DirectoryService(CinemaHost.FileSystem)) { ForceSave = !isNew && forceUpdate }, + new MetadataRefreshOptions(new DirectoryService(CinemaGlobals.FileSystem)) { ForceSave = !isNew && forceUpdate }, default ).GetAwaiter().GetResult(); diff --git a/CinemaJellyfin/CinemaGlobals.cs b/CinemaJellyfin/CinemaGlobals.cs new file mode 100644 index 0000000..b60a12d --- /dev/null +++ b/CinemaJellyfin/CinemaGlobals.cs @@ -0,0 +1,36 @@ +using System; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.IO; + +namespace CinemaJellyfin; + +/// +/// Global services that are resolved at startup and are +/// otherwise inaccessible to some classes (mostly folders). +/// +static class CinemaGlobals +{ +#pragma warning disable CS8618 + private static ILibraryManager _libraryManager; + private static IServerConfigurationManager _config; + private static IFileSystem _fileSystem; +#pragma warning restore CS8618 + + internal static void Initialize(ILibraryManager libraryManager, IServerConfigurationManager config, IFileSystem fileSystem) + { + _libraryManager = libraryManager; + _config = config; + _fileSystem = fileSystem; + } + + public static ILibraryManager LibraryManager => _libraryManager; + + public static IFileSystem FileSystem => _fileSystem; + + public static string InternalMetadataPath => _config.ApplicationPaths.InternalMetadataPath; + + public static string PreferredCulture => _config.Configuration.PreferredMetadataLanguage; + + public static string FallbackCulture => "en"; +} diff --git a/CinemaJellyfin/CinemaHost.cs b/CinemaJellyfin/CinemaHost.cs index 8e974e0..cd4ffb5 100644 --- a/CinemaJellyfin/CinemaHost.cs +++ b/CinemaJellyfin/CinemaHost.cs @@ -1,4 +1,5 @@ using System.Linq.Expressions; +using CinemaJellyfin; using CinemaLib.Webshare; using Jellyfin.Plugin.Cinema.Configuration; using MediaBrowser.Controller.Configuration; @@ -19,9 +20,6 @@ sealed class CinemaHost : IHostedService #pragma warning disable CS8618 // This instance is specially registered and gets created before all classes // except CinemaServiceRegistrator and CinemaPlugin. - private static ILibraryManager _libraryManager; - private static IServerConfigurationManager _config; - private static IFileSystem _fileSystem; private static Session? _webshare; #pragma warning restore CS8618 private readonly ILogger _logger; @@ -33,22 +31,10 @@ sealed class CinemaHost : IHostedService /// public CinemaHost(ILibraryManager libraryManager, IServerConfigurationManager config, IFileSystem fileSystem, ILogger logger) { - _libraryManager = libraryManager; - _config = config; - _fileSystem = fileSystem; + CinemaGlobals.Initialize(libraryManager, config, fileSystem); this._logger = logger; } - public static ILibraryManager LibraryManager => _libraryManager; - - public static string InternalMetadataPath => _config.ApplicationPaths.InternalMetadataPath; - - public static IFileSystem FileSystem => _fileSystem; - - public static string PreferredCulture => _config.Configuration.PreferredMetadataLanguage; - - public static string FallbackCulture => "en"; - public static Session? Webshare => _webshare; public static async Task IsWebshareFreeAccount(CancellationToken cancel) diff --git a/CinemaJellyfin/CinemaLibraryManager.cs b/CinemaJellyfin/CinemaLibraryManager.cs index f5fecd4..ad80267 100644 --- a/CinemaJellyfin/CinemaLibraryManager.cs +++ b/CinemaJellyfin/CinemaLibraryManager.cs @@ -15,6 +15,9 @@ using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; using MediaBrowser.Model.Querying; +using CinemaJellyfin; +using Microsoft.Extensions.DependencyInjection; +using MediaBrowser.Controller.Configuration; namespace Jellyfin.Plugin.Cinema; @@ -33,6 +36,9 @@ sealed class CinemaLibraryManager : ILibraryManager throw new InvalidOperationException("Original LibraryManager service not found."); this._inner = inner; this._userData = userData; + + // We may run before CinemaHost if migrations are executed + CinemaGlobals.Initialize(this, svc.GetRequiredService(), svc.GetRequiredService()); } #region ILibraryManager Members diff --git a/CinemaJellyfin/CinemaQueryExtensions.cs b/CinemaJellyfin/CinemaQueryExtensions.cs index e76b5f0..3da68e8 100644 --- a/CinemaJellyfin/CinemaQueryExtensions.cs +++ b/CinemaJellyfin/CinemaQueryExtensions.cs @@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; +using CinemaJellyfin; using CinemaLib.API; using Jellyfin.Data.Enums; using MediaBrowser.Controller.Entities; @@ -206,7 +207,7 @@ static class CinemaQueryExtensions { // HACK: We use RegisterItem that is volatile intead of CreateItem //_libraryManager.CreateItem(item, parentFolder); - CinemaHost.LibraryManager.RegisterItem(item); + CinemaGlobals.LibraryManager.RegisterItem(item); if (media.cast != null && media.cast.Count > 0) { @@ -264,9 +265,9 @@ static class CinemaQueryExtensions InfoLabelI18n? preferred = null; InfoLabelI18n? fallback = null; foreach (InfoLabelI18n i in media.i18n_info_labels) - if (i.lang == CinemaHost.PreferredCulture) + if (i.lang == CinemaGlobals.PreferredCulture) preferred = i; - else if (i.lang == CinemaHost.FallbackCulture) + else if (i.lang == CinemaGlobals.FallbackCulture) fallback = i; else if (first == null) first = i; @@ -280,7 +281,7 @@ static class CinemaQueryExtensions internal static Guid GetMediaItemId(string csPrimaryId, string? csVersionId) { string idS = csVersionId == null ? csPrimaryId : (csPrimaryId + VersionSeparator + csVersionId); - return CinemaHost.LibraryManager.GetNewItemId(idS, typeof(CinemaPlugin)); + return CinemaGlobals.LibraryManager.GetNewItemId(idS, typeof(CinemaPlugin)); } /// @@ -290,7 +291,7 @@ static class CinemaQueryExtensions where T : BaseItem, new() { Guid id = GetMediaItemId(csPrimaryId, csVersionId); - T? item = CinemaHost.LibraryManager.GetItemById(id) as T; + T? item = CinemaGlobals.LibraryManager.GetItemById(id) as T; if (item == null) {