jellyfin-server/MediaBrowser.Server.Implementations/Sync/SyncedMediaSourceProvider.cs
2015-03-10 22:38:31 -04:00

90 lines
3.0 KiB
C#

using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Sync;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost)
{
_appHost = appHost;
_syncManager = (SyncManager)syncManager;
}
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Synced },
ItemId = item.Id.ToString("N")
});
var list = new List<MediaSourceInfo>();
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;
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 localItems = await dataProvider.GetCachedItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
foreach (var localItem in localItems)
{
list.AddRange(GetPlayableMediaSources(localItem));
}
}
}
}
return list;
}
private IEnumerable<MediaSourceInfo> GetPlayableMediaSources(LocalItem item)
{
return item.Item.MediaSources
.Where(IsMediaSourcePlayable);
}
private bool IsMediaSourcePlayable(MediaSourceInfo mediaSource)
{
if (mediaSource.Protocol == MediaProtocol.File)
{
if (!File.Exists(mediaSource.Path))
{
return false;
}
}
return true;
}
}
}