< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.ScheduledTasks.Tasks.CleanupCollectionAndPlaylistPathsTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionAndPlaylistPathsTask.cs
Line coverage
80%
Covered lines: 8
Uncovered lines: 2
Coverable lines: 10
Total lines: 139
Line coverage: 80%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%11100%
get_Key()100%11100%
get_Description()100%210%
get_Category()100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/CleanupCollectionAndPlaylistPathsTask.cs

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