| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Jellyfin.Data.Enums; |
| | 6 | | using MediaBrowser.Controller.Channels; |
| | 7 | | using MediaBrowser.Controller.Entities; |
| | 8 | | using MediaBrowser.Controller.Library; |
| | 9 | | using Microsoft.Extensions.Logging; |
| | 10 | |
|
| | 11 | | namespace 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 | | { |
| 0 | 30 | | _channelManager = channelManager; |
| 0 | 31 | | _logger = logger; |
| 0 | 32 | | _libraryManager = libraryManager; |
| 0 | 33 | | } |
| | 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 | | { |
| 0 | 43 | | CleanDatabase(cancellationToken); |
| | 44 | |
|
| 0 | 45 | | progress.Report(100); |
| 0 | 46 | | return Task.CompletedTask; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | private void CleanDatabase(CancellationToken cancellationToken) |
| | 50 | | { |
| 0 | 51 | | var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds(); |
| | 52 | |
|
| 0 | 53 | | var uninstalledChannels = _libraryManager.GetItemList(new InternalItemsQuery |
| 0 | 54 | | { |
| 0 | 55 | | IncludeItemTypes = new[] { BaseItemKind.Channel }, |
| 0 | 56 | | ExcludeItemIds = installedChannelIds.ToArray() |
| 0 | 57 | | }); |
| | 58 | |
|
| 0 | 59 | | foreach (var channel in uninstalledChannels) |
| | 60 | | { |
| 0 | 61 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 62 | |
|
| 0 | 63 | | CleanChannel((Channel)channel, cancellationToken); |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | private void CleanChannel(Channel channel, CancellationToken cancellationToken) |
| | 68 | | { |
| 0 | 69 | | _logger.LogInformation("Cleaning channel {0} from database", channel.Id); |
| | 70 | |
|
| | 71 | | // Delete all channel items |
| 0 | 72 | | var items = _libraryManager.GetItemList(new InternalItemsQuery |
| 0 | 73 | | { |
| 0 | 74 | | ChannelIds = new[] { channel.Id } |
| 0 | 75 | | }); |
| | 76 | |
|
| 0 | 77 | | foreach (var item in items) |
| | 78 | | { |
| 0 | 79 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 80 | |
|
| 0 | 81 | | _libraryManager.DeleteItem( |
| 0 | 82 | | item, |
| 0 | 83 | | new DeleteOptions |
| 0 | 84 | | { |
| 0 | 85 | | DeleteFileLocation = false |
| 0 | 86 | | }, |
| 0 | 87 | | false); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // Finally, delete the channel itself |
| 0 | 91 | | _libraryManager.DeleteItem( |
| 0 | 92 | | channel, |
| 0 | 93 | | new DeleteOptions |
| 0 | 94 | | { |
| 0 | 95 | | DeleteFileLocation = false |
| 0 | 96 | | }, |
| 0 | 97 | | false); |
| 0 | 98 | | } |
| | 99 | | } |
| | 100 | | } |