You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../Sync/SyncedMediaSourceProvider.cs

112 lines
4.1 KiB

10 years ago
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Dto;
10 years ago
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Sync;
10 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
10 years ago
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
10 years ago
private readonly ILogger _logger;
10 years ago
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost, ILogger logger)
{
10 years ago
_appHost = appHost;
10 years ago
_logger = logger;
10 years ago
_syncManager = (SyncManager)syncManager;
}
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
10 years ago
Statuses = new[] { SyncJobItemStatus.Synced },
ItemId = item.Id.ToString("N")
});
10 years ago
var list = new List<MediaSourceInfo>();
10 years ago
if (jobItemResult.Items.Length > 0)
{
var targets = _syncManager.ServerSyncProviders
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
var serverId = _appHost.SystemId;
10 years ago
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;
10 years ago
var syncProvider = targetTuple.Item1;
10 years ago
var dataProvider = _syncManager.GetDataProvider(targetTuple.Item1, syncTarget);
var localItems = await dataProvider.GetCachedItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
10 years ago
foreach (var localItem in localItems)
{
10 years ago
foreach (var mediaSource in localItem.Item.MediaSources)
{
await TryAddMediaSource(list, localItem, mediaSource, syncProvider, syncTarget, cancellationToken).ConfigureAwait(false);
}
}
10 years ago
}
}
}
return list;
}
10 years ago
private async Task TryAddMediaSource(List<MediaSourceInfo> list,
LocalItem item,
MediaSourceInfo mediaSource,
IServerSyncProvider provider,
SyncTarget target,
CancellationToken cancellationToken)
{
10 years ago
var requiresDynamicAccess = provider as IHasDynamicAccess;
10 years ago
if (requiresDynamicAccess == null)
{
list.Add(mediaSource);
return;
}
try
{
10 years ago
var dynamicInfo = await requiresDynamicAccess.GetSyncedFileInfo(item.LocalPath, target, cancellationToken).ConfigureAwait(false);
10 years ago
foreach (var stream in mediaSource.MediaStreams)
{
10 years ago
var dynamicStreamInfo = await requiresDynamicAccess.GetSyncedFileInfo(stream.ExternalId, target, cancellationToken).ConfigureAwait(false);
10 years ago
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);
}
}
}
}