diff --git a/Shokofin/Configuration/MediaFolderConfigurationService.cs b/Shokofin/Configuration/MediaFolderConfigurationService.cs index bf6a7d46..7994a5d2 100644 --- a/Shokofin/Configuration/MediaFolderConfigurationService.cs +++ b/Shokofin/Configuration/MediaFolderConfigurationService.cs @@ -209,11 +209,17 @@ private async void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventAr #region Media Folder Mapping - public IReadOnlyList<(string vfsPath, string mainMediaFolderPath, CollectionType? collectionType, IReadOnlyList mediaList)> GetAvailableMediaFoldersForLibrariesForEvents(Func? filter = null) + public async Task mediaList)>> GetAvailableMediaFoldersForLibraries(Func? filter = null) { - LockObj.Wait(); + await LockObj.WaitAsync(); try { var virtualFolders = LibraryManager.GetVirtualFolders(); + if (ShouldGenerateAllConfigurations) + { + ShouldGenerateAllConfigurations = false; + await GenerateAllConfigurations(virtualFolders).ConfigureAwait(false); + } + var attachRoot = Plugin.Instance.Configuration.VFS_AttachRoot; return Plugin.Instance.Configuration.MediaFolders .Where(config => config.IsMapped && !config.IsVirtualRoot && (filter is null || filter(config)) && LibraryManager.GetItemById(config.MediaFolderId) is Folder) diff --git a/Shokofin/Events/EventDispatchService.cs b/Shokofin/Events/EventDispatchService.cs index d9199f60..e8c0cfe4 100644 --- a/Shokofin/Events/EventDispatchService.cs +++ b/Shokofin/Events/EventDispatchService.cs @@ -211,7 +211,7 @@ private async Task ProcessFileEvents(int fileId, List<(UpdateReason Reason, int var locationsToNotify = new List(); var mediaFoldersToNotify = new Dictionary(); var seriesIds = await GetSeriesIdsForFile(fileId, changes.Select(t => t.Event).LastOrDefault(e => e.HasCrossReferences)); - var libraries = ConfigurationService.GetAvailableMediaFoldersForLibrariesForEvents(c => c.IsFileEventsEnabled); + var libraries = await ConfigurationService.GetAvailableMediaFoldersForLibraries(c => c.IsFileEventsEnabled).ConfigureAwait(false); var (reason, importFolderId, relativePath, lastEvent) = changes.Last(); if (reason is not UpdateReason.Removed) { Logger.LogTrace("Processing file changed. (File={FileId})", fileId); diff --git a/Shokofin/Tasks/CleanupVirtualRootTask.cs b/Shokofin/Tasks/CleanupVirtualRootTask.cs index d3bbf4e9..0f083837 100644 --- a/Shokofin/Tasks/CleanupVirtualRootTask.cs +++ b/Shokofin/Tasks/CleanupVirtualRootTask.cs @@ -9,6 +9,7 @@ using MediaBrowser.Model.IO; using MediaBrowser.Model.Tasks; using Microsoft.Extensions.Logging; +using Shokofin.Configuration; using Shokofin.Utils; namespace Shokofin.Tasks; @@ -16,7 +17,7 @@ namespace Shokofin.Tasks; /// /// Clean-up any old VFS roots leftover from an outdated install or failed removal of the roots. /// -public class CleanupVirtualRootTask(ILogger logger, ILibraryManager libraryManager, IFileSystem fileSystem, LibraryScanWatcher scanWatcher) : IScheduledTask, IConfigurableScheduledTask +public class CleanupVirtualRootTask(ILogger logger, ILibraryManager libraryManager, IFileSystem fileSystem, MediaFolderConfigurationService configurationService, LibraryScanWatcher scanWatcher) : IScheduledTask, IConfigurableScheduledTask { /// public string Name => "Clean-up Virtual File System Roots"; @@ -45,6 +46,8 @@ public class CleanupVirtualRootTask(ILogger logger, ILib private readonly IFileSystem FileSystem = fileSystem; + private readonly MediaFolderConfigurationService ConfigurationService = configurationService; + private readonly LibraryScanWatcher ScanWatcher = scanWatcher; public IEnumerable GetDefaultTriggers() @@ -54,11 +57,14 @@ public IEnumerable GetDefaultTriggers() }, ]; - public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) + public async Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) { if (ScanWatcher.IsScanRunning) - return Task.CompletedTask; + return; + var mediaFolders = (await ConfigurationService.GetAvailableMediaFoldersForLibraries().ConfigureAwait(false)) + .SelectMany(x => x.mediaList) + .ToList(); var start = DateTime.Now; var virtualRoots = Plugin.Instance.AllVirtualRoots .Except([Plugin.Instance.VirtualRoot]) @@ -73,7 +79,7 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat Logger.LogTrace("Removed VFS root {Path} in {TimeSpan}.", virtualRoot, perFolderDeltaTime); } - var libraryIds = Plugin.Instance.Configuration.MediaFolders.ToList() + var libraryIds = mediaFolders.ToList() .Select(config => config.LibraryId.ToString()) .Distinct() .ToList(); @@ -96,7 +102,7 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat start = DateTime.Now; var addedCount = 0; var fixedCount = 0; - var vfsPaths = Plugin.Instance.Configuration.MediaFolders + var vfsPaths = mediaFolders .DistinctBy(config => config.LibraryId) .Select(config => LibraryManager.GetItemById(config.LibraryId) as Folder) .Where(folder => folder is not null) @@ -123,7 +129,5 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat deltaTime = DateTime.Now - start; Logger.LogDebug("Added {AddedCount} missing and fixed {FixedCount} broken VFS roots in {TimeSpan}.", addedCount, fixedCount, deltaTime); } - - return Task.CompletedTask; } }