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

112 lines
4.1 KiB
C#
Raw Normal View History

2015-03-07 22:37:48 -07:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2015-03-07 15:43:53 -07:00
using MediaBrowser.Controller.Library;
2015-03-07 16:39:24 -07:00
using MediaBrowser.Controller.Sync;
2015-03-07 15:43:53 -07:00
using MediaBrowser.Model.Dto;
2015-03-27 19:19:20 -07:00
using MediaBrowser.Model.Logging;
2015-03-07 16:39:24 -07:00
using MediaBrowser.Model.Sync;
2015-03-07 22:37:48 -07:00
using System;
2015-03-07 15:43:53 -07:00
using System.Collections.Generic;
2015-03-07 16:39:24 -07:00
using System.Linq;
2015-03-07 15:43:53 -07:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
2015-03-07 22:37:48 -07:00
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
2015-03-27 19:19:20 -07:00
private readonly ILogger _logger;
2015-03-07 16:39:24 -07:00
2015-03-27 19:19:20 -07:00
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost, ILogger logger)
2015-03-07 16:39:24 -07:00
{
2015-03-07 22:37:48 -07:00
_appHost = appHost;
2015-03-27 19:19:20 -07:00
_logger = logger;
2015-03-07 22:37:48 -07:00
_syncManager = (SyncManager)syncManager;
2015-03-07 16:39:24 -07:00
}
2015-03-07 15:43:53 -07:00
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
2015-03-07 16:39:24 -07:00
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
2015-03-27 19:19:20 -07:00
Statuses = new[] { SyncJobItemStatus.Synced },
2015-03-07 16:39:24 -07:00
ItemId = item.Id.ToString("N")
});
2015-03-07 22:37:48 -07:00
var list = new List<MediaSourceInfo>();
2015-03-07 16:39:24 -07:00
2015-03-07 22:37:48 -07: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 11:10:38 -07:00
var serverId = _appHost.SystemId;
2015-03-07 22:37:48 -07: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;
2015-03-27 19:19:20 -07:00
var syncProvider = targetTuple.Item1;
2015-03-07 22:37:48 -07:00
var dataProvider = _syncManager.GetDataProvider(targetTuple.Item1, syncTarget);
2015-03-10 11:10:38 -07:00
var localItems = await dataProvider.GetCachedItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
2015-03-07 22:37:48 -07:00
foreach (var localItem in localItems)
{
2015-03-27 19:19:20 -07:00
foreach (var mediaSource in localItem.Item.MediaSources)
{
await TryAddMediaSource(list, localItem, mediaSource, syncProvider, syncTarget, cancellationToken).ConfigureAwait(false);
}
}
2015-03-07 22:37:48 -07:00
}
}
}
return list;
2015-03-07 15:43:53 -07:00
}
2015-03-27 19:19:20 -07:00
private async Task TryAddMediaSource(List<MediaSourceInfo> list,
LocalItem item,
MediaSourceInfo mediaSource,
IServerSyncProvider provider,
SyncTarget target,
CancellationToken cancellationToken)
{
var requiresDynamicAccess = provider as IRequiresDynamicAccess;
if (requiresDynamicAccess == null)
{
list.Add(mediaSource);
return;
}
try
{
var dynamicInfo = await requiresDynamicAccess.GetFileInfo(item.LocalPath, target, cancellationToken).ConfigureAwait(false);
foreach (var stream in mediaSource.MediaStreams)
{
var dynamicStreamInfo = await requiresDynamicAccess.GetFileInfo(stream.ExternalId, target, cancellationToken).ConfigureAwait(false);
stream.Path = dynamicStreamInfo.Path;
}
mediaSource.Path = dynamicInfo.Path;
mediaSource.Protocol = dynamicInfo.Protocol;
list.Add(mediaSource);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting dynamic media source info", ex);
}
}
2015-03-07 15:43:53 -07:00
}
}