2014-09-28 17:27:26 +02:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-01-29 06:17:58 +01:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
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;
|
2015-10-04 06:23:11 +02:00
|
|
|
|
using CommonIO;
|
2015-10-15 00:35:32 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2015-10-14 07:02:30 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
|
{
|
2014-01-31 20:55:21 +01:00
|
|
|
|
public abstract class MetadataService<TItemType, TIdType> : IMetadataService
|
2014-02-07 04:10:13 +01:00
|
|
|
|
where TItemType : IHasMetadata, IHasLookupInfo<TIdType>, new()
|
|
|
|
|
where TIdType : ItemLookupInfo, new()
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
protected readonly IServerConfigurationManager ServerConfigurationManager;
|
|
|
|
|
protected readonly ILogger Logger;
|
|
|
|
|
protected readonly IProviderManager ProviderManager;
|
2014-02-02 14:36:31 +01:00
|
|
|
|
protected readonly IProviderRepository ProviderRepo;
|
|
|
|
|
protected readonly IFileSystem FileSystem;
|
2014-09-27 04:28:13 +02:00
|
|
|
|
protected readonly IUserDataManager UserDataManager;
|
2015-06-29 03:10:45 +02:00
|
|
|
|
protected readonly ILibraryManager LibraryManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
protected MetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IProviderRepository providerRepo, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
ServerConfigurationManager = serverConfigurationManager;
|
|
|
|
|
Logger = logger;
|
|
|
|
|
ProviderManager = providerManager;
|
2014-02-02 14:36:31 +01:00
|
|
|
|
ProviderRepo = providerRepo;
|
|
|
|
|
FileSystem = fileSystem;
|
2014-09-27 04:28:13 +02:00
|
|
|
|
UserDataManager = userDataManager;
|
2015-06-29 03:10:45 +02:00
|
|
|
|
LibraryManager = libraryManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the provider result.
|
|
|
|
|
/// </summary>
|
2014-02-08 21:02:35 +01:00
|
|
|
|
/// <param name="item">The item.</param>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <param name="result">The result.</param>
|
2014-09-11 03:57:11 +02:00
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <returns>Task.</returns>
|
2014-09-11 03:57:11 +02:00
|
|
|
|
protected Task SaveProviderResult(TItemType item, MetadataStatus result, IDirectoryService directoryService)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-02-08 21:02:35 +01:00
|
|
|
|
result.ItemId = item.Id;
|
|
|
|
|
|
2014-09-11 03:57:11 +02:00
|
|
|
|
//var locationType = item.LocationType;
|
|
|
|
|
|
|
|
|
|
//if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
|
|
|
|
|
//{
|
|
|
|
|
// if (!string.IsNullOrWhiteSpace(item.Path))
|
|
|
|
|
// {
|
|
|
|
|
// var file = directoryService.GetFile(item.Path);
|
|
|
|
|
|
|
|
|
|
// if ((file.Attributes & FileAttributes.Directory) != FileAttributes.Directory && file.Exists)
|
|
|
|
|
// {
|
|
|
|
|
// result.ItemDateModified = FileSystem.GetLastWriteTimeUtc(file);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
result.ItemDateModified = item.DateModified;
|
|
|
|
|
|
2014-02-02 14:36:31 +01:00
|
|
|
|
return ProviderRepo.SaveMetadataStatus(result, CancellationToken.None);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the last result.
|
|
|
|
|
/// </summary>
|
2014-03-16 17:15:10 +01:00
|
|
|
|
/// <param name="item">The item.</param>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <returns>ProviderResult.</returns>
|
2014-03-16 17:15:10 +01:00
|
|
|
|
protected MetadataStatus GetLastResult(IHasMetadata item)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2015-10-14 07:02:30 +02:00
|
|
|
|
if (GetLastRefreshDate(item) == default(DateTime))
|
2014-03-16 17:15:10 +01:00
|
|
|
|
{
|
|
|
|
|
return new MetadataStatus { ItemId = item.Id };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ProviderRepo.GetMetadataStatus(item.Id) ?? new MetadataStatus { ItemId = item.Id };
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 03:07:07 +01:00
|
|
|
|
public async Task<ItemUpdateType> RefreshMetadata(IHasMetadata item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
var itemOfType = (TItemType)item;
|
2014-02-10 00:08:01 +01:00
|
|
|
|
var config = ProviderManager.GetMetadataOptions(item);
|
2014-02-02 14:36:31 +01:00
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
var updateType = ItemUpdateType.None;
|
2014-03-16 17:15:10 +01:00
|
|
|
|
var refreshResult = GetLastResult(item);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-02-02 14:36:31 +01:00
|
|
|
|
var itemImageProvider = new ItemImageProvider(Logger, ProviderManager, ServerConfigurationManager, FileSystem);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
var localImagesFailed = false;
|
|
|
|
|
|
2016-03-05 05:17:02 +01:00
|
|
|
|
var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item, refreshOptions).ToList();
|
2014-01-31 20:55:21 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// Start by validating images
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Always validate images and check for new locally stored ones.
|
2014-02-08 23:38:02 +01:00
|
|
|
|
if (itemImageProvider.ValidateImages(item, allImageProviders.OfType<ILocalImageProvider>(), refreshOptions.DirectoryService))
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
updateType = updateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
localImagesFailed = true;
|
2015-01-27 07:50:40 +01:00
|
|
|
|
Logger.ErrorException("Error validating images for {0}", ex, item.Path ?? item.Name ?? "Unknown name");
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var metadataResult = new MetadataResult<TItemType>
|
|
|
|
|
{
|
|
|
|
|
Item = itemOfType
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// Next run metadata providers
|
2014-02-02 14:36:31 +01:00
|
|
|
|
if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-09-11 03:57:11 +02:00
|
|
|
|
var providers = GetProviders(item, refreshResult, refreshOptions)
|
2014-03-24 18:54:45 +01:00
|
|
|
|
.ToList();
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
if (providers.Count > 0 || !refreshResult.DateLastMetadataRefresh.HasValue)
|
|
|
|
|
{
|
2014-02-13 06:11:54 +01:00
|
|
|
|
if (item.BeforeMetadataRefresh())
|
|
|
|
|
{
|
|
|
|
|
updateType = updateType | ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
2014-02-06 05:39:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
if (providers.Count > 0)
|
|
|
|
|
{
|
2015-09-19 23:25:19 +02:00
|
|
|
|
var id = itemOfType.GetLookupInfo();
|
2015-10-23 18:04:33 +02:00
|
|
|
|
|
2015-11-21 01:12:44 +01:00
|
|
|
|
//await FindIdentities(id, cancellationToken).ConfigureAwait(false);
|
2016-01-23 03:32:14 +01:00
|
|
|
|
id.IsAutomated = refreshOptions.IsAutomated;
|
2014-08-21 18:21:39 +02:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
updateType = updateType | result.UpdateType;
|
2015-09-10 20:28:22 +02:00
|
|
|
|
if (result.Failures == 0)
|
|
|
|
|
{
|
|
|
|
|
refreshResult.SetDateLastMetadataRefresh(DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
refreshResult.SetDateLastMetadataRefresh(null);
|
|
|
|
|
}
|
2014-08-21 18:21:39 +02:00
|
|
|
|
}
|
2014-05-06 04:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// Next run remote image providers, but only if local image providers didn't throw an exception
|
2014-02-02 14:36:31 +01:00
|
|
|
|
if (!localImagesFailed && refreshOptions.ImageRefreshMode != ImageRefreshMode.ValidationOnly)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-09-11 03:57:11 +02:00
|
|
|
|
var providers = GetNonLocalImageProviders(item, allImageProviders, refreshResult, refreshOptions).ToList();
|
2014-01-29 18:23:38 +01:00
|
|
|
|
|
|
|
|
|
if (providers.Count > 0)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-02-02 14:36:31 +01:00
|
|
|
|
var result = await itemImageProvider.RefreshImages(itemOfType, providers, refreshOptions, config, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-01-29 06:17:58 +01:00
|
|
|
|
updateType = updateType | result.UpdateType;
|
2015-09-10 20:28:22 +02:00
|
|
|
|
if (result.Failures == 0)
|
|
|
|
|
{
|
|
|
|
|
refreshResult.SetDateLastImagesRefresh(DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
refreshResult.SetDateLastImagesRefresh(null);
|
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 07:02:30 +02:00
|
|
|
|
var isFirstRefresh = GetLastRefreshDate(item) == default(DateTime);
|
|
|
|
|
|
|
|
|
|
var beforeSaveResult = await BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh, updateType).ConfigureAwait(false);
|
2015-03-11 03:49:32 +01:00
|
|
|
|
updateType = updateType | beforeSaveResult;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-02-11 22:41:01 +01:00
|
|
|
|
// Save if changes were made, or it's never been saved before
|
2015-10-14 07:02:30 +02:00
|
|
|
|
if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.ReplaceAllMetadata)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2015-01-17 19:15:09 +01: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 16:10:30 +02:00
|
|
|
|
{
|
|
|
|
|
updateType = updateType | ItemUpdateType.MetadataDownload;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 07:02:30 +02:00
|
|
|
|
if (refreshOptions.MetadataRefreshMode >= MetadataRefreshMode.Default && refreshOptions.ImageRefreshMode >= ImageRefreshMode.Default)
|
|
|
|
|
{
|
|
|
|
|
item.DateLastRefreshed = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// Save to database
|
2015-06-29 03:10:45 +02:00
|
|
|
|
await SaveItem(metadataResult, updateType, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 19:15:09 +01:00
|
|
|
|
if (updateType > ItemUpdateType.None || refreshResult.IsDirty)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-09-11 03:57:11 +02:00
|
|
|
|
await SaveProviderResult(itemOfType, refreshResult, refreshOptions.DirectoryService).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
2015-01-27 07:50:40 +01:00
|
|
|
|
|
2015-01-28 22:29:02 +01:00
|
|
|
|
await AfterMetadataRefresh(itemOfType, refreshOptions, cancellationToken).ConfigureAwait(false);
|
2015-03-11 03:07:07 +01:00
|
|
|
|
|
|
|
|
|
return updateType;
|
2015-01-28 22:29:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 18:04:33 +02:00
|
|
|
|
private async Task FindIdentities(TIdType id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ItemIdentifier<TIdType>.FindIdentities(id, ProviderManager, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error in FindIdentities", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 07:02:30 +02:00
|
|
|
|
private DateTime GetLastRefreshDate(IHasMetadata item)
|
|
|
|
|
{
|
2015-10-28 20:40:38 +01:00
|
|
|
|
if (EnableDateLastRefreshed(item))
|
2015-10-14 07:02:30 +02:00
|
|
|
|
{
|
|
|
|
|
return item.DateLastRefreshed;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 20:40:38 +01:00
|
|
|
|
return item.DateLastSaved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EnableDateLastRefreshed(IHasMetadata item)
|
|
|
|
|
{
|
2015-10-14 07:02:30 +02:00
|
|
|
|
if (ServerConfigurationManager.Configuration.EnableDateLastRefresh)
|
|
|
|
|
{
|
2015-10-28 20:40:38 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.DateLastRefreshed != default(DateTime))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2015-10-14 07:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 00:35:32 +02:00
|
|
|
|
if (item is BoxSet || (item is IItemByName && !(item is MusicArtist)))
|
2015-10-14 07:02:30 +02:00
|
|
|
|
{
|
2015-10-28 20:40:38 +01:00
|
|
|
|
return true;
|
2015-10-14 07:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 20:40:38 +01:00
|
|
|
|
return false;
|
2015-10-14 07:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-24 04:48:10 +02:00
|
|
|
|
protected async Task SaveItem(MetadataResult<TItemType> result, ItemUpdateType reason, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (result.Item.SupportsPeople && result.People != null)
|
|
|
|
|
{
|
|
|
|
|
await LibraryManager.UpdatePeople(result.Item as BaseItem, result.People.ToList());
|
2016-01-21 19:50:43 +01:00
|
|
|
|
await SavePeopleMetadata(result.People, cancellationToken).ConfigureAwait(false);
|
2015-07-24 04:48:10 +02:00
|
|
|
|
}
|
|
|
|
|
await result.Item.UpdateToRepository(reason, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 19:50:43 +01:00
|
|
|
|
private async Task SavePeopleMetadata(List<PersonInfo> people, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-02-26 05:09:42 +01:00
|
|
|
|
await AddPersonImage(personEntity, person.ImageUrl, cancellationToken).ConfigureAwait(false);
|
2016-01-21 19:50:43 +01:00
|
|
|
|
|
|
|
|
|
saveEntity = true;
|
|
|
|
|
updateType = updateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (saveEntity)
|
|
|
|
|
{
|
|
|
|
|
await personEntity.UpdateToRepository(updateType, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 05:09:42 +01:00
|
|
|
|
private async Task AddPersonImage(Person personEntity, string imageUrl, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (ServerConfigurationManager.Configuration.DownloadImagesInAdvance)
|
|
|
|
|
{
|
2016-03-05 05:17:02 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ProviderManager.SaveImage(personEntity, imageUrl, null, ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2016-02-26 05:09:42 +01:00
|
|
|
|
{
|
2016-03-05 05:17:02 +01:00
|
|
|
|
Logger.ErrorException("Error in AddPersonImage", ex);
|
|
|
|
|
}
|
2016-02-26 05:09:42 +01:00
|
|
|
|
}
|
2016-03-05 05:17:02 +01:00
|
|
|
|
|
|
|
|
|
personEntity.SetImage(new ItemImageInfo
|
|
|
|
|
{
|
|
|
|
|
Path = imageUrl,
|
|
|
|
|
Type = ImageType.Primary,
|
|
|
|
|
IsPlaceholder = true
|
|
|
|
|
}, 0);
|
2016-02-26 05:09:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 22:29:02 +01:00
|
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
|
|
|
|
protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
item.AfterMetadataRefresh();
|
|
|
|
|
return _cachedTask;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
2015-10-14 07:02:30 +02:00
|
|
|
|
|
2015-01-17 19:15:09 +01:00
|
|
|
|
private readonly Task<ItemUpdateType> _cachedResult = Task.FromResult(ItemUpdateType.None);
|
2014-02-03 06:35:43 +01:00
|
|
|
|
/// <summary>
|
2014-02-04 05:04:19 +01:00
|
|
|
|
/// Befores the save.
|
2014-02-03 06:35:43 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2015-01-17 19:15:09 +01: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 06:35:43 +01:00
|
|
|
|
/// <returns>ItemUpdateType.</returns>
|
2015-01-17 19:15:09 +01:00
|
|
|
|
protected virtual Task<ItemUpdateType> BeforeSave(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
|
2014-02-03 06:35:43 +01:00
|
|
|
|
{
|
2015-01-17 19:15:09 +01:00
|
|
|
|
return _cachedResult;
|
2014-02-03 06:35:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the providers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2014-09-11 03:57:11 +02:00
|
|
|
|
/// <param name="status">The status.</param>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
|
/// <returns>IEnumerable{`0}.</returns>
|
2014-09-11 03:57:11 +02:00
|
|
|
|
protected IEnumerable<IMetadataProvider> GetProviders(IHasMetadata item, MetadataStatus status, MetadataRefreshOptions options)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
// Get providers to refresh
|
2014-02-01 07:18:16 +01:00
|
|
|
|
var providers = ((ProviderManager)ProviderManager).GetMetadataProviders<TItemType>(item).ToList();
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
// Run all if either of these flags are true
|
2014-09-11 03:57:11 +02:00
|
|
|
|
var runAllProviders = options.ReplaceAllMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || !status.DateLastMetadataRefresh.HasValue;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentItem = item;
|
|
|
|
|
|
2014-09-11 03:57:11 +02:00
|
|
|
|
var providersWithChanges = providers
|
|
|
|
|
.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
var hasChangeMonitor = i as IHasChangeMonitor;
|
|
|
|
|
if (hasChangeMonitor != null)
|
|
|
|
|
{
|
|
|
|
|
return HasChanged(item, hasChangeMonitor, currentItem.DateLastSaved, options.DirectoryService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasFileChangeMonitor = i as IHasItemChangeMonitor;
|
|
|
|
|
if (hasFileChangeMonitor != null)
|
|
|
|
|
{
|
|
|
|
|
return HasChanged(item, hasFileChangeMonitor, status, options.DirectoryService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
})
|
2014-01-28 19:37:01 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
if (providersWithChanges.Count == 0)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-02-10 19:39:41 +01: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 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-11 03:57:11 +02:00
|
|
|
|
protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(IHasMetadata item, IEnumerable<IImageProvider> allImageProviders, MetadataStatus status, ImageRefreshOptions options)
|
2014-01-29 18:23:38 +01:00
|
|
|
|
{
|
|
|
|
|
// Get providers to refresh
|
2014-01-31 20:55:21 +01:00
|
|
|
|
var providers = allImageProviders.Where(i => !(i is ILocalImageProvider)).ToList();
|
2014-01-29 18:23:38 +01:00
|
|
|
|
|
|
|
|
|
// Run all if either of these flags are true
|
2014-09-11 03:57:11 +02:00
|
|
|
|
var runAllProviders = options.ImageRefreshMode == ImageRefreshMode.FullRefresh || !status.DateLastImagesRefresh.HasValue;
|
2014-01-29 18:23:38 +01:00
|
|
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
|
{
|
2014-09-11 03:57:11 +02:00
|
|
|
|
providers = providers
|
|
|
|
|
.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
var hasChangeMonitor = i as IHasChangeMonitor;
|
|
|
|
|
if (hasChangeMonitor != null)
|
|
|
|
|
{
|
|
|
|
|
return HasChanged(item, hasChangeMonitor, status.DateLastImagesRefresh.Value, options.DirectoryService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasFileChangeMonitor = i as IHasItemChangeMonitor;
|
|
|
|
|
if (hasFileChangeMonitor != null)
|
|
|
|
|
{
|
|
|
|
|
return HasChanged(item, hasFileChangeMonitor, status, options.DirectoryService);
|
|
|
|
|
}
|
2014-09-27 04:28:13 +02:00
|
|
|
|
|
2014-09-11 03:57:11 +02:00
|
|
|
|
return false;
|
|
|
|
|
})
|
2014-01-29 18:23:38 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
public bool CanRefresh(IHasMetadata item)
|
|
|
|
|
{
|
|
|
|
|
return item is TItemType;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
protected virtual async Task<RefreshResult> RefreshWithProviders(MetadataResult<TItemType> metadata,
|
2015-01-28 22:29:02 +01:00
|
|
|
|
TIdType id,
|
|
|
|
|
MetadataRefreshOptions options,
|
|
|
|
|
List<IMetadataProvider> providers,
|
|
|
|
|
ItemImageProvider imageService,
|
2014-10-25 20:32:58 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-01-29 06:17:58 +01:00
|
|
|
|
var refreshResult = new RefreshResult
|
|
|
|
|
{
|
2014-02-10 19:39:41 +01:00
|
|
|
|
UpdateType = ItemUpdateType.None,
|
2014-01-29 06:17:58 +01:00
|
|
|
|
Providers = providers.Select(i => i.GetType().FullName.GetMD5()).ToList()
|
|
|
|
|
};
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var item = metadata.Item;
|
|
|
|
|
|
2014-03-01 05:13:22 +01:00
|
|
|
|
var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList();
|
2014-09-28 22:49:43 +02:00
|
|
|
|
var logName = item.LocationType == LocationType.Remote ? item.Name ?? item.Path : item.Path ?? item.Name;
|
2014-03-01 05:13:22 +01:00
|
|
|
|
|
|
|
|
|
foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider))
|
|
|
|
|
{
|
2014-09-28 22:49:43 +02:00
|
|
|
|
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-03-01 05:13:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var temp = new MetadataResult<TItemType>
|
|
|
|
|
{
|
|
|
|
|
Item = CreateNew()
|
|
|
|
|
};
|
|
|
|
|
temp.Item.Path = item.Path;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var userDataList = new List<UserItemData>();
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// If replacing all metadata, run internet providers first
|
|
|
|
|
if (options.ReplaceAllMetadata)
|
|
|
|
|
{
|
2015-04-24 22:06:37 +02:00
|
|
|
|
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
|
2014-07-09 04:53:04 +02:00
|
|
|
|
.ConfigureAwait(false);
|
2014-10-29 00:17:55 +01:00
|
|
|
|
|
2014-07-09 04:53:04 +02:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | remoteResult.UpdateType;
|
|
|
|
|
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
|
2015-10-05 05:24:24 +02:00
|
|
|
|
refreshResult.Failures += remoteResult.Failures;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasLocalMetadata = false;
|
|
|
|
|
|
2015-04-24 22:06:37 +02:00
|
|
|
|
foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>().ToList())
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-02-17 22:35:08 +01:00
|
|
|
|
var providerName = provider.GetType().Name;
|
2014-09-28 22:49:43 +02:00
|
|
|
|
Logger.Debug("Running {0} for {1}", providerName, logName);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2015-05-15 17:46:20 +02:00
|
|
|
|
var itemInfo = new ItemInfo(item);
|
2014-02-06 05:39:16 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-10-25 20:32:58 +02:00
|
|
|
|
var localItem = await provider.GetMetadata(itemInfo, options.DirectoryService, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
if (localItem.HasMetadata)
|
|
|
|
|
{
|
2014-02-09 05:52:52 +01:00
|
|
|
|
if (imageService.MergeImages(item, localItem.Images))
|
|
|
|
|
{
|
2014-02-22 22:05:00 +01:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.ImageUpdate;
|
2014-02-09 05:52:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-02 19:31:08 +02:00
|
|
|
|
if (localItem.UserDataList != null)
|
|
|
|
|
{
|
|
|
|
|
userDataList.AddRange(localItem.UserDataList);
|
|
|
|
|
}
|
2014-09-27 04:28:13 +02:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
MergeData(localItem, temp, new List<MetadataFields>(), !options.ReplaceAllMetadata, true);
|
2014-03-12 03:11:01 +01:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataImport;
|
|
|
|
|
|
|
|
|
|
// Only one local provider allowed per item
|
2015-02-25 19:11:49 +01:00
|
|
|
|
if (IsFullLocalMetadata(localItem.Item))
|
|
|
|
|
{
|
|
|
|
|
hasLocalMetadata = true;
|
|
|
|
|
}
|
2014-03-12 03:11:01 +01:00
|
|
|
|
break;
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2014-03-12 03:11:01 +01:00
|
|
|
|
|
2014-09-28 22:49:43 +02:00
|
|
|
|
Logger.Debug("{0} returned no metadata for {1}", providerName, logName);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2015-10-05 05:24:24 +02:00
|
|
|
|
refreshResult.Failures++;
|
2014-09-27 04:28:13 +02:00
|
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
2014-09-27 04:28:13 +02:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
// If a local provider fails, consider that a failure
|
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
|
2014-07-02 06:57:18 +02:00
|
|
|
|
if (options.MetadataRefreshMode != MetadataRefreshMode.FullRefresh)
|
|
|
|
|
{
|
|
|
|
|
// If the local provider fails don't continue with remote providers because the user's saved metadata could be lost
|
|
|
|
|
return refreshResult;
|
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 21:02:35 +01:00
|
|
|
|
// Local metadata is king - if any is found don't run remote providers
|
2014-02-15 17:36:09 +01:00
|
|
|
|
if (!options.ReplaceAllMetadata && (!hasLocalMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh))
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2015-04-24 22:06:37 +02:00
|
|
|
|
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
|
2014-07-09 04:53:04 +02:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | remoteResult.UpdateType;
|
2015-10-05 05:24:24 +02:00
|
|
|
|
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
|
|
|
|
|
refreshResult.Failures += remoteResult.Failures;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 00:24:35 +01:00
|
|
|
|
if (providers.Any(i => !(i is ICustomMetadataProvider)))
|
2014-10-29 00:17:55 +01:00
|
|
|
|
{
|
2015-03-23 00:24:35 +01:00
|
|
|
|
if (refreshResult.UpdateType > ItemUpdateType.None)
|
|
|
|
|
{
|
2015-04-24 22:06:37 +02: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 03:10:45 +02:00
|
|
|
|
MergeData(metadata, temp, new List<MetadataFields>(), false, true);
|
2015-04-24 22:06:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
MergeData(temp, metadata, item.LockedFields, true, true);
|
2015-03-23 00:24:35 +01:00
|
|
|
|
}
|
2014-02-01 07:18:16 +01:00
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2015-09-29 19:35:23 +02:00
|
|
|
|
//var isUnidentified = failedProviderCount > 0 && successfulProviderCount == 0;
|
2014-07-09 04:53:04 +02:00
|
|
|
|
|
2014-03-01 05:13:22 +01:00
|
|
|
|
foreach (var provider in customProviders.Where(i => !(i is IPreRefreshProvider)))
|
2014-02-02 14:36:31 +01:00
|
|
|
|
{
|
2014-09-28 22:49:43 +02:00
|
|
|
|
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-02-02 14:36:31 +01:00
|
|
|
|
}
|
2014-02-06 05:39:16 +01:00
|
|
|
|
|
2014-09-27 04:28:13 +02:00
|
|
|
|
await ImportUserData(item, userDataList, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
return refreshResult;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 19:11:49 +01:00
|
|
|
|
protected virtual bool IsFullLocalMetadata(TItemType item)
|
|
|
|
|
{
|
2015-04-19 02:05:36 +02:00
|
|
|
|
if (string.IsNullOrWhiteSpace(item.Name))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-10 20:28:22 +02:00
|
|
|
|
|
2015-02-25 19:11:49 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-27 04:28:13 +02:00
|
|
|
|
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 22:49:43 +02:00
|
|
|
|
private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, string logName, MetadataRefreshOptions options, RefreshResult refreshResult, CancellationToken cancellationToken)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
2014-09-28 22:49:43 +02:00
|
|
|
|
Logger.Debug("Running {0} for {1}", provider.GetType().Name, logName);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-09 21:16:14 +02:00
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | await provider.FetchAsync(item, options, cancellationToken).ConfigureAwait(false);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 14:36:31 +01:00
|
|
|
|
protected virtual TItemType CreateNew()
|
|
|
|
|
{
|
|
|
|
|
return new TItemType();
|
|
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
private async Task<RefreshResult> ExecuteRemoteProviders(MetadataResult<TItemType> temp, string logName, TIdType id, IEnumerable<IRemoteMetadataProvider<TItemType, TIdType>> providers, CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-07-09 04:53:04 +02:00
|
|
|
|
var refreshResult = new RefreshResult();
|
2014-03-03 06:11:03 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
foreach (var provider in providers)
|
|
|
|
|
{
|
2014-02-17 22:35:08 +01:00
|
|
|
|
var providerName = provider.GetType().Name;
|
2014-09-28 22:49:43 +02:00
|
|
|
|
Logger.Debug("Running {0} for {1}", providerName, logName);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-05-05 21:53:05 +02:00
|
|
|
|
if (id != null)
|
2014-02-08 21:02:35 +01:00
|
|
|
|
{
|
2015-06-29 03:10:45 +02:00
|
|
|
|
MergeNewData(temp.Item, id);
|
2014-02-08 21:02:35 +01:00
|
|
|
|
}
|
2014-02-07 04:10:13 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result.HasMetadata)
|
|
|
|
|
{
|
2014-09-22 23:56:54 +02:00
|
|
|
|
NormalizeRemoteResult(result.Item);
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
MergeData(result, temp, new List<MetadataFields>(), false, false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataDownload;
|
|
|
|
|
}
|
2014-02-17 22:35:08 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2014-07-09 04:53:04 +02:00
|
|
|
|
refreshResult.Failures++;
|
2014-09-28 22:49:43 +02:00
|
|
|
|
Logger.Debug("{0} returned no metadata for {1}", providerName, logName);
|
2014-02-17 22:35:08 +01:00
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-07-09 04:53:04 +02:00
|
|
|
|
refreshResult.Failures++;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
|
Logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-03 06:11:03 +01:00
|
|
|
|
|
2014-07-09 04:53:04 +02:00
|
|
|
|
return refreshResult;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
|
private void NormalizeRemoteResult(TItemType item)
|
|
|
|
|
{
|
|
|
|
|
if (!ServerConfigurationManager.Configuration.FindInternetTrailers)
|
|
|
|
|
{
|
|
|
|
|
var hasTrailers = item as IHasTrailers;
|
|
|
|
|
|
|
|
|
|
if (hasTrailers != null)
|
|
|
|
|
{
|
|
|
|
|
hasTrailers.RemoteTrailers.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 21:02:35 +01: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 06:11:54 +01:00
|
|
|
|
var key = providerId.Key;
|
|
|
|
|
|
|
|
|
|
// Don't replace existing Id's.
|
|
|
|
|
if (!lookupInfo.ProviderIds.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
lookupInfo.ProviderIds[key] = providerId.Value;
|
|
|
|
|
}
|
2014-02-08 21:02:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
protected abstract void MergeData(MetadataResult<TItemType> source,
|
|
|
|
|
MetadataResult<TItemType> target,
|
2015-01-28 22:29:02 +01:00
|
|
|
|
List<MetadataFields> lockedFields,
|
|
|
|
|
bool replaceData,
|
2014-10-29 00:17:55 +01:00
|
|
|
|
bool mergeMetadataSettings);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
public virtual int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-14 05:00:13 +01:00
|
|
|
|
|
2014-09-11 03:57:11 +02:00
|
|
|
|
private bool HasChanged(IHasMetadata item, IHasItemChangeMonitor changeMonitor, MetadataStatus status, IDirectoryService directoryService)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-11-13 03:49:32 +01:00
|
|
|
|
var hasChanged = changeMonitor.HasChanged(item, status, directoryService);
|
|
|
|
|
|
|
|
|
|
//if (hasChanged)
|
|
|
|
|
//{
|
|
|
|
|
// Logger.Debug("{0} reports change to {1}", changeMonitor.GetType().Name, item.Path ?? item.Name);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return hasChanged;
|
2014-09-11 03:57:11 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error in {0}.HasChanged", ex, changeMonitor.GetType().Name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-14 05:00:13 +01:00
|
|
|
|
private bool HasChanged(IHasMetadata item, IHasChangeMonitor changeMonitor, DateTime date, IDirectoryService directoryService)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-11-13 03:49:32 +01:00
|
|
|
|
var hasChanged = changeMonitor.HasChanged(item, directoryService, date);
|
|
|
|
|
|
|
|
|
|
//if (hasChanged)
|
|
|
|
|
//{
|
|
|
|
|
// Logger.Debug("{0} reports change to {1} since {2}", changeMonitor.GetType().Name,
|
|
|
|
|
// item.Path ?? item.Name, date);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return hasChanged;
|
2014-02-14 05:00:13 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error in {0}.HasChanged", ex, changeMonitor.GetType().Name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RefreshResult
|
|
|
|
|
{
|
|
|
|
|
public ItemUpdateType UpdateType { get; set; }
|
|
|
|
|
public string ErrorMessage { get; set; }
|
2014-01-29 06:17:58 +01:00
|
|
|
|
public List<Guid> Providers { get; set; }
|
2014-07-09 04:53:04 +02:00
|
|
|
|
public int Failures { get; set; }
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|