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

68 lines
2.4 KiB
C#
Raw Normal View History

2015-03-08 05:37:48 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2015-03-07 22:43:53 +00:00
using MediaBrowser.Controller.Library;
2015-03-07 23:39:24 +00:00
using MediaBrowser.Controller.Sync;
2015-03-07 22:43:53 +00:00
using MediaBrowser.Model.Dto;
2015-03-07 23:39:24 +00:00
using MediaBrowser.Model.Sync;
2015-03-08 05:37:48 +00:00
using System;
2015-03-07 22:43:53 +00:00
using System.Collections.Generic;
2015-03-07 23:39:24 +00:00
using System.Linq;
2015-03-07 22:43:53 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
2015-03-08 05:37:48 +00:00
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
2015-03-07 23:39:24 +00:00
2015-03-08 05:37:48 +00:00
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost)
2015-03-07 23:39:24 +00:00
{
2015-03-08 05:37:48 +00:00
_appHost = appHost;
_syncManager = (SyncManager)syncManager;
2015-03-07 23:39:24 +00:00
}
2015-03-07 22:43:53 +00:00
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
2015-03-07 23:39:24 +00:00
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Synced },
ItemId = item.Id.ToString("N")
});
2015-03-08 05:37:48 +00:00
var list = new List<MediaSourceInfo>();
2015-03-07 23:39:24 +00:00
2015-03-08 05:37:48 +00:00
if (jobItemResult.Items.Length > 0)
{
var targets = _syncManager.ServerSyncProviders
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
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);
var localItemId = MediaSync.GetLocalId(_appHost.SystemId, item.Id.ToString("N"));
var localItem = await dataProvider.GetCachedItem(syncTarget, localItemId).ConfigureAwait(false);
if (localItem != null)
{
list.AddRange(localItem.Item.MediaSources);
}
}
}
}
return list;
2015-03-07 22:43:53 +00:00
}
}
}