2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2014-09-22 17:56:54 -04:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:20:16 -05:00
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-09-22 17:56:54 -04:00
|
|
|
|
2016-11-02 16:53:50 -04:00
|
|
|
namespace Emby.Server.Implementations.Channels
|
2014-09-22 17:56:54 -04:00
|
|
|
{
|
2015-07-28 23:42:03 -04:00
|
|
|
public class ChannelPostScanTask
|
2014-09-22 17:56:54 -04:00
|
|
|
{
|
|
|
|
private readonly IChannelManager _channelManager;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly ILogger _logger;
|
2015-08-22 13:50:37 -04:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-09-22 17:56:54 -04:00
|
|
|
|
2015-08-22 13:50:37 -04:00
|
|
|
public ChannelPostScanTask(IChannelManager channelManager, IUserManager userManager, ILogger logger, ILibraryManager libraryManager)
|
2014-09-22 17:56:54 -04:00
|
|
|
{
|
|
|
|
_channelManager = channelManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
_logger = logger;
|
2015-08-22 13:50:37 -04:00
|
|
|
_libraryManager = libraryManager;
|
2014-09-22 17:56:54 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
2014-09-22 17:56:54 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
CleanDatabase(cancellationToken);
|
2016-03-20 17:32:26 -04:00
|
|
|
|
2014-09-22 17:56:54 -04:00
|
|
|
progress.Report(100);
|
2018-09-12 13:26:21 -04:00
|
|
|
return Task.CompletedTask;
|
2014-09-22 17:56:54 -04:00
|
|
|
}
|
|
|
|
|
2015-08-21 11:55:11 -04:00
|
|
|
public static string GetUserDistinctValue(User user)
|
|
|
|
{
|
|
|
|
var channels = user.Policy.EnabledChannels
|
|
|
|
.OrderBy(i => i)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
return string.Join("|", channels.ToArray());
|
|
|
|
}
|
2015-08-22 13:50:37 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private void CleanDatabase(CancellationToken cancellationToken)
|
2015-08-22 13:50:37 -04:00
|
|
|
{
|
2015-11-06 10:02:22 -05:00
|
|
|
var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
|
2015-08-22 13:50:37 -04:00
|
|
|
|
2015-11-06 10:02:22 -05:00
|
|
|
var databaseIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
2015-08-22 13:50:37 -04:00
|
|
|
{
|
|
|
|
IncludeItemTypes = new[] { typeof(Channel).Name }
|
|
|
|
});
|
|
|
|
|
2015-11-06 10:02:22 -05:00
|
|
|
var invalidIds = databaseIds
|
|
|
|
.Except(installedChannelIds)
|
2015-08-22 13:50:37 -04:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
foreach (var id in invalidIds)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
CleanChannel(id, cancellationToken);
|
2015-08-22 13:50:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private void CleanChannel(Guid id, CancellationToken cancellationToken)
|
2015-08-22 13:50:37 -04:00
|
|
|
{
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogInformation("Cleaning channel {0} from database", id);
|
2015-08-22 13:50:37 -04:00
|
|
|
|
|
|
|
// Delete all channel items
|
|
|
|
var allIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
ChannelIds = new[] { id }
|
2015-08-22 13:50:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
foreach (var deleteId in allIds)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
DeleteItem(deleteId);
|
2015-08-22 13:50:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, delete the channel itself
|
2018-09-12 13:26:21 -04:00
|
|
|
DeleteItem(id);
|
2015-08-22 13:50:37 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private void DeleteItem(Guid id)
|
2015-08-22 13:50:37 -04:00
|
|
|
{
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
2016-03-20 17:32:26 -04:00
|
|
|
if (item == null)
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
return;
|
2016-03-20 17:32:26 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
_libraryManager.DeleteItem(item, new DeleteOptions
|
2015-08-22 13:50:37 -04:00
|
|
|
{
|
|
|
|
DeleteFileLocation = false
|
2014-09-22 17:56:54 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
}, false);
|
2014-09-22 17:56:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|