mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-17 02:49:05 -07:00
70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using MediaBrowser.Controller.Providers;
|
|||
|
|
|||
|
namespace MediaBrowser.Providers.Manager
|
|||
|
{
|
|||
|
public class ItemIdentifier<TLookupInfo, TIdentity>
|
|||
|
where TLookupInfo : ItemLookupInfo
|
|||
|
where TIdentity : IItemIdentity
|
|||
|
{
|
|||
|
public async Task<IEnumerable<TIdentity>> FindIdentities(TLookupInfo item, ProviderManager providerManager, CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
var providers = providerManager.GetItemIdentityProviders<TLookupInfo, TIdentity>();
|
|||
|
var converters = providerManager.GetItemIdentityConverters<TIdentity>();
|
|||
|
|
|||
|
var identities = new List<IdentityPair>();
|
|||
|
|
|||
|
foreach (var provider in providers)
|
|||
|
{
|
|||
|
var result = new IdentityPair
|
|||
|
{
|
|||
|
Identity = await provider.FindIdentity(item),
|
|||
|
Order = provider.Order
|
|||
|
};
|
|||
|
|
|||
|
identities.Add(result);
|
|||
|
}
|
|||
|
|
|||
|
var convertersAvailable = new List<IItemIdentityConverter<TIdentity>>(converters);
|
|||
|
bool changesMade;
|
|||
|
|
|||
|
do
|
|||
|
{
|
|||
|
changesMade = false;
|
|||
|
|
|||
|
for (int i = convertersAvailable.Count - 1; i >= 0; i--)
|
|||
|
{
|
|||
|
var converter = convertersAvailable[i];
|
|||
|
var input = identities.FirstOrDefault(id => id.Identity.Type == converter.SourceType);
|
|||
|
var existing = identities.Where(id => id.Identity.Type == converter.ResultType);
|
|||
|
|
|||
|
if (input != null && !existing.Any(id => id.Order >= converter.Order))
|
|||
|
{
|
|||
|
var result = new IdentityPair
|
|||
|
{
|
|||
|
Identity = await converter.Convert(input.Identity).ConfigureAwait(false),
|
|||
|
Order = converter.Order
|
|||
|
};
|
|||
|
|
|||
|
identities.Add(result);
|
|||
|
convertersAvailable.RemoveAt(i);
|
|||
|
changesMade = true;
|
|||
|
}
|
|||
|
}
|
|||
|
} while (changesMade);
|
|||
|
|
|||
|
return identities.Select(id => id.Identity);
|
|||
|
}
|
|||
|
|
|||
|
private class IdentityPair
|
|||
|
{
|
|||
|
public TIdentity Identity;
|
|||
|
public int Order;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|