2014-09-28 09:50:33 -07:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2015-08-21 08:55:11 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-12-15 22:01:57 -07:00
|
|
|
|
using MoreLinq;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
using System;
|
2014-09-28 09:50:33 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Channels
|
|
|
|
|
{
|
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 IUserManager _userManager;
|
|
|
|
|
private readonly ILogger _logger;
|
2015-08-22 10:50:37 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
2015-08-22 10:50:37 -07:00
|
|
|
|
public ChannelPostScanTask(IChannelManager channelManager, IUserManager userManager, ILogger logger, ILibraryManager libraryManager)
|
2014-09-22 14:56:54 -07:00
|
|
|
|
{
|
|
|
|
|
_channelManager = channelManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_logger = logger;
|
2015-08-22 10:50:37 -07:00
|
|
|
|
_libraryManager = libraryManager;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var users = _userManager.Users
|
2015-08-21 08:55:11 -07:00
|
|
|
|
.DistinctBy(GetUserDistinctValue)
|
2014-09-22 14:56:54 -07:00
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
|
|
|
|
double percentPerUser = 1;
|
|
|
|
|
percentPerUser /= users.Count;
|
|
|
|
|
var startingPercent = numComplete * percentPerUser * 100;
|
|
|
|
|
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
2016-03-27 14:11:27 -07:00
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(startingPercent + percentPerUser * p));
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
|
|
|
|
await DownloadContent(user, cancellationToken, innerProgress).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= users.Count;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 10:50:37 -07:00
|
|
|
|
await CleanDatabase(cancellationToken).ConfigureAwait(false);
|
2016-03-20 14:32:26 -07:00
|
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 08:55:11 -07:00
|
|
|
|
public static string GetUserDistinctValue(User user)
|
|
|
|
|
{
|
|
|
|
|
var channels = user.Policy.EnabledChannels
|
|
|
|
|
.OrderBy(i => i)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return string.Join("|", channels.ToArray());
|
|
|
|
|
}
|
2015-08-22 10:50:37 -07:00
|
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
|
private async Task DownloadContent(string user, CancellationToken cancellationToken, IProgress<double> progress)
|
|
|
|
|
{
|
|
|
|
|
var channels = await _channelManager.GetChannelsInternal(new ChannelQuery
|
|
|
|
|
{
|
|
|
|
|
UserId = user
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
2014-09-28 09:50:33 -07:00
|
|
|
|
var numItems = channels.Items.Length;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
|
|
|
|
foreach (var channel in channels.Items)
|
|
|
|
|
{
|
2014-09-28 08:27:26 -07:00
|
|
|
|
var channelId = channel.Id.ToString("N");
|
|
|
|
|
|
|
|
|
|
var features = _channelManager.GetChannelFeatures(channelId);
|
|
|
|
|
|
|
|
|
|
const int currentRefreshLevel = 1;
|
2014-12-18 21:20:07 -07:00
|
|
|
|
var maxRefreshLevel = features.AutoRefreshLevels ?? 0;
|
2015-10-06 07:59:42 -07:00
|
|
|
|
maxRefreshLevel = Math.Max(maxRefreshLevel, 2);
|
2014-09-28 08:27:26 -07:00
|
|
|
|
|
2014-12-18 21:20:07 -07:00
|
|
|
|
if (maxRefreshLevel > 0)
|
2014-09-22 14:56:54 -07:00
|
|
|
|
{
|
2014-12-18 21:20:07 -07:00
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
var startingNumberComplete = numComplete;
|
|
|
|
|
innerProgress.RegisterAction(p =>
|
|
|
|
|
{
|
|
|
|
|
double innerPercent = startingNumberComplete;
|
2016-03-27 14:11:27 -07:00
|
|
|
|
innerPercent += p / 100;
|
2014-12-18 21:20:07 -07:00
|
|
|
|
innerPercent /= numItems;
|
|
|
|
|
progress.Report(innerPercent * 100);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await GetAllItems(user, channelId, null, currentRefreshLevel, maxRefreshLevel, innerProgress, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting channel content", ex);
|
|
|
|
|
}
|
2014-09-22 14:56:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
2014-09-28 09:50:33 -07:00
|
|
|
|
percent /= numItems;
|
2014-09-22 14:56:54 -07:00
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 10:50:37 -07:00
|
|
|
|
private async Task CleanDatabase(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-11-06 08:02:22 -07:00
|
|
|
|
var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
|
2015-08-22 10:50:37 -07:00
|
|
|
|
|
2015-11-06 08:02:22 -07:00
|
|
|
|
var databaseIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
2015-08-22 10:50:37 -07:00
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Channel).Name }
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-06 08:02:22 -07:00
|
|
|
|
var invalidIds = databaseIds
|
|
|
|
|
.Except(installedChannelIds)
|
2015-08-22 10:50:37 -07:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var id in invalidIds)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
await CleanChannel(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CleanChannel(Guid id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-10-04 15:04:56 -07:00
|
|
|
|
_logger.Info("Cleaning channel {0} from database", id);
|
2015-08-22 10:50:37 -07:00
|
|
|
|
|
|
|
|
|
// Delete all channel items
|
|
|
|
|
var allIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelIds = new[] { id.ToString("N") }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
foreach (var deleteId in allIds)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
await DeleteItem(deleteId).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finally, delete the channel itself
|
|
|
|
|
await DeleteItem(id).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task DeleteItem(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
|
2016-03-20 14:32:26 -07:00
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 10:50:37 -07:00
|
|
|
|
return _libraryManager.DeleteItem(item, new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-28 09:50:33 -07:00
|
|
|
|
private async Task GetAllItems(string user, string channelId, string folderId, int currentRefreshLevel, int maxRefreshLevel, IProgress<double> progress, CancellationToken cancellationToken)
|
2014-09-22 14:56:54 -07:00
|
|
|
|
{
|
|
|
|
|
var folderItems = new List<string>();
|
|
|
|
|
|
2014-09-29 21:47:30 -07:00
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(p / 2));
|
|
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
|
var result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelId = channelId,
|
|
|
|
|
UserId = user,
|
|
|
|
|
FolderId = folderId
|
|
|
|
|
|
2014-09-29 21:47:30 -07:00
|
|
|
|
}, innerProgress, cancellationToken);
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
|
|
|
|
folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));
|
|
|
|
|
|
|
|
|
|
var totalRetrieved = result.Items.Length;
|
|
|
|
|
var totalCount = result.TotalRecordCount;
|
|
|
|
|
|
|
|
|
|
while (totalRetrieved < totalCount)
|
|
|
|
|
{
|
|
|
|
|
result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelId = channelId,
|
|
|
|
|
UserId = user,
|
|
|
|
|
StartIndex = totalRetrieved,
|
|
|
|
|
FolderId = folderId
|
|
|
|
|
|
2014-09-29 21:47:30 -07:00
|
|
|
|
}, new Progress<double>(), cancellationToken);
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
|
|
|
|
folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));
|
2014-09-28 09:50:33 -07:00
|
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
|
totalRetrieved += result.Items.Length;
|
|
|
|
|
totalCount = result.TotalRecordCount;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-28 13:49:43 -07:00
|
|
|
|
progress.Report(50);
|
|
|
|
|
|
2014-09-28 08:27:26 -07:00
|
|
|
|
if (currentRefreshLevel < maxRefreshLevel)
|
2014-09-22 14:56:54 -07:00
|
|
|
|
{
|
2014-09-28 13:49:43 -07:00
|
|
|
|
var numComplete = 0;
|
|
|
|
|
var numItems = folderItems.Count;
|
2014-09-29 21:47:30 -07:00
|
|
|
|
|
2014-09-22 14:56:54 -07:00
|
|
|
|
foreach (var folder in folderItems)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-09-29 21:47:30 -07:00
|
|
|
|
innerProgress = new ActionableProgress<double>();
|
2014-09-28 13:49:43 -07:00
|
|
|
|
|
|
|
|
|
var startingNumberComplete = numComplete;
|
|
|
|
|
innerProgress.RegisterAction(p =>
|
|
|
|
|
{
|
|
|
|
|
double innerPercent = startingNumberComplete;
|
2016-03-27 14:11:27 -07:00
|
|
|
|
innerPercent += p / 100;
|
2014-09-28 13:49:43 -07:00
|
|
|
|
innerPercent /= numItems;
|
2016-03-27 14:11:27 -07:00
|
|
|
|
progress.Report(innerPercent * 50 + 50);
|
2014-09-28 13:49:43 -07:00
|
|
|
|
});
|
2014-09-29 21:47:30 -07:00
|
|
|
|
|
2014-09-28 09:50:33 -07:00
|
|
|
|
await GetAllItems(user, channelId, folder, currentRefreshLevel + 1, maxRefreshLevel, innerProgress, cancellationToken).ConfigureAwait(false);
|
2014-09-22 14:56:54 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting channel content", ex);
|
|
|
|
|
}
|
2014-09-28 13:49:43 -07:00
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numItems;
|
2016-03-27 14:11:27 -07:00
|
|
|
|
progress.Report(percent * 50 + 50);
|
2014-09-22 14:56:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-28 13:49:43 -07:00
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
2014-09-22 14:56:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|