jellyfin/MediaBrowser.Server.Implementations/Sync/SyncedMediaSourceProvider.cs

66 lines
2.3 KiB
C#
Raw Normal View History

2015-03-08 06:37:48 +01:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2015-03-07 23:43:53 +01:00
using MediaBrowser.Controller.Library;
2015-03-08 00:39:24 +01:00
using MediaBrowser.Controller.Sync;
2015-03-07 23:43:53 +01:00
using MediaBrowser.Model.Dto;
2015-03-08 00:39:24 +01:00
using MediaBrowser.Model.Sync;
2015-03-08 06:37:48 +01:00
using System;
2015-03-07 23:43:53 +01:00
using System.Collections.Generic;
2015-03-08 00:39:24 +01:00
using System.Linq;
2015-03-07 23:43:53 +01:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
2015-03-08 06:37:48 +01:00
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
2015-03-08 00:39:24 +01:00
2015-03-08 06:37:48 +01:00
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost)
2015-03-08 00:39:24 +01:00
{
2015-03-08 06:37:48 +01:00
_appHost = appHost;
_syncManager = (SyncManager)syncManager;
2015-03-08 00:39:24 +01:00
}
2015-03-07 23:43:53 +01:00
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
2015-03-08 00:39:24 +01:00
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Synced },
ItemId = item.Id.ToString("N")
});
2015-03-08 06:37:48 +01:00
var list = new List<MediaSourceInfo>();
2015-03-08 00:39:24 +01:00
2015-03-08 06:37:48 +01:00
if (jobItemResult.Items.Length > 0)
{
var targets = _syncManager.ServerSyncProviders
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
2015-03-10 19:10:38 +01:00
var serverId = _appHost.SystemId;
2015-03-08 06:37:48 +01:00
foreach (var jobItem in jobItemResult.Items)
{
var targetTuple = targets.FirstOrDefault(i => string.Equals(i.Item2.Id, jobItem.TargetId, StringComparison.OrdinalIgnoreCase));
if (targetTuple != null)
{
var syncTarget = targetTuple.Item2;
var dataProvider = _syncManager.GetDataProvider(targetTuple.Item1, syncTarget);
2015-03-10 19:10:38 +01:00
var localItems = await dataProvider.GetCachedItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
2015-03-08 06:37:48 +01:00
2015-03-10 19:10:38 +01:00
list.AddRange(localItems.SelectMany(i => i.Item.MediaSources));
2015-03-08 06:37:48 +01:00
}
}
}
return list;
2015-03-07 23:43:53 +01:00
}
}
}