< Summary - Jellyfin

Information
Class: Jellyfin.LiveTv.Channels.ChannelPostScanTask
Assembly: Jellyfin.LiveTv
File(s): /srv/git/jellyfin/src/Jellyfin.LiveTv/Channels/ChannelPostScanTask.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 100
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%210%
Run(...)100%210%
CleanDatabase(...)0%620%
CleanChannel(...)0%620%

File(s)

/srv/git/jellyfin/src/Jellyfin.LiveTv/Channels/ChannelPostScanTask.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Jellyfin.Data.Enums;
 6using MediaBrowser.Controller.Channels;
 7using MediaBrowser.Controller.Entities;
 8using MediaBrowser.Controller.Library;
 9using Microsoft.Extensions.Logging;
 10
 11namespace Jellyfin.LiveTv.Channels
 12{
 13    /// <summary>
 14    /// A task to remove all non-installed channels from the database.
 15    /// </summary>
 16    public class ChannelPostScanTask
 17    {
 18        private readonly IChannelManager _channelManager;
 19        private readonly ILogger _logger;
 20        private readonly ILibraryManager _libraryManager;
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="ChannelPostScanTask"/> class.
 24        /// </summary>
 25        /// <param name="channelManager">The channel manager.</param>
 26        /// <param name="logger">The logger.</param>
 27        /// <param name="libraryManager">The library manager.</param>
 28        public ChannelPostScanTask(IChannelManager channelManager, ILogger logger, ILibraryManager libraryManager)
 29        {
 030            _channelManager = channelManager;
 031            _logger = logger;
 032            _libraryManager = libraryManager;
 033        }
 34
 35        /// <summary>
 36        /// Runs this task.
 37        /// </summary>
 38        /// <param name="progress">The progress.</param>
 39        /// <param name="cancellationToken">The cancellation token.</param>
 40        /// <returns>The completed task.</returns>
 41        public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
 42        {
 043            CleanDatabase(cancellationToken);
 44
 045            progress.Report(100);
 046            return Task.CompletedTask;
 47        }
 48
 49        private void CleanDatabase(CancellationToken cancellationToken)
 50        {
 051            var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
 52
 053            var uninstalledChannels = _libraryManager.GetItemList(new InternalItemsQuery
 054            {
 055                IncludeItemTypes = new[] { BaseItemKind.Channel },
 056                ExcludeItemIds = installedChannelIds.ToArray()
 057            });
 58
 059            foreach (var channel in uninstalledChannels)
 60            {
 061                cancellationToken.ThrowIfCancellationRequested();
 62
 063                CleanChannel((Channel)channel, cancellationToken);
 64            }
 065        }
 66
 67        private void CleanChannel(Channel channel, CancellationToken cancellationToken)
 68        {
 069            _logger.LogInformation("Cleaning channel {0} from database", channel.Id);
 70
 71            // Delete all channel items
 072            var items = _libraryManager.GetItemList(new InternalItemsQuery
 073            {
 074                ChannelIds = new[] { channel.Id }
 075            });
 76
 077            foreach (var item in items)
 78            {
 079                cancellationToken.ThrowIfCancellationRequested();
 80
 081                _libraryManager.DeleteItem(
 082                    item,
 083                    new DeleteOptions
 084                    {
 085                        DeleteFileLocation = false
 086                    },
 087                    false);
 88            }
 89
 90            // Finally, delete the channel itself
 091            _libraryManager.DeleteItem(
 092                channel,
 093                new DeleteOptions
 094                {
 095                    DeleteFileLocation = false
 096                },
 097                false);
 098        }
 99    }
 100}