Skip to content

Commit

Permalink
fix: ensure all media folder configurations are available
Browse files Browse the repository at this point in the history
…before cleaning up the VFS roots, since we rely on the metadata being available to fix them properly.
  • Loading branch information
revam committed Oct 15, 2024
1 parent 83c4745 commit be15776
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
10 changes: 8 additions & 2 deletions Shokofin/Configuration/MediaFolderConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,17 @@ private async void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventAr

#region Media Folder Mapping

public IReadOnlyList<(string vfsPath, string mainMediaFolderPath, CollectionType? collectionType, IReadOnlyList<MediaFolderConfiguration> mediaList)> GetAvailableMediaFoldersForLibrariesForEvents(Func<MediaFolderConfiguration, bool>? filter = null)
public async Task<IReadOnlyList<(string vfsPath, string mainMediaFolderPath, CollectionType? collectionType, IReadOnlyList<MediaFolderConfiguration> mediaList)>> GetAvailableMediaFoldersForLibraries(Func<MediaFolderConfiguration, bool>? 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)
Expand Down
2 changes: 1 addition & 1 deletion Shokofin/Events/EventDispatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private async Task ProcessFileEvents(int fileId, List<(UpdateReason Reason, int
var locationsToNotify = new List<string>();
var mediaFoldersToNotify = new Dictionary<string, (string pathToReport, Folder mediaFolder)>();
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);
Expand Down
18 changes: 11 additions & 7 deletions Shokofin/Tasks/CleanupVirtualRootTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
using Shokofin.Configuration;
using Shokofin.Utils;

namespace Shokofin.Tasks;

/// <summary>
/// Clean-up any old VFS roots leftover from an outdated install or failed removal of the roots.
/// </summary>
public class CleanupVirtualRootTask(ILogger<CleanupVirtualRootTask> logger, ILibraryManager libraryManager, IFileSystem fileSystem, LibraryScanWatcher scanWatcher) : IScheduledTask, IConfigurableScheduledTask
public class CleanupVirtualRootTask(ILogger<CleanupVirtualRootTask> logger, ILibraryManager libraryManager, IFileSystem fileSystem, MediaFolderConfigurationService configurationService, LibraryScanWatcher scanWatcher) : IScheduledTask, IConfigurableScheduledTask
{
/// <inheritdoc />
public string Name => "Clean-up Virtual File System Roots";
Expand Down Expand Up @@ -45,6 +46,8 @@ public class CleanupVirtualRootTask(ILogger<CleanupVirtualRootTask> logger, ILib

private readonly IFileSystem FileSystem = fileSystem;

private readonly MediaFolderConfigurationService ConfigurationService = configurationService;

private readonly LibraryScanWatcher ScanWatcher = scanWatcher;

public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
Expand All @@ -54,11 +57,14 @@ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
},
];

public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
public async Task ExecuteAsync(IProgress<double> 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])
Expand All @@ -73,7 +79,7 @@ public Task ExecuteAsync(IProgress<double> 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();
Expand All @@ -96,7 +102,7 @@ public Task ExecuteAsync(IProgress<double> 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)
Expand All @@ -123,7 +129,5 @@ public Task ExecuteAsync(IProgress<double> 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;
}
}

0 comments on commit be15776

Please sign in to comment.