jellyfin/MediaBrowser.Providers/Manager/MetadataService.cs

791 lines
31 KiB
C#
Raw Normal View History

2014-09-28 15:27:26 +00:00
using MediaBrowser.Common.Extensions;
2014-01-29 05:17:58 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Configuration;
2016-04-06 02:18:56 +00:00
using MediaBrowser.Model.Providers;
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()
{
protected readonly IServerConfigurationManager ServerConfigurationManager;
protected readonly ILogger Logger;
protected readonly IProviderManager ProviderManager;
2014-02-02 13:36:31 +00:00
protected readonly IFileSystem FileSystem;
protected readonly IUserDataManager UserDataManager;
2015-06-29 01:10:45 +00:00
protected readonly ILibraryManager LibraryManager;
2016-05-21 06:12:00 +00:00
protected MetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager)
{
ServerConfigurationManager = serverConfigurationManager;
Logger = logger;
ProviderManager = providerManager;
2014-02-02 13:36:31 +00:00
FileSystem = fileSystem;
UserDataManager = userDataManager;
2015-06-29 01:10:45 +00:00
LibraryManager = libraryManager;
}
public async Task<ItemUpdateType> RefreshMetadata(IHasMetadata item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
{
var itemOfType = (TItemType)item;
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;
2016-08-18 05:56:10 +00:00
var requiresRefresh = false;
if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
{
// TODO: If this returns true, should we instead just change metadata refresh mode to Full?
requiresRefresh = item.RequiresRefresh();
}
2014-02-02 13:36:31 +00:00
var itemImageProvider = new ItemImageProvider(Logger, ProviderManager, ServerConfigurationManager, FileSystem);
var localImagesFailed = false;
2016-03-05 04:17:02 +00:00
var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item, refreshOptions).ToList();
2014-01-31 19:55:21 +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))
{
updateType = updateType | ItemUpdateType.ImageUpdate;
}
}
catch (Exception ex)
{
localImagesFailed = true;
2015-01-27 06:50:40 +00:00
Logger.ErrorException("Error validating images for {0}", ex, item.Path ?? item.Name ?? "Unknown name");
}
2015-06-29 01:10:45 +00:00
var metadataResult = new MetadataResult<TItemType>
{
Item = itemOfType
};
2016-04-16 02:18:40 +00:00
bool hasRefreshedMetadata = true;
bool hasRefreshedImages = true;
2016-04-08 18:32:38 +00:00
// Next run metadata providers
2014-02-02 13:36:31 +00:00
if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
{
2016-05-21 06:12:00 +00:00
var providers = GetProviders(item, refreshOptions, requiresRefresh)
2014-03-24 17:54:45 +00:00
.ToList();
2016-05-21 06:12:00 +00:00
var dateLastRefresh = item.DateLastRefreshed;
2016-04-08 18:32:38 +00:00
if (providers.Count > 0 || dateLastRefresh == default(DateTime))
{
2014-02-13 05:11:54 +00:00
if (item.BeforeMetadataRefresh())
{
updateType = updateType | ItemUpdateType.MetadataImport;
}
}
if (providers.Count > 0)
{
2015-09-19 21:25:19 +00:00
var id = itemOfType.GetLookupInfo();
2015-10-23 16:04:33 +00:00
2016-04-06 02:18:56 +00:00
if (refreshOptions.SearchResult != null)
{
ApplySearchResult(id, refreshOptions.SearchResult);
}
2015-11-21 00:12:44 +00:00
//await FindIdentities(id, cancellationToken).ConfigureAwait(false);
2016-01-23 02:32:14 +00:00
id.IsAutomated = refreshOptions.IsAutomated;
2015-06-29 01:10:45 +00:00
var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false);
updateType = updateType | result.UpdateType;
2015-09-10 18:28:22 +00:00
if (result.Failures == 0)
{
2016-04-08 18:32:38 +00:00
hasRefreshedMetadata = true;
2015-09-10 18:28:22 +00:00
}
else
{
2016-04-16 02:18:40 +00:00
hasRefreshedMetadata = false;
2015-09-10 18:28:22 +00:00
}
}
}
LibraryOptions libraryOptions = null;
// 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)
{
2016-05-21 06:12:00 +00:00
var providers = GetNonLocalImageProviders(item, allImageProviders, refreshOptions).ToList();
2014-01-29 17:23:38 +00:00
if (providers.Count > 0)
{
if (libraryOptions == null)
{
2016-09-11 21:02:32 +00:00
libraryOptions = LibraryManager.GetLibraryOptions((BaseItem)item);
}
var result = await itemImageProvider.RefreshImages(itemOfType, libraryOptions, providers, refreshOptions, config, cancellationToken).ConfigureAwait(false);
2014-01-29 05:17:58 +00:00
updateType = updateType | result.UpdateType;
2015-09-10 18:28:22 +00:00
if (result.Failures == 0)
{
2016-04-08 18:32:38 +00:00
hasRefreshedImages = true;
2015-09-10 18:28:22 +00:00
}
else
{
2016-04-16 02:18:40 +00:00
hasRefreshedImages = false;
2015-09-10 18:28:22 +00:00
}
}
}
var isFirstRefresh = item.DateLastRefreshed == default(DateTime);
2015-10-14 05:02:30 +00:00
2016-05-21 03:58:04 +00:00
var beforeSaveResult = await BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || requiresRefresh, updateType).ConfigureAwait(false);
updateType = updateType | beforeSaveResult;
2016-07-24 16:46:17 +00:00
if (item.LocationType == LocationType.FileSystem)
{
var file = refreshOptions.DirectoryService.GetFile(item.Path);
if (file != null)
{
var fileLastWriteTime = file.LastWriteTimeUtc;
if (item.EnableRefreshOnDateModifiedChange && fileLastWriteTime != item.DateModified)
2016-07-24 16:46:17 +00:00
{
Logger.Debug("Date modified for {0}. Old date {1} new date {2} Id {3}", item.Path, item.DateModified, fileLastWriteTime, item.Id);
requiresRefresh = true;
}
item.DateModified = fileLastWriteTime;
}
}
2014-02-11 21:41:01 +00:00
// Save if changes were made, or it's never been saved before
2016-05-21 03:58:04 +00:00
if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.ReplaceAllMetadata || requiresRefresh)
{
2015-01-17 18:15:09 +00:00
// If any of these properties are set then make sure the updateType is not None, just to force everything to save
if (refreshOptions.ForceSave || refreshOptions.ReplaceAllMetadata)
2014-09-13 14:10:30 +00:00
{
updateType = updateType | ItemUpdateType.MetadataDownload;
}
2016-04-08 18:32:38 +00:00
if (hasRefreshedMetadata && hasRefreshedImages)
2015-10-14 05:02:30 +00:00
{
item.DateLastRefreshed = DateTime.UtcNow;
2016-04-08 18:32:38 +00:00
}
else
{
item.DateLastRefreshed = default(DateTime);
2015-10-14 05:02:30 +00:00
}
if (libraryOptions == null)
{
2016-09-11 21:02:32 +00:00
libraryOptions = LibraryManager.GetLibraryOptions((BaseItem)item);
}
// Save to database
await SaveItem(metadataResult, libraryOptions, updateType, cancellationToken).ConfigureAwait(false);
}
2015-01-28 21:29:02 +00:00
await AfterMetadataRefresh(itemOfType, refreshOptions, cancellationToken).ConfigureAwait(false);
return updateType;
2015-01-28 21:29:02 +00:00
}
2016-04-06 02:18:56 +00:00
private void ApplySearchResult(ItemLookupInfo lookupInfo, RemoteSearchResult result)
{
lookupInfo.ProviderIds = result.ProviderIds;
lookupInfo.Name = result.Name;
lookupInfo.Year = result.ProductionYear;
}
protected async Task SaveItem(MetadataResult<TItemType> result, LibraryOptions libraryOptions, ItemUpdateType reason, CancellationToken cancellationToken)
2015-07-24 02:48:10 +00:00
{
if (result.Item.SupportsPeople && result.People != null)
{
var baseItem = result.Item as BaseItem;
await LibraryManager.UpdatePeople(baseItem, result.People.ToList());
await SavePeopleMetadata(result.People, libraryOptions, cancellationToken).ConfigureAwait(false);
2015-07-24 02:48:10 +00:00
}
await result.Item.UpdateToRepository(reason, cancellationToken).ConfigureAwait(false);
}
private async Task SavePeopleMetadata(List<PersonInfo> people, LibraryOptions libraryOptions, CancellationToken cancellationToken)
2016-01-21 18:50:43 +00:00
{
foreach (var person in people)
{
cancellationToken.ThrowIfCancellationRequested();
if (person.ProviderIds.Any() || !string.IsNullOrWhiteSpace(person.ImageUrl))
{
var updateType = ItemUpdateType.MetadataDownload;
var saveEntity = false;
var personEntity = LibraryManager.GetPerson(person.Name);
foreach (var id in person.ProviderIds)
{
if (!string.Equals(personEntity.GetProviderId(id.Key), id.Value, StringComparison.OrdinalIgnoreCase))
{
personEntity.SetProviderId(id.Key, id.Value);
saveEntity = true;
}
}
if (!string.IsNullOrWhiteSpace(person.ImageUrl) && !personEntity.HasImage(ImageType.Primary))
{
await AddPersonImage(personEntity, libraryOptions, person.ImageUrl, cancellationToken).ConfigureAwait(false);
2016-01-21 18:50:43 +00:00
saveEntity = true;
updateType = updateType | ItemUpdateType.ImageUpdate;
}
if (saveEntity)
{
await personEntity.UpdateToRepository(updateType, cancellationToken).ConfigureAwait(false);
}
}
}
}
private async Task AddPersonImage(Person personEntity, LibraryOptions libraryOptions, string imageUrl, CancellationToken cancellationToken)
2016-02-26 04:09:42 +00:00
{
if (libraryOptions.DownloadImagesInAdvance)
2016-02-26 04:09:42 +00:00
{
2016-03-05 04:17:02 +00:00
try
{
await ProviderManager.SaveImage(personEntity, imageUrl, null, ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
return;
}
catch (Exception ex)
2016-02-26 04:09:42 +00:00
{
2016-03-05 04:17:02 +00:00
Logger.ErrorException("Error in AddPersonImage", ex);
}
2016-02-26 04:09:42 +00:00
}
2016-03-05 04:17:02 +00:00
personEntity.SetImage(new ItemImageInfo
{
Path = imageUrl,
Type = ImageType.Primary,
IsPlaceholder = true
}, 0);
2016-02-26 04:09:42 +00:00
}
2015-01-28 21:29:02 +00:00
private readonly Task _cachedTask = Task.FromResult(true);
protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
{
item.AfterMetadataRefresh();
return _cachedTask;
}
2015-10-14 05:02:30 +00:00
2014-02-03 05:35:43 +00:00
/// <summary>
/// Befores the save.
2014-02-03 05:35:43 +00:00
/// </summary>
/// <param name="item">The item.</param>
2015-01-17 18:15:09 +00:00
/// <param name="isFullRefresh">if set to <c>true</c> [is full refresh].</param>
/// <param name="currentUpdateType">Type of the current update.</param>
2014-02-03 05:35:43 +00:00
/// <returns>ItemUpdateType.</returns>
2016-05-09 03:13:38 +00:00
protected virtual async Task<ItemUpdateType> BeforeSave(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
{
var updateType = ItemUpdateType.None;
updateType |= SaveCumulativeRunTimeTicks(item, isFullRefresh, currentUpdateType);
updateType |= SaveDateLastMediaAdded(item, isFullRefresh, currentUpdateType);
var presentationUniqueKey = item.CreatePresentationUniqueKey();
if (!string.Equals(item.PresentationUniqueKey, presentationUniqueKey, StringComparison.Ordinal))
{
item.PresentationUniqueKey = presentationUniqueKey;
updateType |= ItemUpdateType.MetadataImport;
}
2016-08-26 17:24:04 +00:00
var inheritedParentalRatingValue = item.GetInheritedParentalRatingValue() ?? 0;
if (inheritedParentalRatingValue != item.InheritedParentalRatingValue)
{
item.InheritedParentalRatingValue = inheritedParentalRatingValue;
updateType |= ItemUpdateType.MetadataImport;
}
2016-09-22 06:57:31 +00:00
var inheritedTags = item.GetInheritedTags();
if (!inheritedTags.SequenceEqual(item.InheritedTags, StringComparer.Ordinal))
{
item.InheritedTags = inheritedTags;
updateType |= ItemUpdateType.MetadataImport;
}
2016-05-09 03:13:38 +00:00
return updateType;
}
private ItemUpdateType SaveCumulativeRunTimeTicks(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
{
var updateType = ItemUpdateType.None;
if (isFullRefresh || currentUpdateType > ItemUpdateType.None)
{
var folder = item as Folder;
if (folder != null && folder.SupportsCumulativeRunTimeTicks)
{
2016-05-09 04:56:41 +00:00
var items = folder.GetRecursiveChildren(i => !i.IsFolder).ToList();
var ticks = items.Select(i => i.RunTimeTicks ?? 0).Sum();
2016-05-09 03:13:38 +00:00
if (!folder.RunTimeTicks.HasValue || folder.RunTimeTicks.Value != ticks)
{
folder.RunTimeTicks = ticks;
updateType = ItemUpdateType.MetadataEdit;
}
}
}
return updateType;
}
private ItemUpdateType SaveDateLastMediaAdded(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
2014-02-03 05:35:43 +00:00
{
2016-05-09 03:13:38 +00:00
var updateType = ItemUpdateType.None;
2016-10-17 16:35:29 +00:00
var folder = item as Folder;
if (folder != null && folder.SupportsDateLastMediaAdded)
2016-05-09 03:13:38 +00:00
{
2016-10-17 16:35:29 +00:00
var items = folder.GetRecursiveChildren(i => !i.IsFolder).Select(i => i.DateCreated).ToList();
var date = items.Count == 0 ? (DateTime?)null : items.Max();
2016-05-09 03:13:38 +00:00
2016-10-17 16:35:29 +00:00
if ((!folder.DateLastMediaAdded.HasValue && date.HasValue) || folder.DateLastMediaAdded != date)
{
folder.DateLastMediaAdded = date;
updateType = ItemUpdateType.MetadataImport;
2016-05-09 03:13:38 +00:00
}
}
return updateType;
2014-02-03 05:35:43 +00:00
}
/// <summary>
/// Gets the providers.
/// </summary>
/// <returns>IEnumerable{`0}.</returns>
2016-05-21 06:12:00 +00:00
protected IEnumerable<IMetadataProvider> GetProviders(IHasMetadata item, MetadataRefreshOptions options, bool requiresRefresh)
{
// Get providers to refresh
2014-02-01 06:18:16 +00:00
var providers = ((ProviderManager)ProviderManager).GetMetadataProviders<TItemType>(item).ToList();
2016-05-21 06:12:00 +00:00
var dateLastRefresh = item.DateLastRefreshed;
2016-04-08 18:32:38 +00:00
// Run all if either of these flags are true
2016-05-21 03:58:04 +00:00
var runAllProviders = options.ReplaceAllMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || dateLastRefresh == default(DateTime) || requiresRefresh;
if (!runAllProviders)
{
2014-09-11 01:57:11 +00:00
var providersWithChanges = providers
.Where(i =>
{
var hasFileChangeMonitor = i as IHasItemChangeMonitor;
if (hasFileChangeMonitor != null)
{
2016-04-08 18:32:38 +00:00
return HasChanged(item, hasFileChangeMonitor, options.DirectoryService);
2014-09-11 01:57:11 +00:00
}
return false;
})
.ToList();
2014-02-10 18:39:41 +00:00
if (providersWithChanges.Count == 0)
{
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();
}
}
return providers;
}
2016-05-21 06:12:00 +00:00
protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(IHasMetadata item, IEnumerable<IImageProvider> allImageProviders, 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
2016-05-21 06:12:00 +00:00
var dateLastImageRefresh = item.DateLastRefreshed;
2016-04-08 18:32:38 +00:00
2014-01-29 17:23:38 +00:00
// Run all if either of these flags are true
2016-04-08 18:32:38 +00:00
var runAllProviders = options.ImageRefreshMode == ImageRefreshMode.FullRefresh || dateLastImageRefresh == default(DateTime);
2014-01-29 17:23:38 +00:00
if (!runAllProviders)
{
2014-09-11 01:57:11 +00:00
providers = providers
.Where(i =>
{
var hasFileChangeMonitor = i as IHasItemChangeMonitor;
if (hasFileChangeMonitor != null)
{
2016-04-08 18:32:38 +00:00
return HasChanged(item, hasFileChangeMonitor, options.DirectoryService);
2014-09-11 01:57:11 +00:00
}
2014-09-11 01:57:11 +00:00
return false;
})
2014-01-29 17:23:38 +00:00
.ToList();
}
return providers;
}
public bool CanRefresh(IHasMetadata item)
{
return item is TItemType;
}
2015-06-29 01:10:45 +00:00
protected virtual async Task<RefreshResult> RefreshWithProviders(MetadataResult<TItemType> metadata,
2015-01-28 21:29:02 +00:00
TIdType id,
MetadataRefreshOptions options,
List<IMetadataProvider> providers,
ItemImageProvider imageService,
2014-10-25 18:32:58 +00:00
CancellationToken cancellationToken)
{
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()
};
2015-06-29 01:10:45 +00:00
var item = metadata.Item;
var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList();
2014-09-28 20:49:43 +00:00
var logName = item.LocationType == LocationType.Remote ? item.Name ?? item.Path : item.Path ?? item.Name;
foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider))
{
2014-09-28 20:49:43 +00:00
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
}
2015-06-29 01:10:45 +00:00
var temp = new MetadataResult<TItemType>
{
Item = CreateNew()
};
temp.Item.Path = item.Path;
2015-06-29 01:10:45 +00:00
var userDataList = new List<UserItemData>();
// If replacing all metadata, run internet providers first
if (options.ReplaceAllMetadata)
{
2015-04-24 20:06:37 +00:00
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
.ConfigureAwait(false);
2014-10-28 23:17:55 +00:00
refreshResult.UpdateType = refreshResult.UpdateType | remoteResult.UpdateType;
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
2015-10-05 03:24:24 +00:00
refreshResult.Failures += remoteResult.Failures;
}
var hasLocalMetadata = false;
2015-04-24 20:06:37 +00:00
foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>().ToList())
{
var providerName = provider.GetType().Name;
2014-09-28 20:49:43 +00:00
Logger.Debug("Running {0} for {1}", providerName, logName);
2015-05-15 15:46:20 +00:00
var itemInfo = new ItemInfo(item);
try
{
2014-10-25 18:32:58 +00:00
var localItem = await provider.GetMetadata(itemInfo, options.DirectoryService, cancellationToken).ConfigureAwait(false);
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
}
2015-08-02 17:31:08 +00:00
if (localItem.UserDataList != null)
{
userDataList.AddRange(localItem.UserDataList);
}
2015-06-29 01:10:45 +00:00
MergeData(localItem, temp, new List<MetadataFields>(), !options.ReplaceAllMetadata, true);
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataImport;
// Only one local provider allowed per item
2016-10-03 00:43:28 +00:00
if (item.IsLocked || IsFullLocalMetadata(localItem.Item))
2015-02-25 18:11:49 +00:00
{
hasLocalMetadata = true;
}
break;
}
2014-09-28 20:49:43 +00:00
Logger.Debug("{0} returned no metadata for {1}", providerName, logName);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
Logger.ErrorException("Error in {0}", ex, provider.Name);
// If a local provider fails, consider that a failure
refreshResult.ErrorMessage = ex.Message;
}
}
2014-02-08 20:02:35 +00:00
// Local metadata is king - if any is found don't run remote providers
2016-08-14 21:29:35 +00:00
if (!options.ReplaceAllMetadata && (!hasLocalMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || !item.StopRefreshIfLocalMetadataFound))
{
2015-04-24 20:06:37 +00:00
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
.ConfigureAwait(false);
refreshResult.UpdateType = refreshResult.UpdateType | remoteResult.UpdateType;
2015-10-05 03:24:24 +00:00
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
refreshResult.Failures += remoteResult.Failures;
}
2015-03-22 23:24:35 +00:00
if (providers.Any(i => !(i is ICustomMetadataProvider)))
2014-10-28 23:17:55 +00:00
{
2015-03-22 23:24:35 +00:00
if (refreshResult.UpdateType > ItemUpdateType.None)
{
2015-04-24 20:06:37 +00:00
// If no local metadata, take data from item itself
if (!hasLocalMetadata)
{
// TODO: If the new metadata from above has some blank data, this can cause old data to get filled into those empty fields
2015-06-29 01:10:45 +00:00
MergeData(metadata, temp, new List<MetadataFields>(), false, true);
2015-04-24 20:06:37 +00:00
}
2015-06-29 01:10:45 +00:00
MergeData(temp, metadata, item.LockedFields, true, true);
2015-03-22 23:24:35 +00:00
}
2014-02-01 06:18:16 +00:00
}
2015-09-29 17:35:23 +00:00
//var isUnidentified = failedProviderCount > 0 && successfulProviderCount == 0;
foreach (var provider in customProviders.Where(i => !(i is IPreRefreshProvider)))
2014-02-02 13:36:31 +00:00
{
2014-09-28 20:49:43 +00:00
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
2014-02-02 13:36:31 +00:00
}
await ImportUserData(item, userDataList, cancellationToken).ConfigureAwait(false);
return refreshResult;
}
2015-02-25 18:11:49 +00:00
protected virtual bool IsFullLocalMetadata(TItemType item)
{
2015-04-19 00:05:36 +00:00
if (string.IsNullOrWhiteSpace(item.Name))
{
return false;
}
2015-09-10 18:28:22 +00:00
2015-02-25 18:11:49 +00:00
return true;
}
private async Task ImportUserData(TItemType item, List<UserItemData> userDataList, CancellationToken cancellationToken)
{
var hasUserData = item as IHasUserData;
if (hasUserData != null)
{
foreach (var userData in userDataList)
{
await UserDataManager.SaveUserData(userData.UserId, hasUserData, userData, UserDataSaveReason.Import, cancellationToken)
.ConfigureAwait(false);
}
}
}
2014-09-28 20:49:43 +00:00
private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, string logName, MetadataRefreshOptions options, RefreshResult refreshResult, CancellationToken cancellationToken)
{
2014-09-28 20:49:43 +00:00
Logger.Debug("Running {0} for {1}", provider.GetType().Name, logName);
try
{
2014-06-09 19:16:14 +00:00
refreshResult.UpdateType = refreshResult.UpdateType | await provider.FetchAsync(item, options, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
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
2015-06-29 01:10:45 +00:00
private async Task<RefreshResult> ExecuteRemoteProviders(MetadataResult<TItemType> temp, string logName, TIdType id, IEnumerable<IRemoteMetadataProvider<TItemType, TIdType>> providers, CancellationToken cancellationToken)
{
var refreshResult = new RefreshResult();
var results = new List<MetadataResult<TItemType>>();
foreach (var provider in providers)
{
var providerName = provider.GetType().Name;
2014-09-28 20:49:43 +00:00
Logger.Debug("Running {0} for {1}", providerName, logName);
if (id != null)
2014-02-08 20:02:35 +00:00
{
2015-06-29 01:10:45 +00:00
MergeNewData(temp.Item, id);
2014-02-08 20:02:35 +00:00
}
2014-02-07 03:10:13 +00:00
try
{
var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false);
if (result.HasMetadata)
{
results.Add(result);
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataDownload;
}
else
{
2014-09-28 20:49:43 +00:00
Logger.Debug("{0} returned no metadata for {1}", providerName, logName);
}
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
refreshResult.Failures++;
refreshResult.ErrorMessage = ex.Message;
Logger.ErrorException("Error in {0}", ex, provider.Name);
}
}
var orderedResults = new List<MetadataResult<TItemType>>();
2016-09-22 22:04:48 +00:00
var preferredLanguage = NormalizeLanguage(id.MetadataLanguage);
2016-09-22 22:04:48 +00:00
// prioritize results with matching ResultLanguage
foreach (var result in results)
{
2016-09-22 22:04:48 +00:00
if (!result.QueriedById)
{
2016-09-22 22:04:48 +00:00
break;
}
2016-09-22 22:04:48 +00:00
if (string.Equals(NormalizeLanguage(result.ResultLanguage), preferredLanguage, StringComparison.OrdinalIgnoreCase) && result.QueriedById)
{
2016-09-22 22:04:48 +00:00
orderedResults.Add(result);
}
}
// add all other results
foreach (var result in results)
{
if (!orderedResults.Contains(result))
{
orderedResults.Add(result);
}
}
foreach (var result in results)
{
MergeData(result, temp, new List<MetadataFields>(), false, false);
}
return refreshResult;
}
2016-09-22 22:04:48 +00:00
private string NormalizeLanguage(string language)
{
if (string.IsNullOrWhiteSpace(language))
{
2016-09-23 06:57:55 +00:00
return "en";
2016-09-22 22:04:48 +00:00
}
return language;
}
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
}
}
2015-06-29 01:10:45 +00:00
protected abstract void MergeData(MetadataResult<TItemType> source,
MetadataResult<TItemType> target,
2015-01-28 21:29:02 +00:00
List<MetadataFields> lockedFields,
bool replaceData,
2014-10-28 23:17:55 +00:00
bool mergeMetadataSettings);
public virtual int Order
{
get
{
return 0;
}
}
2014-02-14 04:00:13 +00:00
2016-04-08 18:32:38 +00:00
private bool HasChanged(IHasMetadata item, IHasItemChangeMonitor changeMonitor, IDirectoryService directoryService)
2014-09-11 01:57:11 +00:00
{
try
{
2016-04-08 18:32:38 +00:00
var hasChanged = changeMonitor.HasChanged(item, directoryService);
2015-11-13 02:49:32 +00:00
//if (hasChanged)
//{
// Logger.Debug("{0} reports change to {1}", changeMonitor.GetType().Name, item.Path ?? item.Name);
//}
return hasChanged;
2014-09-11 01:57:11 +00:00
}
catch (Exception ex)
{
Logger.ErrorException("Error in {0}.HasChanged", ex, changeMonitor.GetType().Name);
return false;
}
}
}
public class RefreshResult
{
public ItemUpdateType UpdateType { get; set; }
public string ErrorMessage { get; set; }
2014-01-29 05:17:58 +00:00
public List<Guid> Providers { get; set; }
public int Failures { get; set; }
}
}