< 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
41%
Covered lines: 10
Uncovered lines: 14
Coverable lines: 24
Total lines: 140
Line coverage: 41.6%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
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%
CleanupLinkedChildren(...)0%110100%
GetDefaultTriggers()100%11100%

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    private readonly IFileSystem _fileSystem;
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="CleanupCollectionAndPlaylistPathsTask"/> class.
 34    /// </summary>
 35    /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 36    /// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param>
 37    /// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
 38    /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
 39    /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
 40    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 41    public CleanupCollectionAndPlaylistPathsTask(
 42        ILocalizationManager localization,
 43        ICollectionManager collectionManager,
 44        IPlaylistManager playlistManager,
 45        ILogger<CleanupCollectionAndPlaylistPathsTask> logger,
 46        IProviderManager providerManager,
 47        IFileSystem fileSystem)
 48    {
 2249        _localization = localization;
 2250        _collectionManager = collectionManager;
 2251        _playlistManager = playlistManager;
 2252        _logger = logger;
 2253        _providerManager = providerManager;
 2254        _fileSystem = fileSystem;
 2255    }
 56
 57    /// <inheritdoc />
 2758    public string Name => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylists");
 59
 60    /// <inheritdoc />
 161    public string Key => "CleanCollectionsAndPlaylists";
 62
 63    /// <inheritdoc />
 064    public string Description => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylistsDescription");
 65
 66    /// <inheritdoc />
 067    public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
 68
 69    /// <inheritdoc />
 70    public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 71    {
 72        var collectionsFolder = await _collectionManager.GetCollectionsFolder(false).ConfigureAwait(false);
 73        if (collectionsFolder is null)
 74        {
 75            _logger.LogDebug("There is no collections folder to be found");
 76        }
 77        else
 78        {
 79            var collections = collectionsFolder.Children.OfType<BoxSet>().ToArray();
 80            _logger.LogDebug("Found {CollectionLength} boxsets", collections.Length);
 81
 82            for (var index = 0; index < collections.Length; index++)
 83            {
 84                var collection = collections[index];
 85                _logger.LogDebug("Checking boxset {CollectionName}", collection.Name);
 86
 87                CleanupLinkedChildren(collection, cancellationToken);
 88                progress.Report(50D / collections.Length * (index + 1));
 89            }
 90        }
 91
 92        var playlistsFolder = _playlistManager.GetPlaylistsFolder();
 93        if (playlistsFolder is null)
 94        {
 95            _logger.LogDebug("There is no playlists folder to be found");
 96            return;
 97        }
 98
 99        var playlists = playlistsFolder.Children.OfType<Playlist>().ToArray();
 100        _logger.LogDebug("Found {PlaylistLength} playlists", playlists.Length);
 101
 102        for (var index = 0; index < playlists.Length; index++)
 103        {
 104            var playlist = playlists[index];
 105            _logger.LogDebug("Checking playlist {PlaylistName}", playlist.Name);
 106
 107            CleanupLinkedChildren(playlist, cancellationToken);
 108            progress.Report(50D / playlists.Length * (index + 1));
 109        }
 110    }
 111
 112    private void CleanupLinkedChildren<T>(T folder, CancellationToken cancellationToken)
 113        where T : Folder
 114    {
 0115        List<LinkedChild>? itemsToRemove = null;
 0116        foreach (var linkedChild in folder.LinkedChildren)
 117        {
 0118            var path = linkedChild.Path;
 0119            if (!File.Exists(path) && !Directory.Exists(path))
 120            {
 0121                _logger.LogInformation("Item in {FolderName} cannot be found at {ItemPath}", folder.Name, path);
 0122                (itemsToRemove ??= new List<LinkedChild>()).Add(linkedChild);
 123            }
 124        }
 125
 0126        if (itemsToRemove is not null)
 127        {
 0128            _logger.LogDebug("Updating {FolderName}", folder.Name);
 0129            folder.LinkedChildren = folder.LinkedChildren.Except(itemsToRemove).ToArray();
 0130            _providerManager.SaveMetadataAsync(folder, ItemUpdateType.MetadataEdit);
 0131            folder.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken);
 132        }
 0133    }
 134
 135    /// <inheritdoc />
 136    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 137    {
 22138        return [new TaskTriggerInfo() { Type = TaskTriggerInfo.TriggerStartup }];
 139    }
 140}