2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2014-09-22 14:56:54 -07:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-12-11 19:31:30 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2019-01-13 12:20:16 -07:00
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
2016-11-02 13:53:50 -07:00
|
|
|
namespace Emby.Server.Implementations.Channels
|
2014-09-22 14:56:54 -07:00
|
|
|
{
|
2020-04-14 12:36:29 -07:00
|
|
|
/// <summary>
|
2020-04-19 10:39:12 -07:00
|
|
|
/// A task to remove all non-installed channels from the database.
|
2020-04-14 12:36:29 -07:00
|
|
|
/// </summary>
|
2015-07-28 20:42:03 -07:00
|
|
|
public class ChannelPostScanTask
|
2014-09-22 14:56:54 -07:00
|
|
|
{
|
|
|
|
private readonly IChannelManager _channelManager;
|
|
|
|
private readonly ILogger _logger;
|
2015-08-22 10:50:37 -07:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
2020-04-14 12:36:29 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ChannelPostScanTask"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="channelManager">The channel manager.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
public ChannelPostScanTask(IChannelManager channelManager, ILogger logger, ILibraryManager libraryManager)
|
2014-09-22 14:56:54 -07:00
|
|
|
{
|
|
|
|
_channelManager = channelManager;
|
|
|
|
_logger = logger;
|
2015-08-22 10:50:37 -07:00
|
|
|
_libraryManager = libraryManager;
|
2014-09-22 14:56:54 -07:00
|
|
|
}
|
|
|
|
|
2020-04-14 12:36:29 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Runs this task.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>The completed task.</returns>
|
2018-09-12 10:26:21 -07:00
|
|
|
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
2014-09-22 14:56:54 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
CleanDatabase(cancellationToken);
|
2016-03-20 14:32:26 -07:00
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
progress.Report(100);
|
2018-09-12 10:26:21 -07:00
|
|
|
return Task.CompletedTask;
|
2014-09-22 14:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private void CleanDatabase(CancellationToken cancellationToken)
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
2015-11-06 08:02:22 -07:00
|
|
|
var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
|
2015-08-22 10:50:37 -07:00
|
|
|
|
2019-02-14 09:02:46 -07:00
|
|
|
var uninstalledChannels = _libraryManager.GetItemList(new InternalItemsQuery
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
2021-12-11 19:31:30 -07:00
|
|
|
IncludeItemTypes = new[] { BaseItemKind.Channel },
|
2019-02-13 14:10:37 -07:00
|
|
|
ExcludeItemIds = installedChannelIds.ToArray()
|
2015-08-22 10:50:37 -07:00
|
|
|
});
|
|
|
|
|
2019-02-14 09:02:46 -07:00
|
|
|
foreach (var channel in uninstalledChannels)
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-02-14 09:02:46 -07:00
|
|
|
CleanChannel((Channel)channel, cancellationToken);
|
2015-08-22 10:50:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 14:10:37 -07:00
|
|
|
private void CleanChannel(Channel channel, CancellationToken cancellationToken)
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
2019-02-13 14:10:37 -07:00
|
|
|
_logger.LogInformation("Cleaning channel {0} from database", channel.Id);
|
2015-08-22 10:50:37 -07:00
|
|
|
|
|
|
|
// Delete all channel items
|
2019-02-13 14:10:37 -07:00
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
2019-02-13 14:10:37 -07:00
|
|
|
ChannelIds = new[] { channel.Id }
|
2015-08-22 10:50:37 -07:00
|
|
|
});
|
|
|
|
|
2019-02-13 14:10:37 -07:00
|
|
|
foreach (var item in items)
|
2015-08-22 10:50:37 -07:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2020-01-31 14:23:46 -07:00
|
|
|
_libraryManager.DeleteItem(
|
|
|
|
item,
|
|
|
|
new DeleteOptions
|
|
|
|
{
|
|
|
|
DeleteFileLocation = false
|
|
|
|
},
|
|
|
|
false);
|
2016-03-20 14:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:10:37 -07:00
|
|
|
// Finally, delete the channel itself
|
2020-01-31 14:23:46 -07:00
|
|
|
_libraryManager.DeleteItem(
|
|
|
|
channel,
|
|
|
|
new DeleteOptions
|
|
|
|
{
|
|
|
|
DeleteFileLocation = false
|
|
|
|
},
|
|
|
|
false);
|
2014-09-22 14:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|