aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Pierre Bachmann <info@jean-pierre-bachmann.dev>2023-02-01 19:34:58 +0100
committerJean-Pierre Bachmann <info@jean-pierre-bachmann.dev>2023-02-01 19:34:58 +0100
commit6b8d1695297c28098924c5da18da133083ca9c92 (patch)
tree25e31887dacebb5cc23c7f3a9a5e25781e48495a
parent992b460912f3100f126bc4db0fe042fd4aefee7c (diff)
Added CleanupCollection task
-rw-r--r--Emby.Server.Implementations/Collections/CollectionManager.cs3
-rw-r--r--Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionPathsTask.cs118
-rw-r--r--MediaBrowser.Controller/Collections/ICollectionManager.cs7
3 files changed, 127 insertions, 1 deletions
diff --git a/Emby.Server.Implementations/Collections/CollectionManager.cs b/Emby.Server.Implementations/Collections/CollectionManager.cs
index b53c8ca51..20370fff5 100644
--- a/Emby.Server.Implementations/Collections/CollectionManager.cs
+++ b/Emby.Server.Implementations/Collections/CollectionManager.cs
@@ -112,7 +112,8 @@ namespace Emby.Server.Implementations.Collections
return Path.Combine(_appPaths.DataPath, "collections");
}
- private Task<Folder?> GetCollectionsFolder(bool createIfNeeded)
+ /// <inheritdoc />
+ public Task<Folder?> GetCollectionsFolder(bool createIfNeeded)
{
return EnsureLibraryFolder(GetCollectionsFolderPath(), createIfNeeded);
}
diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionPathsTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionPathsTask.cs
new file mode 100644
index 000000000..8e9270a12
--- /dev/null
+++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionPathsTask.cs
@@ -0,0 +1,118 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using Emby.Server.Implementations.Collections;
+using MediaBrowser.Controller.Collections;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Globalization;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Model.Tasks;
+using Microsoft.Extensions.Logging;
+
+namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
+
+/// <summary>
+/// Deletes Path references from collections that no longer exists.
+/// </summary>
+public class CleanupCollectionPathsTask : IScheduledTask
+{
+ private readonly ILocalizationManager _localization;
+ private readonly ICollectionManager _collectionManager;
+ private readonly ILogger<CleanupCollectionPathsTask> _logger;
+ private readonly IProviderManager _providerManager;
+ private readonly IFileSystem _fileSystem;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CleanupCollectionPathsTask"/> class.
+ /// </summary>
+ /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
+ /// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param>
+ /// <param name="logger">The logger.</param>
+ /// <param name="providerManager">The provider manager.</param>
+ /// <param name="fileSystem">The filesystem.</param>
+ public CleanupCollectionPathsTask(
+ ILocalizationManager localization,
+ ICollectionManager collectionManager,
+ ILogger<CleanupCollectionPathsTask> logger,
+ IProviderManager providerManager,
+ IFileSystem fileSystem)
+ {
+ _localization = localization;
+ _collectionManager = collectionManager;
+ _logger = logger;
+ _providerManager = providerManager;
+ _fileSystem = fileSystem;
+ }
+
+ /// <inheritdoc />
+ public string Name => _localization.GetLocalizedString("TaskCleanCollections");
+
+ /// <inheritdoc />
+ public string Key => "CleanCollections";
+
+ /// <inheritdoc />
+ public string Description => _localization.GetLocalizedString("TaskCleanCollectionsDescription");
+
+ /// <inheritdoc />
+ public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
+
+ /// <inheritdoc />
+ public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ var collectionsFolder = await _collectionManager.GetCollectionsFolder(true).ConfigureAwait(false);
+ if (collectionsFolder is null)
+ {
+ _logger.LogInformation("There is no collection folder to be found.");
+ return;
+ }
+
+ var collections = collectionsFolder.Children.OfType<BoxSet>()
+ .ToArray();
+ _logger.LogTrace("Found {CollectionLength} Boxsets.", collections.Length);
+ for (var index = 0; index < collections.Length; index++)
+ {
+ var collection = collections[index];
+ _logger.LogTrace("Check Boxset {CollectionName}.", collection.Name);
+ var itemsToRemove = new List<LinkedChild>();
+ foreach (var collectionLinkedChild in collection.LinkedChildren.ToArray())
+ {
+ if (!File.Exists(collectionLinkedChild.Path))
+ {
+ _logger.LogInformation("Item in boxset {0} cannot be found at {1}.", collection.Name, collectionLinkedChild.Path);
+ itemsToRemove.Add(collectionLinkedChild);
+ }
+ }
+
+ if (itemsToRemove.Any())
+ {
+ _logger.LogTrace("Update Boxset {CollectionName}.", collection.Name);
+ collection.LinkedChildren = collection.LinkedChildren.Except(itemsToRemove).ToArray();
+ await collection.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken)
+ .ConfigureAwait(false);
+
+ _providerManager.QueueRefresh(
+ collection.Id,
+ new MetadataRefreshOptions(new DirectoryService(_fileSystem))
+ {
+ ForceSave = true
+ },
+ RefreshPriority.High);
+ }
+
+ progress.Report(100D / collections.Length * (index + 1));
+ }
+ }
+
+ /// <inheritdoc />
+ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
+ {
+ return new[] { new TaskTriggerInfo() { Type = TaskTriggerInfo.TriggerStartup } };
+ // return Enumerable.Empty<TaskTriggerInfo>();
+ }
+}
diff --git a/MediaBrowser.Controller/Collections/ICollectionManager.cs b/MediaBrowser.Controller/Collections/ICollectionManager.cs
index b8c33ee5a..38a78a67b 100644
--- a/MediaBrowser.Controller/Collections/ICollectionManager.cs
+++ b/MediaBrowser.Controller/Collections/ICollectionManager.cs
@@ -56,5 +56,12 @@ namespace MediaBrowser.Controller.Collections
/// <param name="user">The user.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
IEnumerable<BaseItem> CollapseItemsWithinBoxSets(IEnumerable<BaseItem> items, User user);
+
+ /// <summary>
+ /// Gets the folder where collections are stored.
+ /// </summary>
+ /// <param name="createIfNeeded">Will create the collection folder on the storage if set to true.</param>
+ /// <returns>The folder instance referencing the collection storage.</returns>
+ Task<Folder?> GetCollectionsFolder(bool createIfNeeded);
}
}