CleanupCollectionAndPlaylistPathsTask.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Collections;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Entities.Movies;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Playlists;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Globalization;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Tasks;
  16. using Microsoft.Extensions.Logging;
  17. namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
  18. /// <summary>
  19. /// Deletes path references from collections and playlists that no longer exists.
  20. /// </summary>
  21. public class CleanupCollectionAndPlaylistPathsTask : IScheduledTask
  22. {
  23. private readonly ILocalizationManager _localization;
  24. private readonly ICollectionManager _collectionManager;
  25. private readonly IPlaylistManager _playlistManager;
  26. private readonly ILogger<CleanupCollectionAndPlaylistPathsTask> _logger;
  27. private readonly IProviderManager _providerManager;
  28. private readonly IFileSystem _fileSystem;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="CleanupCollectionAndPlaylistPathsTask"/> class.
  31. /// </summary>
  32. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  33. /// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param>
  34. /// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
  35. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  36. /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
  37. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  38. public CleanupCollectionAndPlaylistPathsTask(
  39. ILocalizationManager localization,
  40. ICollectionManager collectionManager,
  41. IPlaylistManager playlistManager,
  42. ILogger<CleanupCollectionAndPlaylistPathsTask> logger,
  43. IProviderManager providerManager,
  44. IFileSystem fileSystem)
  45. {
  46. _localization = localization;
  47. _collectionManager = collectionManager;
  48. _playlistManager = playlistManager;
  49. _logger = logger;
  50. _providerManager = providerManager;
  51. _fileSystem = fileSystem;
  52. }
  53. /// <inheritdoc />
  54. public string Name => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylists");
  55. /// <inheritdoc />
  56. public string Key => "CleanCollectionsAndPlaylists";
  57. /// <inheritdoc />
  58. public string Description => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylistsDescription");
  59. /// <inheritdoc />
  60. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  61. /// <inheritdoc />
  62. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  63. {
  64. var collectionsFolder = await _collectionManager.GetCollectionsFolder(false).ConfigureAwait(false);
  65. if (collectionsFolder is null)
  66. {
  67. _logger.LogDebug("There is no collections folder to be found");
  68. }
  69. else
  70. {
  71. var collections = collectionsFolder.Children.OfType<BoxSet>().ToArray();
  72. _logger.LogDebug("Found {CollectionLength} boxsets", collections.Length);
  73. for (var index = 0; index < collections.Length; index++)
  74. {
  75. var collection = collections[index];
  76. _logger.LogDebug("Checking boxset {CollectionName}", collection.Name);
  77. CleanupLinkedChildren(collection, cancellationToken);
  78. progress.Report(50D / collections.Length * (index + 1));
  79. }
  80. }
  81. var playlistsFolder = _playlistManager.GetPlaylistsFolder();
  82. if (playlistsFolder is null)
  83. {
  84. _logger.LogDebug("There is no playlists folder to be found");
  85. return;
  86. }
  87. var playlists = playlistsFolder.Children.OfType<Playlist>().ToArray();
  88. _logger.LogDebug("Found {PlaylistLength} playlists", playlists.Length);
  89. for (var index = 0; index < playlists.Length; index++)
  90. {
  91. var playlist = playlists[index];
  92. _logger.LogDebug("Checking playlist {PlaylistName}", playlist.Name);
  93. CleanupLinkedChildren(playlist, cancellationToken);
  94. progress.Report(50D / playlists.Length * (index + 1));
  95. }
  96. }
  97. private void CleanupLinkedChildren<T>(T folder, CancellationToken cancellationToken)
  98. where T : Folder
  99. {
  100. List<LinkedChild>? itemsToRemove = null;
  101. foreach (var linkedChild in folder.LinkedChildren)
  102. {
  103. var path = linkedChild.Path;
  104. if (!File.Exists(path) && !Directory.Exists(path))
  105. {
  106. _logger.LogInformation("Item in {FolderName} cannot be found at {ItemPath}", folder.Name, path);
  107. (itemsToRemove ??= new List<LinkedChild>()).Add(linkedChild);
  108. }
  109. }
  110. if (itemsToRemove is not null)
  111. {
  112. _logger.LogDebug("Updating {FolderName}", folder.Name);
  113. folder.LinkedChildren = folder.LinkedChildren.Except(itemsToRemove).ToArray();
  114. _providerManager.SaveMetadataAsync(folder, ItemUpdateType.MetadataEdit);
  115. folder.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken);
  116. }
  117. }
  118. /// <inheritdoc />
  119. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  120. {
  121. return [new TaskTriggerInfo() { Type = TaskTriggerInfoType.StartupTrigger }];
  122. }
  123. }