jellyfin/Emby.Server.Implementations/Sync/MultiProviderSync.cs

80 lines
2.9 KiB
C#
Raw Normal View History

2015-04-05 15:01:57 +00:00
using MediaBrowser.Common.Configuration;
2015-02-28 13:42:47 +00:00
using MediaBrowser.Common.Progress;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Sync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
2016-11-03 22:34:16 +00:00
using MediaBrowser.Model.Cryptography;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2015-02-28 13:42:47 +00:00
2016-11-03 22:34:16 +00:00
namespace Emby.Server.Implementations.Sync
2015-02-28 13:42:47 +00:00
{
public class MultiProviderSync
{
2015-03-08 04:44:31 +00:00
private readonly SyncManager _syncManager;
2015-02-28 13:42:47 +00:00
private readonly IServerApplicationHost _appHost;
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
2015-04-05 15:01:57 +00:00
private readonly IConfigurationManager _config;
2016-11-03 22:34:16 +00:00
private readonly ICryptographyProvider _cryptographyProvider;
2015-02-28 13:42:47 +00:00
2016-11-03 22:34:16 +00:00
public MultiProviderSync(SyncManager syncManager, IServerApplicationHost appHost, ILogger logger, IFileSystem fileSystem, IConfigurationManager config, ICryptographyProvider cryptographyProvider)
2015-02-28 13:42:47 +00:00
{
_syncManager = syncManager;
_appHost = appHost;
_logger = logger;
_fileSystem = fileSystem;
2015-04-05 15:01:57 +00:00
_config = config;
2016-11-03 22:34:16 +00:00
_cryptographyProvider = cryptographyProvider;
2015-02-28 13:42:47 +00:00
}
public async Task Sync(IEnumerable<IServerSyncProvider> providers, IProgress<double> progress, CancellationToken cancellationToken)
{
var targets = providers
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
var numComplete = 0;
double startingPercent = 0;
double percentPerItem = 1;
if (targets.Count > 0)
{
percentPerItem /= targets.Count;
}
foreach (var target in targets)
{
cancellationToken.ThrowIfCancellationRequested();
var currentPercent = startingPercent;
var innerProgress = new ActionableProgress<double>();
innerProgress.RegisterAction(pct =>
{
var totalProgress = pct * percentPerItem;
totalProgress += currentPercent;
progress.Report(totalProgress);
});
2015-03-08 04:44:31 +00:00
var dataProvider = _syncManager.GetDataProvider(target.Item1, target.Item2);
2016-11-03 22:34:16 +00:00
await new MediaSync(_logger, _syncManager, _appHost, _fileSystem, _config, _cryptographyProvider)
2015-03-08 04:44:31 +00:00
.Sync(target.Item1, dataProvider, target.Item2, innerProgress, cancellationToken)
2015-02-28 13:42:47 +00:00
.ConfigureAwait(false);
numComplete++;
startingPercent = numComplete;
startingPercent /= targets.Count;
startingPercent *= 100;
progress.Report(startingPercent);
}
}
}
}