2014-01-29 05:17:58 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-02-10 18:39:41 +00:00
|
|
|
|
using System.IO;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public abstract class MetadataService<TItemType, TIdType> : IMetadataService
|
2014-02-07 03:10:13 +00:00
|
|
|
|
where TItemType : IHasMetadata, IHasLookupInfo<TIdType>, new()
|
|
|
|
|
where TIdType : ItemLookupInfo, new()
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
protected readonly IServerConfigurationManager ServerConfigurationManager;
|
|
|
|
|
protected readonly ILogger Logger;
|
|
|
|
|
protected readonly IProviderManager ProviderManager;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
protected readonly IProviderRepository ProviderRepo;
|
|
|
|
|
protected readonly IFileSystem FileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
protected MetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IProviderRepository providerRepo, IFileSystem fileSystem)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
ServerConfigurationManager = serverConfigurationManager;
|
|
|
|
|
Logger = logger;
|
|
|
|
|
ProviderManager = providerManager;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
ProviderRepo = providerRepo;
|
|
|
|
|
FileSystem = fileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the provider result.
|
|
|
|
|
/// </summary>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
/// <param name="item">The item.</param>
|
2014-01-28 18:37:01 +00:00
|
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
protected Task SaveProviderResult(TItemType item, MetadataStatus result)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
|
result.ItemId = item.Id;
|
|
|
|
|
result.ItemName = item.Name;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.ItemType = item.GetType().Name;
|
2014-02-08 20:02:35 +00:00
|
|
|
|
|
|
|
|
|
var series = item as IHasSeries;
|
|
|
|
|
|
|
|
|
|
result.SeriesName = series == null ? null : series.SeriesName;
|
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
return ProviderRepo.SaveMetadataStatus(result, CancellationToken.None);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the last result.
|
|
|
|
|
/// </summary>
|
2014-03-16 16:15:10 +00:00
|
|
|
|
/// <param name="item">The item.</param>
|
2014-01-28 18:37:01 +00:00
|
|
|
|
/// <returns>ProviderResult.</returns>
|
2014-03-16 16:15:10 +00:00
|
|
|
|
protected MetadataStatus GetLastResult(IHasMetadata item)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-03-16 16:15:10 +00:00
|
|
|
|
if (item.DateLastSaved == default(DateTime))
|
|
|
|
|
{
|
|
|
|
|
return new MetadataStatus { ItemId = item.Id };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ProviderRepo.GetMetadataStatus(item.Id) ?? new MetadataStatus { ItemId = item.Id };
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
public async Task RefreshMetadata(IHasMetadata item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
if (refreshOptions.DirectoryService == null)
|
|
|
|
|
{
|
|
|
|
|
refreshOptions.DirectoryService = new DirectoryService(Logger);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
var itemOfType = (TItemType)item;
|
2014-02-09 23:08:01 +00:00
|
|
|
|
var config = ProviderManager.GetMetadataOptions(item);
|
2014-02-02 13:36:31 +00:00
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
var updateType = ItemUpdateType.None;
|
2014-03-16 16:15:10 +00:00
|
|
|
|
var refreshResult = GetLastResult(item);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
refreshResult.LastErrorMessage = string.Empty;
|
|
|
|
|
refreshResult.LastStatus = ProviderRefreshStatus.Success;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
var itemImageProvider = new ItemImageProvider(Logger, ProviderManager, ServerConfigurationManager, FileSystem);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
var localImagesFailed = false;
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item).ToList();
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
// Start by validating images
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Always validate images and check for new locally stored ones.
|
2014-02-08 22:38:02 +00:00
|
|
|
|
if (itemImageProvider.ValidateImages(item, allImageProviders.OfType<ILocalImageProvider>(), refreshOptions.DirectoryService))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
updateType = updateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
localImagesFailed = true;
|
|
|
|
|
Logger.ErrorException("Error validating images for {0}", ex, item.Path ?? item.Name);
|
|
|
|
|
refreshResult.AddStatus(ProviderRefreshStatus.Failure, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next run metadata providers
|
2014-02-02 13:36:31 +00:00
|
|
|
|
if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-03-24 17:54:45 +00:00
|
|
|
|
var providers = GetProviders(item, refreshResult.DateLastMetadataRefresh.HasValue, refreshOptions)
|
|
|
|
|
.ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (providers.Count > 0 || !refreshResult.DateLastMetadataRefresh.HasValue)
|
|
|
|
|
{
|
2014-02-13 05:11:54 +00:00
|
|
|
|
if (item.BeforeMetadataRefresh())
|
|
|
|
|
{
|
|
|
|
|
updateType = updateType | ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
if (providers.Count > 0)
|
|
|
|
|
{
|
2014-02-09 04:52:52 +00:00
|
|
|
|
var result = await RefreshWithProviders(itemOfType, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
updateType = updateType | result.UpdateType;
|
|
|
|
|
refreshResult.AddStatus(result.Status, result.ErrorMessage);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
refreshResult.SetDateLastMetadataRefresh(DateTime.UtcNow);
|
|
|
|
|
refreshResult.AddImageProvidersRefreshed(result.Providers);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next run remote image providers, but only if local image providers didn't throw an exception
|
2014-02-02 13:36:31 +00:00
|
|
|
|
if (!localImagesFailed && refreshOptions.ImageRefreshMode != ImageRefreshMode.ValidationOnly)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-03 17:44:13 +00:00
|
|
|
|
var providers = GetNonLocalImageProviders(item, allImageProviders, refreshResult.DateLastImagesRefresh, refreshOptions).ToList();
|
2014-01-29 17:23:38 +00:00
|
|
|
|
|
|
|
|
|
if (providers.Count > 0)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-02 13:36:31 +00:00
|
|
|
|
var result = await itemImageProvider.RefreshImages(itemOfType, providers, refreshOptions, config, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
updateType = updateType | result.UpdateType;
|
|
|
|
|
refreshResult.AddStatus(result.Status, result.ErrorMessage);
|
|
|
|
|
refreshResult.SetDateLastImagesRefresh(DateTime.UtcNow);
|
|
|
|
|
refreshResult.AddImageProvidersRefreshed(result.Providers);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 04:04:19 +00:00
|
|
|
|
updateType = updateType | BeforeSave(itemOfType);
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
var providersHadChanges = updateType > ItemUpdateType.None;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-11 21:41:01 +00:00
|
|
|
|
// Save if changes were made, or it's never been saved before
|
|
|
|
|
if (refreshOptions.ForceSave || providersHadChanges || item.DateLastSaved == default(DateTime))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
// Save to database
|
|
|
|
|
await SaveItem(itemOfType, updateType, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
if (providersHadChanges || refreshResult.IsDirty)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
|
await SaveProviderResult(itemOfType, refreshResult).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
/// <summary>
|
2014-02-04 04:04:19 +00:00
|
|
|
|
/// Befores the save.
|
2014-02-03 05:35:43 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>ItemUpdateType.</returns>
|
2014-02-04 04:04:19 +00:00
|
|
|
|
protected virtual ItemUpdateType BeforeSave(TItemType item)
|
2014-02-03 05:35:43 +00:00
|
|
|
|
{
|
2014-02-11 04:55:01 +00:00
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
|
|
|
|
|
{
|
|
|
|
|
item.Name = Path.GetFileNameWithoutExtension(item.Path);
|
2014-04-08 04:17:18 +00:00
|
|
|
|
updateType = updateType | ItemUpdateType.MetadataDownload;
|
2014-02-11 04:55:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateType;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the providers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="hasRefreshedMetadata">if set to <c>true</c> [has refreshed metadata].</param>
|
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
|
/// <returns>IEnumerable{`0}.</returns>
|
|
|
|
|
protected virtual IEnumerable<IMetadataProvider> GetProviders(IHasMetadata item, bool hasRefreshedMetadata, MetadataRefreshOptions options)
|
|
|
|
|
{
|
|
|
|
|
// Get providers to refresh
|
2014-02-01 06:18:16 +00:00
|
|
|
|
var providers = ((ProviderManager)ProviderManager).GetMetadataProviders<TItemType>(item).ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
// Run all if either of these flags are true
|
|
|
|
|
var runAllProviders = options.ReplaceAllMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || !hasRefreshedMetadata;
|
|
|
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentItem = item;
|
|
|
|
|
|
|
|
|
|
var providersWithChanges = providers.OfType<IHasChangeMonitor>()
|
2014-02-14 04:00:13 +00:00
|
|
|
|
.Where(i => HasChanged(currentItem, i, currentItem.DateLastSaved, options.DirectoryService))
|
2014-02-10 18:39:41 +00:00
|
|
|
|
.Cast<IMetadataProvider<TItemType>>()
|
2014-01-28 18:37:01 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
if (providersWithChanges.Count == 0)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
|
providers = new List<IMetadataProvider<TItemType>>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
providers = providers.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
// If any provider reports a change, always run local ones as well
|
|
|
|
|
if (i is ILocalMetadataProvider)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var anyRemoteProvidersChanged = providersWithChanges.OfType<IRemoteMetadataProvider>()
|
|
|
|
|
.Any();
|
|
|
|
|
|
|
|
|
|
// If any remote providers changed, run them all so that priorities can be honored
|
|
|
|
|
if (i is IRemoteMetadataProvider)
|
|
|
|
|
{
|
|
|
|
|
return anyRemoteProvidersChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run custom providers if they report a change or any remote providers change
|
|
|
|
|
return anyRemoteProvidersChanged || providersWithChanges.Contains(i);
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 17:44:13 +00:00
|
|
|
|
protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(IHasMetadata item, IEnumerable<IImageProvider> allImageProviders, DateTime? dateLastImageRefresh, ImageRefreshOptions options)
|
2014-01-29 17:23:38 +00:00
|
|
|
|
{
|
|
|
|
|
// Get providers to refresh
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var providers = allImageProviders.Where(i => !(i is ILocalImageProvider)).ToList();
|
2014-01-29 17:23:38 +00:00
|
|
|
|
|
|
|
|
|
// Run all if either of these flags are true
|
2014-02-03 17:44:13 +00:00
|
|
|
|
var runAllProviders = options.ImageRefreshMode == ImageRefreshMode.FullRefresh || !dateLastImageRefresh.HasValue;
|
2014-01-29 17:23:38 +00:00
|
|
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
|
{
|
|
|
|
|
providers = providers.OfType<IHasChangeMonitor>()
|
2014-02-14 04:00:13 +00:00
|
|
|
|
.Where(i => HasChanged(item, i, dateLastImageRefresh.Value, options.DirectoryService))
|
2014-01-29 17:23:38 +00:00
|
|
|
|
.Cast<IImageProvider>()
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
protected Task SaveItem(TItemType item, ItemUpdateType reason, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return item.UpdateToRepository(reason, cancellationToken);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
public bool CanRefresh(IHasMetadata item)
|
|
|
|
|
{
|
|
|
|
|
return item is TItemType;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 04:52:52 +00:00
|
|
|
|
protected virtual async Task<RefreshResult> RefreshWithProviders(TItemType item, MetadataRefreshOptions options, List<IMetadataProvider> providers, ItemImageProvider imageService, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-01-29 05:17:58 +00:00
|
|
|
|
var refreshResult = new RefreshResult
|
|
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
|
UpdateType = ItemUpdateType.None,
|
2014-01-29 05:17:58 +00:00
|
|
|
|
Providers = providers.Select(i => i.GetType().FullName.GetMD5()).ToList()
|
|
|
|
|
};
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-03-01 04:13:22 +00:00
|
|
|
|
var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider))
|
|
|
|
|
{
|
|
|
|
|
await RunCustomProvider(provider, item, options.DirectoryService, refreshResult, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var temp = CreateNew();
|
2014-02-04 20:19:29 +00:00
|
|
|
|
temp.Path = item.Path;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
// If replacing all metadata, run internet providers first
|
|
|
|
|
if (options.ReplaceAllMetadata)
|
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
await ExecuteRemoteProviders(item, temp, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasLocalMetadata = false;
|
|
|
|
|
|
|
|
|
|
foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>())
|
|
|
|
|
{
|
2014-02-17 21:35:08 +00:00
|
|
|
|
var providerName = provider.GetType().Name;
|
|
|
|
|
Logger.Debug("Running {0} for {1}", providerName, item.Path ?? item.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var itemInfo = new ItemInfo { Path = item.Path, IsInMixedFolder = item.IsInMixedFolder };
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var localItem = await provider.GetMetadata(itemInfo, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
if (localItem.HasMetadata)
|
|
|
|
|
{
|
2014-02-09 04:52:52 +00:00
|
|
|
|
if (imageService.MergeImages(item, localItem.Images))
|
|
|
|
|
{
|
2014-02-22 21:05:00 +00:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.ImageUpdate;
|
2014-02-09 04:52:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 02:11:01 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(localItem.Item.Name))
|
2014-02-03 05:35:43 +00:00
|
|
|
|
{
|
2014-03-12 02:11:01 +00:00
|
|
|
|
localItem.Item.Name = item.Name ?? Path.GetFileNameWithoutExtension(item.Path);
|
2014-02-03 05:35:43 +00:00
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-03-12 02:11:01 +00:00
|
|
|
|
MergeData(localItem.Item, temp, new List<MetadataFields>(), !options.ReplaceAllMetadata, true);
|
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataImport;
|
|
|
|
|
|
|
|
|
|
// Only one local provider allowed per item
|
|
|
|
|
hasLocalMetadata = true;
|
2014-04-05 15:02:50 +00:00
|
|
|
|
item.IsUnidentified = false;
|
2014-03-12 02:11:01 +00:00
|
|
|
|
break;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
}
|
2014-03-12 02:11:01 +00:00
|
|
|
|
|
|
|
|
|
Logger.Debug("{0} returned no metadata for {1}", providerName, item.Path ?? item.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// If a local provider fails, consider that a failure
|
|
|
|
|
refreshResult.Status = ProviderRefreshStatus.Failure;
|
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
|
|
|
|
|
// If the local provider fails don't continue with remote providers because the user's saved metadata could be lost
|
|
|
|
|
return refreshResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
// Local metadata is king - if any is found don't run remote providers
|
2014-02-15 16:36:09 +00:00
|
|
|
|
if (!options.ReplaceAllMetadata && (!hasLocalMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
await ExecuteRemoteProviders(item, temp, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
if (refreshResult.UpdateType > ItemUpdateType.None)
|
2014-02-01 06:18:16 +00:00
|
|
|
|
{
|
|
|
|
|
MergeData(temp, item, item.LockedFields, true, true);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-03-01 04:13:22 +00:00
|
|
|
|
foreach (var provider in customProviders.Where(i => !(i is IPreRefreshProvider)))
|
2014-02-02 13:36:31 +00:00
|
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
|
await RunCustomProvider(provider, item, options.DirectoryService, refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-02-02 13:36:31 +00:00
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
return refreshResult;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, IDirectoryService directoryService, RefreshResult refreshResult, CancellationToken cancellationToken)
|
2014-02-04 20:19:29 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | await provider.FetchAsync(item, directoryService, cancellationToken).ConfigureAwait(false);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
refreshResult.Status = ProviderRefreshStatus.CompletedWithErrors;
|
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
protected virtual TItemType CreateNew()
|
|
|
|
|
{
|
|
|
|
|
return new TItemType();
|
|
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
2014-02-07 03:10:13 +00:00
|
|
|
|
private async Task ExecuteRemoteProviders(TItemType item, TItemType temp, IEnumerable<IRemoteMetadataProvider<TItemType, TIdType>> providers, RefreshResult refreshResult, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
TIdType id = null;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-03-03 05:11:03 +00:00
|
|
|
|
var unidentifiedCount = 0;
|
|
|
|
|
var identifiedCount = 0;
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
foreach (var provider in providers)
|
|
|
|
|
{
|
2014-02-17 21:35:08 +00:00
|
|
|
|
var providerName = provider.GetType().Name;
|
|
|
|
|
Logger.Debug("Running {0} for {1}", providerName, item.Path ?? item.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
if (id == null)
|
|
|
|
|
{
|
|
|
|
|
id = item.GetLookupInfo();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MergeNewData(temp, id);
|
|
|
|
|
}
|
2014-02-07 03:10:13 +00:00
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result.HasMetadata)
|
|
|
|
|
{
|
|
|
|
|
MergeData(result.Item, temp, new List<MetadataFields>(), false, false);
|
|
|
|
|
|
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataDownload;
|
2014-03-01 04:13:22 +00:00
|
|
|
|
|
2014-03-03 05:11:03 +00:00
|
|
|
|
identifiedCount++;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
2014-02-17 21:35:08 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2014-03-03 05:11:03 +00:00
|
|
|
|
unidentifiedCount++;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
Logger.Debug("{0} returned no metadata for {1}", providerName, item.Path ?? item.Name);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-03-03 05:11:03 +00:00
|
|
|
|
unidentifiedCount++;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
refreshResult.Status = ProviderRefreshStatus.CompletedWithErrors;
|
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-03 05:11:03 +00:00
|
|
|
|
|
|
|
|
|
var isUnidentified = unidentifiedCount > 0 && identifiedCount == 0;
|
|
|
|
|
|
|
|
|
|
if (item.IsUnidentified != isUnidentified)
|
|
|
|
|
{
|
|
|
|
|
item.IsUnidentified = isUnidentified;
|
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
private void MergeNewData(TItemType source, TIdType lookupInfo)
|
|
|
|
|
{
|
|
|
|
|
// Copy new provider id's that may have been obtained
|
|
|
|
|
foreach (var providerId in source.ProviderIds)
|
|
|
|
|
{
|
2014-02-13 05:11:54 +00:00
|
|
|
|
var key = providerId.Key;
|
|
|
|
|
|
|
|
|
|
// Don't replace existing Id's.
|
|
|
|
|
if (!lookupInfo.ProviderIds.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
lookupInfo.ProviderIds[key] = providerId.Value;
|
|
|
|
|
}
|
2014-02-08 20:02:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
protected abstract void MergeData(TItemType source, TItemType target, List<MetadataFields> lockedFields, bool replaceData, bool mergeMetadataSettings);
|
|
|
|
|
|
|
|
|
|
public virtual int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-14 04:00:13 +00:00
|
|
|
|
|
|
|
|
|
private bool HasChanged(IHasMetadata item, IHasChangeMonitor changeMonitor, DateTime date, IDirectoryService directoryService)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return changeMonitor.HasChanged(item, directoryService, date);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error in {0}.HasChanged", ex, changeMonitor.GetType().Name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RefreshResult
|
|
|
|
|
{
|
|
|
|
|
public ItemUpdateType UpdateType { get; set; }
|
|
|
|
|
public ProviderRefreshStatus Status { get; set; }
|
|
|
|
|
public string ErrorMessage { get; set; }
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public List<Guid> Providers { get; set; }
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|