ChannelPostScanTask.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Controller.Channels;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using Microsoft.Extensions.Logging;
  9. namespace Emby.Server.Implementations.Channels
  10. {
  11. /// <summary>
  12. /// A task to remove all non-installed channels from the database.
  13. /// </summary>
  14. public class ChannelPostScanTask
  15. {
  16. private readonly IChannelManager _channelManager;
  17. private readonly ILogger _logger;
  18. private readonly ILibraryManager _libraryManager;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ChannelPostScanTask"/> class.
  21. /// </summary>
  22. /// <param name="channelManager">The channel manager.</param>
  23. /// <param name="logger">The logger.</param>
  24. /// <param name="libraryManager">The library manager.</param>
  25. public ChannelPostScanTask(IChannelManager channelManager, ILogger logger, ILibraryManager libraryManager)
  26. {
  27. _channelManager = channelManager;
  28. _logger = logger;
  29. _libraryManager = libraryManager;
  30. }
  31. /// <summary>
  32. /// Runs this task.
  33. /// </summary>
  34. /// <param name="progress">The progress.</param>
  35. /// <param name="cancellationToken">The cancellation token.</param>
  36. /// <returns>The completed task.</returns>
  37. public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  38. {
  39. CleanDatabase(cancellationToken);
  40. progress.Report(100);
  41. return Task.CompletedTask;
  42. }
  43. private void CleanDatabase(CancellationToken cancellationToken)
  44. {
  45. var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
  46. var uninstalledChannels = _libraryManager.GetItemList(new InternalItemsQuery
  47. {
  48. IncludeItemTypes = new[] { typeof(Channel).Name },
  49. ExcludeItemIds = installedChannelIds.ToArray()
  50. });
  51. foreach (var channel in uninstalledChannels)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. CleanChannel((Channel)channel, cancellationToken);
  55. }
  56. }
  57. private void CleanChannel(Channel channel, CancellationToken cancellationToken)
  58. {
  59. _logger.LogInformation("Cleaning channel {0} from database", channel.Id);
  60. // Delete all channel items
  61. var items = _libraryManager.GetItemList(new InternalItemsQuery
  62. {
  63. ChannelIds = new[] { channel.Id }
  64. });
  65. foreach (var item in items)
  66. {
  67. cancellationToken.ThrowIfCancellationRequested();
  68. _libraryManager.DeleteItem(
  69. item,
  70. new DeleteOptions
  71. {
  72. DeleteFileLocation = false
  73. },
  74. false);
  75. }
  76. // Finally, delete the channel itself
  77. _libraryManager.DeleteItem(
  78. channel,
  79. new DeleteOptions
  80. {
  81. DeleteFileLocation = false
  82. },
  83. false);
  84. }
  85. }
  86. }