| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Net.Http; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Controller.Configuration; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 15 | | using MediaBrowser.Controller.IO; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Controller.Persistence; |
| | | 18 | | using MediaBrowser.Controller.Providers; |
| | | 19 | | using MediaBrowser.Model.Configuration; |
| | | 20 | | using MediaBrowser.Model.Entities; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.Providers; |
| | | 23 | | using Microsoft.Extensions.Logging; |
| | | 24 | | |
| | | 25 | | namespace MediaBrowser.Providers.Manager |
| | | 26 | | { |
| | | 27 | | public abstract class MetadataService<TItemType, TIdType> : IMetadataService |
| | | 28 | | where TItemType : BaseItem, IHasLookupInfo<TIdType>, new() |
| | | 29 | | where TIdType : ItemLookupInfo, new() |
| | | 30 | | { |
| | | 31 | | protected MetadataService( |
| | | 32 | | IServerConfigurationManager serverConfigurationManager, |
| | | 33 | | ILogger<MetadataService<TItemType, TIdType>> logger, |
| | | 34 | | IProviderManager providerManager, |
| | | 35 | | IFileSystem fileSystem, |
| | | 36 | | ILibraryManager libraryManager, |
| | | 37 | | IExternalDataManager externalDataManager, |
| | | 38 | | IItemRepository itemRepository) |
| | | 39 | | { |
| | 546 | 40 | | ServerConfigurationManager = serverConfigurationManager; |
| | 546 | 41 | | Logger = logger; |
| | 546 | 42 | | ProviderManager = providerManager; |
| | 546 | 43 | | FileSystem = fileSystem; |
| | 546 | 44 | | LibraryManager = libraryManager; |
| | 546 | 45 | | ExternalDataManager = externalDataManager; |
| | 546 | 46 | | ItemRepository = itemRepository; |
| | 546 | 47 | | ImageProvider = new ItemImageProvider(Logger, ProviderManager, FileSystem); |
| | 546 | 48 | | } |
| | | 49 | | |
| | | 50 | | protected ItemImageProvider ImageProvider { get; } |
| | | 51 | | |
| | | 52 | | protected IServerConfigurationManager ServerConfigurationManager { get; } |
| | | 53 | | |
| | | 54 | | protected ILogger<MetadataService<TItemType, TIdType>> Logger { get; } |
| | | 55 | | |
| | | 56 | | protected IProviderManager ProviderManager { get; } |
| | | 57 | | |
| | | 58 | | protected IFileSystem FileSystem { get; } |
| | | 59 | | |
| | | 60 | | protected ILibraryManager LibraryManager { get; } |
| | | 61 | | |
| | | 62 | | protected IExternalDataManager ExternalDataManager { get; } |
| | | 63 | | |
| | | 64 | | protected IItemRepository ItemRepository { get; } |
| | | 65 | | |
| | 34 | 66 | | protected virtual bool EnableUpdatingPremiereDateFromChildren => false; |
| | | 67 | | |
| | 34 | 68 | | protected virtual bool EnableUpdatingGenresFromChildren => false; |
| | | 69 | | |
| | 34 | 70 | | protected virtual bool EnableUpdatingStudiosFromChildren => false; |
| | | 71 | | |
| | 34 | 72 | | protected virtual bool EnableUpdatingOfficialRatingFromChildren => false; |
| | | 73 | | |
| | 504 | 74 | | public virtual int Order => 0; |
| | | 75 | | |
| | | 76 | | private FileSystemMetadata TryGetFileSystemMetadata(string path, IDirectoryService directoryService) |
| | | 77 | | { |
| | | 78 | | try |
| | | 79 | | { |
| | 68 | 80 | | return directoryService.GetFileSystemEntry(path); |
| | | 81 | | } |
| | 0 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 0 | 84 | | Logger.LogError(ex, "Error getting file {Path}", path); |
| | 0 | 85 | | return null; |
| | | 86 | | } |
| | 68 | 87 | | } |
| | | 88 | | |
| | | 89 | | public virtual async Task<ItemUpdateType> RefreshMetadata(BaseItem item, MetadataRefreshOptions refreshOptions, |
| | | 90 | | { |
| | | 91 | | var itemOfType = (TItemType)item; |
| | | 92 | | var updateType = ItemUpdateType.None; |
| | | 93 | | |
| | | 94 | | var libraryOptions = LibraryManager.GetLibraryOptions(item); |
| | | 95 | | var isFirstRefresh = item.DateLastRefreshed == DateTime.MinValue; |
| | | 96 | | var hasRefreshedMetadata = true; |
| | | 97 | | var hasRefreshedImages = true; |
| | | 98 | | |
| | | 99 | | var requiresRefresh = libraryOptions.AutomaticRefreshIntervalDays > 0 && (DateTime.UtcNow - item.DateLastRef |
| | | 100 | | |
| | | 101 | | if (!requiresRefresh && refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None) |
| | | 102 | | { |
| | | 103 | | // TODO: If this returns true, should we instead just change metadata refresh mode to Full? |
| | | 104 | | requiresRefresh = item.RequiresRefresh(); |
| | | 105 | | |
| | | 106 | | if (requiresRefresh) |
| | | 107 | | { |
| | | 108 | | Logger.LogDebug("Refreshing {Type} {Item} because item.RequiresRefresh() returned true", typeof(TIte |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | if (refreshOptions.RemoveOldMetadata && refreshOptions.ReplaceAllImages) |
| | | 113 | | { |
| | | 114 | | if (ImageProvider.RemoveImages(item)) |
| | | 115 | | { |
| | | 116 | | updateType |= ItemUpdateType.ImageUpdate; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | var localImagesFailed = false; |
| | | 121 | | var allImageProviders = ProviderManager.GetImageProviders(item, refreshOptions).ToList(); |
| | | 122 | | |
| | | 123 | | // Only validate already registered images if we are replacing and saving locally |
| | | 124 | | if (item.IsSaveLocalMetadataEnabled() && refreshOptions.ReplaceAllImages) |
| | | 125 | | { |
| | | 126 | | item.ValidateImages(); |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | | 130 | | // Run full image validation and register new local images |
| | | 131 | | try |
| | | 132 | | { |
| | | 133 | | if (ImageProvider.ValidateImages(item, allImageProviders.OfType<ILocalImageProvider>(), refreshOptio |
| | | 134 | | { |
| | | 135 | | updateType |= ItemUpdateType.ImageUpdate; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | | 140 | | localImagesFailed = true; |
| | | 141 | | Logger.LogError(ex, "Error validating images for {Item}", item.Path ?? item.Name ?? "Unknown name"); |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | var metadataResult = new MetadataResult<TItemType> |
| | | 146 | | { |
| | | 147 | | Item = itemOfType |
| | | 148 | | }; |
| | | 149 | | |
| | | 150 | | var beforeSaveResult = await BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || r |
| | | 151 | | .ConfigureAwait(false); |
| | | 152 | | updateType |= beforeSaveResult; |
| | | 153 | | |
| | | 154 | | updateType = await SaveInternal(item, refreshOptions, updateType, isFirstRefresh, requiresRefresh, metadataR |
| | | 155 | | |
| | | 156 | | // Next run metadata providers |
| | | 157 | | if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None) |
| | | 158 | | { |
| | | 159 | | var providers = GetProviders(item, libraryOptions, refreshOptions, isFirstRefresh, requiresRefresh) |
| | | 160 | | .ToList(); |
| | | 161 | | |
| | | 162 | | if (providers.Count > 0 || isFirstRefresh || requiresRefresh) |
| | | 163 | | { |
| | | 164 | | if (item.BeforeMetadataRefresh(refreshOptions.ReplaceAllMetadata)) |
| | | 165 | | { |
| | | 166 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | if (providers.Count > 0) |
| | | 171 | | { |
| | | 172 | | var id = itemOfType.GetLookupInfo(); |
| | | 173 | | |
| | | 174 | | if (refreshOptions.SearchResult is not null) |
| | | 175 | | { |
| | | 176 | | ApplySearchResult(id, refreshOptions.SearchResult); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | id.IsAutomated = refreshOptions.IsAutomated; |
| | | 180 | | |
| | | 181 | | var hasMetadataSavers = ProviderManager.GetMetadataSavers(item, libraryOptions).Any(); |
| | | 182 | | var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, ImageProvider |
| | | 183 | | |
| | | 184 | | updateType |= result.UpdateType; |
| | | 185 | | if (result.Failures > 0) |
| | | 186 | | { |
| | | 187 | | hasRefreshedMetadata = false; |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | // Next run remote image providers, but only if local image providers didn't throw an exception |
| | | 193 | | if (!localImagesFailed && refreshOptions.ImageRefreshMode > MetadataRefreshMode.ValidationOnly) |
| | | 194 | | { |
| | | 195 | | var providers = GetNonLocalImageProviders(item, allImageProviders, refreshOptions).ToList(); |
| | | 196 | | |
| | | 197 | | if (providers.Count > 0) |
| | | 198 | | { |
| | | 199 | | var result = await ImageProvider.RefreshImages(itemOfType, libraryOptions, providers, refreshOptions |
| | | 200 | | |
| | | 201 | | updateType |= result.UpdateType; |
| | | 202 | | if (result.Failures > 0) |
| | | 203 | | { |
| | | 204 | | hasRefreshedImages = false; |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | if (hasRefreshedMetadata && hasRefreshedImages) |
| | | 210 | | { |
| | | 211 | | item.DateLastRefreshed = DateTime.UtcNow; |
| | | 212 | | updateType |= item.OnMetadataChanged(); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | updateType = await SaveInternal(item, refreshOptions, updateType, isFirstRefresh, requiresRefresh, metadataR |
| | | 216 | | |
| | | 217 | | await AfterMetadataRefresh(itemOfType, refreshOptions, cancellationToken).ConfigureAwait(false); |
| | | 218 | | |
| | | 219 | | return updateType; |
| | | 220 | | |
| | | 221 | | async Task<ItemUpdateType> SaveInternal(BaseItem item, MetadataRefreshOptions refreshOptions, ItemUpdateType |
| | | 222 | | { |
| | | 223 | | // Save if changes were made, or it's never been saved before |
| | | 224 | | if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.Rep |
| | | 225 | | { |
| | | 226 | | if (item.IsFileProtocol) |
| | | 227 | | { |
| | | 228 | | var file = TryGetFileSystemMetadata(item.Path, refreshOptions.DirectoryService); |
| | | 229 | | if (file is not null) |
| | | 230 | | { |
| | | 231 | | item.DateModified = file.LastWriteTimeUtc; |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | // If any of these properties are set then make sure the updateType is not None, just to force every |
| | | 236 | | if (refreshOptions.ForceSave || refreshOptions.ReplaceAllMetadata) |
| | | 237 | | { |
| | | 238 | | updateType |= ItemUpdateType.MetadataDownload; |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | // Save to database |
| | | 242 | | await SaveItemAsync(metadataResult, updateType, cancellationToken).ConfigureAwait(false); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | return updateType; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | private void ApplySearchResult(ItemLookupInfo lookupInfo, RemoteSearchResult result) |
| | | 250 | | { |
| | | 251 | | // Episode and Season do not support Identify, so the search results are the Series' |
| | | 252 | | switch (lookupInfo) |
| | | 253 | | { |
| | | 254 | | case EpisodeInfo episodeInfo: |
| | 0 | 255 | | episodeInfo.SeriesProviderIds = result.ProviderIds; |
| | 0 | 256 | | episodeInfo.ProviderIds.Clear(); |
| | 0 | 257 | | break; |
| | | 258 | | case SeasonInfo seasonInfo: |
| | 0 | 259 | | seasonInfo.SeriesProviderIds = result.ProviderIds; |
| | 0 | 260 | | seasonInfo.ProviderIds.Clear(); |
| | 0 | 261 | | break; |
| | | 262 | | default: |
| | 0 | 263 | | lookupInfo.ProviderIds = result.ProviderIds; |
| | 0 | 264 | | lookupInfo.Name = result.Name; |
| | 0 | 265 | | lookupInfo.Year = result.ProductionYear; |
| | | 266 | | break; |
| | | 267 | | } |
| | 0 | 268 | | } |
| | | 269 | | |
| | | 270 | | protected async Task SaveItemAsync(MetadataResult<TItemType> result, ItemUpdateType reason, CancellationToken ca |
| | | 271 | | { |
| | | 272 | | await result.Item.UpdateToRepositoryAsync(reason, cancellationToken).ConfigureAwait(false); |
| | | 273 | | if (result.Item.SupportsPeople && result.People is not null) |
| | | 274 | | { |
| | | 275 | | var baseItem = result.Item; |
| | | 276 | | |
| | | 277 | | await LibraryManager.UpdatePeopleAsync(baseItem, result.People, cancellationToken).ConfigureAwait(false) |
| | | 278 | | } |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationT |
| | | 282 | | { |
| | 55 | 283 | | item.AfterMetadataRefresh(); |
| | 55 | 284 | | return Task.CompletedTask; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Before the save. |
| | | 289 | | /// </summary> |
| | | 290 | | /// <param name="item">The item.</param> |
| | | 291 | | /// <param name="isFullRefresh">if set to <c>true</c> [is full refresh].</param> |
| | | 292 | | /// <param name="currentUpdateType">Type of the current update.</param> |
| | | 293 | | /// <returns>ItemUpdateType.</returns> |
| | | 294 | | private async Task<ItemUpdateType> BeforeSave(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateTy |
| | | 295 | | { |
| | | 296 | | var updateType = BeforeSaveInternal(item, isFullRefresh, currentUpdateType); |
| | | 297 | | |
| | | 298 | | updateType |= item.OnMetadataChanged(); |
| | | 299 | | |
| | | 300 | | if (updateType == ItemUpdateType.None) |
| | | 301 | | { |
| | | 302 | | if (!await ItemRepository.ItemExistsAsync(item.Id).ConfigureAwait(false)) |
| | | 303 | | { |
| | | 304 | | return ItemUpdateType.MetadataImport; |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | return updateType; |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | protected virtual ItemUpdateType BeforeSaveInternal(TItemType item, bool isFullRefresh, ItemUpdateType updateTyp |
| | | 312 | | { |
| | 55 | 313 | | if (EnableUpdateMetadataFromChildren(item, isFullRefresh, updateType)) |
| | | 314 | | { |
| | 0 | 315 | | if (isFullRefresh || updateType > ItemUpdateType.None) |
| | | 316 | | { |
| | 0 | 317 | | var children = GetChildrenForMetadataUpdates(item); |
| | | 318 | | |
| | 0 | 319 | | updateType = UpdateMetadataFromChildren(item, children, isFullRefresh, updateType); |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | |
| | 55 | 323 | | var presentationUniqueKey = item.CreatePresentationUniqueKey(); |
| | 55 | 324 | | if (!string.Equals(item.PresentationUniqueKey, presentationUniqueKey, StringComparison.Ordinal)) |
| | | 325 | | { |
| | 33 | 326 | | item.PresentationUniqueKey = presentationUniqueKey; |
| | 33 | 327 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | // Cleanup extracted files if source file was modified |
| | 55 | 331 | | var itemPath = item.Path; |
| | 55 | 332 | | if (!string.IsNullOrEmpty(itemPath)) |
| | | 333 | | { |
| | 55 | 334 | | var info = FileSystem.GetFileSystemInfo(itemPath); |
| | 55 | 335 | | if (info.Exists && item.HasChanged(info.LastWriteTimeUtc)) |
| | | 336 | | { |
| | 0 | 337 | | Logger.LogDebug("File modification time changed from {Then} to {Now}: {Path}", item.DateModified, in |
| | | 338 | | |
| | 0 | 339 | | item.DateModified = info.LastWriteTimeUtc; |
| | 0 | 340 | | if (ServerConfigurationManager.GetMetadataConfiguration().UseFileCreationTimeForDateAdded) |
| | | 341 | | { |
| | 0 | 342 | | item.DateCreated = info.CreationTimeUtc; |
| | | 343 | | } |
| | | 344 | | |
| | 0 | 345 | | if (item is Video video) |
| | | 346 | | { |
| | 0 | 347 | | Logger.LogInformation("File changed, pruning extracted data: {Path}", item.Path); |
| | 0 | 348 | | ExternalDataManager.DeleteExternalItemDataAsync(video, CancellationToken.None).GetAwaiter().GetR |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | |
| | 55 | 355 | | return updateType; |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | protected virtual bool EnableUpdateMetadataFromChildren(TItemType item, bool isFullRefresh, ItemUpdateType curre |
| | | 359 | | { |
| | 55 | 360 | | if (isFullRefresh || currentUpdateType > ItemUpdateType.None) |
| | | 361 | | { |
| | 34 | 362 | | if (EnableUpdatingPremiereDateFromChildren || EnableUpdatingGenresFromChildren || EnableUpdatingStudiosF |
| | | 363 | | { |
| | 0 | 364 | | return true; |
| | | 365 | | } |
| | | 366 | | |
| | 34 | 367 | | if (item is Folder folder) |
| | | 368 | | { |
| | 34 | 369 | | return folder.SupportsDateLastMediaAdded || folder.SupportsCumulativeRunTimeTicks; |
| | | 370 | | } |
| | | 371 | | } |
| | | 372 | | |
| | 21 | 373 | | return false; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | protected virtual IReadOnlyList<BaseItem> GetChildrenForMetadataUpdates(TItemType item) |
| | | 377 | | { |
| | 0 | 378 | | if (item is Folder folder) |
| | | 379 | | { |
| | 0 | 380 | | return folder.GetRecursiveChildren(); |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | return []; |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | protected virtual ItemUpdateType UpdateMetadataFromChildren(TItemType item, IReadOnlyList<BaseItem> children, bo |
| | | 387 | | { |
| | 0 | 388 | | var updateType = ItemUpdateType.None; |
| | | 389 | | |
| | 0 | 390 | | if (isFullRefresh || currentUpdateType > ItemUpdateType.None) |
| | | 391 | | { |
| | 0 | 392 | | updateType |= UpdateCumulativeRunTimeTicks(item, children); |
| | 0 | 393 | | updateType |= UpdateDateLastMediaAdded(item, children); |
| | | 394 | | |
| | | 395 | | // don't update user-changeable metadata for locked items |
| | 0 | 396 | | if (item.IsLocked) |
| | | 397 | | { |
| | 0 | 398 | | return updateType; |
| | | 399 | | } |
| | | 400 | | |
| | 0 | 401 | | if (EnableUpdatingPremiereDateFromChildren) |
| | | 402 | | { |
| | 0 | 403 | | updateType |= UpdatePremiereDate(item, children); |
| | | 404 | | } |
| | | 405 | | |
| | 0 | 406 | | if (EnableUpdatingGenresFromChildren) |
| | | 407 | | { |
| | 0 | 408 | | updateType |= UpdateGenres(item, children); |
| | | 409 | | } |
| | | 410 | | |
| | 0 | 411 | | if (EnableUpdatingStudiosFromChildren) |
| | | 412 | | { |
| | 0 | 413 | | updateType |= UpdateStudios(item, children); |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | if (EnableUpdatingOfficialRatingFromChildren) |
| | | 417 | | { |
| | 0 | 418 | | updateType |= UpdateOfficialRating(item, children); |
| | | 419 | | } |
| | | 420 | | } |
| | | 421 | | |
| | 0 | 422 | | return updateType; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | private ItemUpdateType UpdateCumulativeRunTimeTicks(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 426 | | { |
| | 0 | 427 | | if (item is Folder folder && folder.SupportsCumulativeRunTimeTicks) |
| | | 428 | | { |
| | 0 | 429 | | long ticks = 0; |
| | | 430 | | |
| | 0 | 431 | | foreach (var child in children) |
| | | 432 | | { |
| | 0 | 433 | | if (!child.IsFolder) |
| | | 434 | | { |
| | 0 | 435 | | ticks += child.RunTimeTicks ?? 0; |
| | | 436 | | } |
| | | 437 | | } |
| | | 438 | | |
| | 0 | 439 | | if (!folder.RunTimeTicks.HasValue || folder.RunTimeTicks.Value != ticks) |
| | | 440 | | { |
| | 0 | 441 | | folder.RunTimeTicks = ticks; |
| | 0 | 442 | | return ItemUpdateType.MetadataImport; |
| | | 443 | | } |
| | | 444 | | } |
| | | 445 | | |
| | 0 | 446 | | return ItemUpdateType.None; |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | private ItemUpdateType UpdateDateLastMediaAdded(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 450 | | { |
| | 0 | 451 | | var updateType = ItemUpdateType.None; |
| | | 452 | | |
| | 0 | 453 | | if (item is Folder folder && folder.SupportsDateLastMediaAdded) |
| | | 454 | | { |
| | 0 | 455 | | var dateLastMediaAdded = DateTime.MinValue; |
| | 0 | 456 | | var any = false; |
| | | 457 | | |
| | 0 | 458 | | foreach (var child in children) |
| | | 459 | | { |
| | | 460 | | // Exclude any folders and virtual items since they are only placeholders |
| | 0 | 461 | | if (!child.IsFolder && !child.IsVirtualItem) |
| | | 462 | | { |
| | 0 | 463 | | var childDateCreated = child.DateCreated; |
| | 0 | 464 | | if (childDateCreated > dateLastMediaAdded) |
| | | 465 | | { |
| | 0 | 466 | | dateLastMediaAdded = childDateCreated; |
| | | 467 | | } |
| | | 468 | | |
| | 0 | 469 | | any = true; |
| | | 470 | | } |
| | | 471 | | } |
| | | 472 | | |
| | 0 | 473 | | if ((!folder.DateLastMediaAdded.HasValue && any) || folder.DateLastMediaAdded != dateLastMediaAdded) |
| | | 474 | | { |
| | 0 | 475 | | folder.DateLastMediaAdded = dateLastMediaAdded; |
| | 0 | 476 | | updateType = ItemUpdateType.MetadataImport; |
| | | 477 | | } |
| | | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | return updateType; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | private ItemUpdateType UpdatePremiereDate(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 484 | | { |
| | 0 | 485 | | var updateType = ItemUpdateType.None; |
| | | 486 | | |
| | 0 | 487 | | if (children.Count == 0) |
| | | 488 | | { |
| | 0 | 489 | | return updateType; |
| | | 490 | | } |
| | | 491 | | |
| | 0 | 492 | | var date = children.Select(i => i.PremiereDate ?? DateTime.MaxValue).Min(); |
| | | 493 | | |
| | 0 | 494 | | var originalPremiereDate = item.PremiereDate; |
| | 0 | 495 | | var originalProductionYear = item.ProductionYear; |
| | | 496 | | |
| | 0 | 497 | | if (date > DateTime.MinValue && date < DateTime.MaxValue) |
| | | 498 | | { |
| | 0 | 499 | | item.PremiereDate = date; |
| | 0 | 500 | | item.ProductionYear = date.Year; |
| | | 501 | | } |
| | | 502 | | else |
| | | 503 | | { |
| | 0 | 504 | | var year = children.Select(i => i.ProductionYear ?? 0).Min(); |
| | | 505 | | |
| | 0 | 506 | | if (year > 0) |
| | | 507 | | { |
| | 0 | 508 | | item.ProductionYear = year; |
| | | 509 | | } |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | if ((originalPremiereDate ?? DateTime.MinValue) != (item.PremiereDate ?? DateTime.MinValue) |
| | 0 | 513 | | || (originalProductionYear ?? -1) != (item.ProductionYear ?? -1)) |
| | | 514 | | { |
| | 0 | 515 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 516 | | } |
| | | 517 | | |
| | 0 | 518 | | return updateType; |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | private ItemUpdateType UpdateGenres(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 522 | | { |
| | 0 | 523 | | var updateType = ItemUpdateType.None; |
| | | 524 | | |
| | 0 | 525 | | if (!item.LockedFields.Contains(MetadataField.Genres)) |
| | | 526 | | { |
| | 0 | 527 | | var currentList = item.Genres; |
| | | 528 | | |
| | 0 | 529 | | item.Genres = children.SelectMany(i => i.Genres) |
| | 0 | 530 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 531 | | .ToArray(); |
| | | 532 | | |
| | 0 | 533 | | if (currentList.Length != item.Genres.Length || !currentList.Order().SequenceEqual(item.Genres.Order(), |
| | | 534 | | { |
| | 0 | 535 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 536 | | } |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | return updateType; |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private ItemUpdateType UpdateStudios(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 543 | | { |
| | 0 | 544 | | var updateType = ItemUpdateType.None; |
| | | 545 | | |
| | 0 | 546 | | if (!item.LockedFields.Contains(MetadataField.Studios)) |
| | | 547 | | { |
| | 0 | 548 | | var currentList = item.Studios; |
| | | 549 | | |
| | 0 | 550 | | item.Studios = children.SelectMany(i => i.Studios) |
| | 0 | 551 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 552 | | .ToArray(); |
| | | 553 | | |
| | 0 | 554 | | if (currentList.Length != item.Studios.Length || !currentList.Order().SequenceEqual(item.Studios.Order() |
| | | 555 | | { |
| | 0 | 556 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 557 | | } |
| | | 558 | | } |
| | | 559 | | |
| | 0 | 560 | | return updateType; |
| | | 561 | | } |
| | | 562 | | |
| | | 563 | | private ItemUpdateType UpdateOfficialRating(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 564 | | { |
| | 0 | 565 | | var updateType = ItemUpdateType.None; |
| | | 566 | | |
| | 0 | 567 | | if (!item.LockedFields.Contains(MetadataField.OfficialRating)) |
| | | 568 | | { |
| | 0 | 569 | | if (item.UpdateRatingToItems(children)) |
| | | 570 | | { |
| | 0 | 571 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 572 | | } |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | return updateType; |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | /// <summary> |
| | | 579 | | /// Gets the providers. |
| | | 580 | | /// </summary> |
| | | 581 | | /// <param name="item">A media item.</param> |
| | | 582 | | /// <param name="libraryOptions">The LibraryOptions to use.</param> |
| | | 583 | | /// <param name="options">The MetadataRefreshOptions to use.</param> |
| | | 584 | | /// <param name="isFirstRefresh">Specifies first refresh mode.</param> |
| | | 585 | | /// <param name="requiresRefresh">Specifies refresh mode.</param> |
| | | 586 | | /// <returns>IEnumerable{`0}.</returns> |
| | | 587 | | protected IEnumerable<IMetadataProvider> GetProviders(BaseItem item, LibraryOptions libraryOptions, MetadataRefr |
| | | 588 | | { |
| | | 589 | | // Get providers to refresh |
| | 55 | 590 | | var providers = ProviderManager.GetMetadataProviders<TItemType>(item, libraryOptions).ToList(); |
| | | 591 | | |
| | 55 | 592 | | var metadataRefreshMode = options.MetadataRefreshMode; |
| | | 593 | | |
| | | 594 | | // Run all if either of these flags are true |
| | 55 | 595 | | var runAllProviders = options.ReplaceAllMetadata || |
| | 55 | 596 | | metadataRefreshMode == MetadataRefreshMode.FullRefresh || |
| | 55 | 597 | | (isFirstRefresh && metadataRefreshMode >= MetadataRefreshMode.Default) || |
| | 55 | 598 | | (requiresRefresh && metadataRefreshMode >= MetadataRefreshMode.Default); |
| | | 599 | | |
| | 55 | 600 | | if (!runAllProviders) |
| | | 601 | | { |
| | 21 | 602 | | var providersWithChanges = providers |
| | 21 | 603 | | .Where(i => |
| | 21 | 604 | | { |
| | 21 | 605 | | if (i is IHasItemChangeMonitor hasFileChangeMonitor) |
| | 21 | 606 | | { |
| | 21 | 607 | | return HasChanged(item, hasFileChangeMonitor, options.DirectoryService); |
| | 21 | 608 | | } |
| | 21 | 609 | | |
| | 21 | 610 | | return false; |
| | 21 | 611 | | }) |
| | 21 | 612 | | .ToList(); |
| | | 613 | | |
| | 21 | 614 | | if (providersWithChanges.Count == 0) |
| | | 615 | | { |
| | 0 | 616 | | providers = new List<IMetadataProvider<TItemType>>(); |
| | | 617 | | } |
| | | 618 | | else |
| | | 619 | | { |
| | 21 | 620 | | var anyRemoteProvidersChanged = providersWithChanges.OfType<IRemoteMetadataProvider>() |
| | 21 | 621 | | .Any(); |
| | | 622 | | |
| | 21 | 623 | | var anyLocalProvidersChanged = providersWithChanges.OfType<ILocalMetadataProvider>() |
| | 21 | 624 | | .Any(); |
| | | 625 | | |
| | 21 | 626 | | var anyLocalPreRefreshProvidersChanged = providersWithChanges.OfType<IPreRefreshProvider>() |
| | 21 | 627 | | .Any(); |
| | | 628 | | |
| | 21 | 629 | | providers = providers.Where(i => |
| | 21 | 630 | | { |
| | 21 | 631 | | // If any provider reports a change, always run local ones as well |
| | 21 | 632 | | if (i is ILocalMetadataProvider) |
| | 21 | 633 | | { |
| | 21 | 634 | | return anyRemoteProvidersChanged || anyLocalProvidersChanged || anyLocalPreRefreshProvidersC |
| | 21 | 635 | | } |
| | 21 | 636 | | |
| | 21 | 637 | | // If any remote providers changed, run them all so that priorities can be honored |
| | 21 | 638 | | if (i is IRemoteMetadataProvider) |
| | 21 | 639 | | { |
| | 21 | 640 | | if (options.MetadataRefreshMode == MetadataRefreshMode.ValidationOnly) |
| | 21 | 641 | | { |
| | 21 | 642 | | return false; |
| | 21 | 643 | | } |
| | 21 | 644 | | |
| | 21 | 645 | | return anyRemoteProvidersChanged; |
| | 21 | 646 | | } |
| | 21 | 647 | | |
| | 21 | 648 | | // Run custom refresh providers if they report a change or any remote providers change |
| | 21 | 649 | | return anyRemoteProvidersChanged || providersWithChanges.Contains(i); |
| | 21 | 650 | | }).ToList(); |
| | | 651 | | } |
| | | 652 | | } |
| | | 653 | | |
| | 55 | 654 | | return providers; |
| | | 655 | | } |
| | | 656 | | |
| | | 657 | | protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(BaseItem item, IEnumerable<IImageProvide |
| | | 658 | | { |
| | | 659 | | // Get providers to refresh |
| | 55 | 660 | | var providers = allImageProviders.Where(i => i is not ILocalImageProvider); |
| | | 661 | | |
| | 55 | 662 | | var dateLastImageRefresh = item.DateLastRefreshed; |
| | | 663 | | |
| | | 664 | | // Run all if either of these flags are true |
| | 55 | 665 | | var runAllProviders = options.ImageRefreshMode == MetadataRefreshMode.FullRefresh || dateLastImageRefresh.Da |
| | | 666 | | |
| | 55 | 667 | | if (!runAllProviders) |
| | | 668 | | { |
| | 22 | 669 | | providers = providers |
| | 22 | 670 | | .Where(i => |
| | 22 | 671 | | { |
| | 22 | 672 | | if (i is IHasItemChangeMonitor hasFileChangeMonitor) |
| | 22 | 673 | | { |
| | 22 | 674 | | return HasChanged(item, hasFileChangeMonitor, options.DirectoryService); |
| | 22 | 675 | | } |
| | 22 | 676 | | |
| | 22 | 677 | | return false; |
| | 22 | 678 | | }); |
| | | 679 | | } |
| | | 680 | | |
| | 55 | 681 | | return providers; |
| | | 682 | | } |
| | | 683 | | |
| | | 684 | | public bool CanRefresh(BaseItem item) |
| | | 685 | | { |
| | 1118 | 686 | | return item is TItemType; |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | public bool CanRefreshPrimary(Type type) |
| | | 690 | | { |
| | 1365 | 691 | | return type == typeof(TItemType); |
| | | 692 | | } |
| | | 693 | | |
| | | 694 | | protected virtual async Task<RefreshResult> RefreshWithProviders( |
| | | 695 | | MetadataResult<TItemType> metadata, |
| | | 696 | | TIdType id, |
| | | 697 | | MetadataRefreshOptions options, |
| | | 698 | | ICollection<IMetadataProvider> providers, |
| | | 699 | | ItemImageProvider imageService, |
| | | 700 | | bool isSavingMetadata, |
| | | 701 | | CancellationToken cancellationToken) |
| | | 702 | | { |
| | | 703 | | var refreshResult = new RefreshResult |
| | | 704 | | { |
| | | 705 | | UpdateType = ItemUpdateType.None |
| | | 706 | | }; |
| | | 707 | | |
| | | 708 | | var item = metadata.Item; |
| | | 709 | | |
| | | 710 | | var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList(); |
| | | 711 | | var logName = !item.IsFileProtocol ? item.Name ?? item.Path : item.Path ?? item.Name; |
| | | 712 | | |
| | | 713 | | foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider)) |
| | | 714 | | { |
| | | 715 | | await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwa |
| | | 716 | | } |
| | | 717 | | |
| | | 718 | | if (item.IsLocked) |
| | | 719 | | { |
| | | 720 | | return refreshResult; |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | var temp = new MetadataResult<TItemType> |
| | | 724 | | { |
| | | 725 | | Item = CreateNew() |
| | | 726 | | }; |
| | | 727 | | temp.Item.Path = item.Path; |
| | | 728 | | temp.Item.Id = item.Id; |
| | | 729 | | temp.Item.ParentIndexNumber = item.ParentIndexNumber; |
| | | 730 | | temp.Item.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; |
| | | 731 | | temp.Item.PreferredMetadataLanguage = item.PreferredMetadataLanguage; |
| | | 732 | | |
| | | 733 | | var foundImageTypes = new List<ImageType>(); |
| | | 734 | | |
| | | 735 | | // Do not execute local providers if we are identifying or replacing with local metadata saving enabled |
| | | 736 | | if (options.SearchResult is null && !(isSavingMetadata && options.ReplaceAllMetadata)) |
| | | 737 | | { |
| | | 738 | | foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>()) |
| | | 739 | | { |
| | | 740 | | var providerName = provider.GetType().Name; |
| | | 741 | | Logger.LogDebug("Running {Provider} for {Item}", providerName, logName); |
| | | 742 | | |
| | | 743 | | var itemInfo = new ItemInfo(item); |
| | | 744 | | |
| | | 745 | | try |
| | | 746 | | { |
| | | 747 | | var localItem = await provider.GetMetadata(itemInfo, options.DirectoryService, cancellationToken |
| | | 748 | | |
| | | 749 | | if (localItem.HasMetadata) |
| | | 750 | | { |
| | | 751 | | foreach (var remoteImage in localItem.RemoteImages) |
| | | 752 | | { |
| | | 753 | | try |
| | | 754 | | { |
| | | 755 | | if (item.ImageInfos.Any(x => x.Type == remoteImage.Type) |
| | | 756 | | && !options.IsReplacingImage(remoteImage.Type)) |
| | | 757 | | { |
| | | 758 | | continue; |
| | | 759 | | } |
| | | 760 | | |
| | | 761 | | await ProviderManager.SaveImage(item, remoteImage.Url, remoteImage.Type, null, cance |
| | | 762 | | refreshResult.UpdateType |= ItemUpdateType.ImageUpdate; |
| | | 763 | | |
| | | 764 | | // remember imagetype that has just been downloaded |
| | | 765 | | foundImageTypes.Add(remoteImage.Type); |
| | | 766 | | } |
| | | 767 | | catch (HttpRequestException ex) |
| | | 768 | | { |
| | | 769 | | Logger.LogError(ex, "Could not save {ImageType} image: {Url}", Enum.GetName(remoteIm |
| | | 770 | | } |
| | | 771 | | } |
| | | 772 | | |
| | | 773 | | if (foundImageTypes.Count > 0) |
| | | 774 | | { |
| | | 775 | | imageService.UpdateReplaceImages(options, foundImageTypes); |
| | | 776 | | } |
| | | 777 | | |
| | | 778 | | if (imageService.MergeImages(item, localItem.Images, options)) |
| | | 779 | | { |
| | | 780 | | refreshResult.UpdateType |= ItemUpdateType.ImageUpdate; |
| | | 781 | | } |
| | | 782 | | |
| | | 783 | | MergeData(localItem, temp, [], false, true); |
| | | 784 | | refreshResult.UpdateType |= ItemUpdateType.MetadataImport; |
| | | 785 | | |
| | | 786 | | break; |
| | | 787 | | } |
| | | 788 | | |
| | | 789 | | Logger.LogDebug("{Provider} returned no metadata for {Item}", providerName, logName); |
| | | 790 | | } |
| | | 791 | | catch (OperationCanceledException) |
| | | 792 | | { |
| | | 793 | | throw; |
| | | 794 | | } |
| | | 795 | | catch (Exception ex) |
| | | 796 | | { |
| | | 797 | | Logger.LogError(ex, "Error in {Provider}", provider.Name); |
| | | 798 | | |
| | | 799 | | // If a local provider fails, consider that a failure |
| | | 800 | | refreshResult.ErrorMessage = ex.Message; |
| | | 801 | | } |
| | | 802 | | } |
| | | 803 | | } |
| | | 804 | | |
| | | 805 | | var isLocalLocked = temp.Item.IsLocked; |
| | | 806 | | if (!isLocalLocked && (options.ReplaceAllMetadata || options.MetadataRefreshMode > MetadataRefreshMode.Valid |
| | | 807 | | { |
| | | 808 | | var remoteResult = await ExecuteRemoteProviders(temp, logName, false, id, providers.OfType<IRemoteMetada |
| | | 809 | | .ConfigureAwait(false); |
| | | 810 | | |
| | | 811 | | refreshResult.UpdateType |= remoteResult.UpdateType; |
| | | 812 | | refreshResult.ErrorMessage = remoteResult.ErrorMessage; |
| | | 813 | | refreshResult.Failures += remoteResult.Failures; |
| | | 814 | | } |
| | | 815 | | |
| | | 816 | | if (providers.Any(i => i is not ICustomMetadataProvider)) |
| | | 817 | | { |
| | | 818 | | if (refreshResult.UpdateType > ItemUpdateType.None) |
| | | 819 | | { |
| | | 820 | | if (!options.RemoveOldMetadata) |
| | | 821 | | { |
| | | 822 | | // Add existing metadata to provider result if it does not exist there |
| | | 823 | | MergeData(metadata, temp, [], false, false); |
| | | 824 | | } |
| | | 825 | | |
| | | 826 | | if (isLocalLocked) |
| | | 827 | | { |
| | | 828 | | MergeData(temp, metadata, item.LockedFields, true, true); |
| | | 829 | | } |
| | | 830 | | else |
| | | 831 | | { |
| | | 832 | | var shouldReplace = (options.MetadataRefreshMode > MetadataRefreshMode.ValidationOnly && options |
| | | 833 | | // Case for Scan for new and updated files |
| | | 834 | | || (options.MetadataRefreshMode == MetadataRefreshMode.Default && !options.ReplaceAllMetadat |
| | | 835 | | MergeData(temp, metadata, item.LockedFields, shouldReplace, true); |
| | | 836 | | } |
| | | 837 | | } |
| | | 838 | | } |
| | | 839 | | |
| | | 840 | | foreach (var provider in customProviders.Where(i => i is not IPreRefreshProvider)) |
| | | 841 | | { |
| | | 842 | | await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwa |
| | | 843 | | } |
| | | 844 | | |
| | | 845 | | return refreshResult; |
| | | 846 | | } |
| | | 847 | | |
| | | 848 | | private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, string logName |
| | | 849 | | { |
| | | 850 | | Logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, logName); |
| | | 851 | | |
| | | 852 | | try |
| | | 853 | | { |
| | | 854 | | refreshResult.UpdateType |= await provider.FetchAsync(item, options, cancellationToken).ConfigureAwait(f |
| | | 855 | | } |
| | | 856 | | catch (OperationCanceledException) |
| | | 857 | | { |
| | | 858 | | throw; |
| | | 859 | | } |
| | | 860 | | catch (Exception ex) |
| | | 861 | | { |
| | | 862 | | refreshResult.ErrorMessage = ex.Message; |
| | | 863 | | Logger.LogError(ex, "Error in {Provider}", provider.Name); |
| | | 864 | | } |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | protected virtual TItemType CreateNew() |
| | | 868 | | { |
| | 55 | 869 | | return new TItemType(); |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | private async Task<RefreshResult> ExecuteRemoteProviders(MetadataResult<TItemType> temp, string logName, bool re |
| | | 873 | | { |
| | | 874 | | var refreshResult = new RefreshResult(); |
| | | 875 | | |
| | | 876 | | if (id is not null) |
| | | 877 | | { |
| | | 878 | | MergeNewData(temp.Item, id); |
| | | 879 | | } |
| | | 880 | | |
| | | 881 | | foreach (var provider in providers) |
| | | 882 | | { |
| | | 883 | | var providerName = provider.GetType().Name; |
| | | 884 | | Logger.LogDebug("Running {Provider} for {Item}", providerName, logName); |
| | | 885 | | |
| | | 886 | | try |
| | | 887 | | { |
| | | 888 | | var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false); |
| | | 889 | | |
| | | 890 | | if (result.HasMetadata) |
| | | 891 | | { |
| | | 892 | | result.Provider = provider.Name; |
| | | 893 | | |
| | | 894 | | MergeData(result, temp, [], replaceData, false); |
| | | 895 | | MergeNewData(temp.Item, id); |
| | | 896 | | |
| | | 897 | | refreshResult.UpdateType |= ItemUpdateType.MetadataDownload; |
| | | 898 | | } |
| | | 899 | | else |
| | | 900 | | { |
| | | 901 | | Logger.LogDebug("{Provider} returned no metadata for {Item}", providerName, logName); |
| | | 902 | | } |
| | | 903 | | } |
| | | 904 | | catch (OperationCanceledException) |
| | | 905 | | { |
| | | 906 | | throw; |
| | | 907 | | } |
| | | 908 | | catch (Exception ex) |
| | | 909 | | { |
| | | 910 | | refreshResult.Failures++; |
| | | 911 | | refreshResult.ErrorMessage = ex.Message; |
| | | 912 | | Logger.LogError(ex, "Error in {Provider}", provider.Name); |
| | | 913 | | } |
| | | 914 | | } |
| | | 915 | | |
| | | 916 | | return refreshResult; |
| | | 917 | | } |
| | | 918 | | |
| | | 919 | | private void MergeNewData(TItemType source, TIdType lookupInfo) |
| | | 920 | | { |
| | | 921 | | // Copy new provider id's that may have been obtained |
| | 110 | 922 | | foreach (var providerId in source.ProviderIds) |
| | | 923 | | { |
| | 0 | 924 | | var key = providerId.Key; |
| | | 925 | | |
| | | 926 | | // Don't replace existing Id's. |
| | 0 | 927 | | lookupInfo.ProviderIds.TryAdd(key, providerId.Value); |
| | | 928 | | } |
| | 55 | 929 | | } |
| | | 930 | | |
| | | 931 | | private bool HasChanged(BaseItem item, IHasItemChangeMonitor changeMonitor, IDirectoryService directoryService) |
| | | 932 | | { |
| | | 933 | | try |
| | | 934 | | { |
| | 21 | 935 | | var hasChanged = changeMonitor.HasChanged(item, directoryService); |
| | | 936 | | |
| | 21 | 937 | | if (hasChanged) |
| | | 938 | | { |
| | 21 | 939 | | Logger.LogDebug("{Monitor} reports change to {Item}", changeMonitor.GetType().Name, item.Path ?? ite |
| | | 940 | | } |
| | | 941 | | |
| | 21 | 942 | | return hasChanged; |
| | | 943 | | } |
| | 0 | 944 | | catch (Exception ex) |
| | | 945 | | { |
| | 0 | 946 | | Logger.LogError(ex, "Error in {Monitor}.HasChanged", changeMonitor.GetType().Name); |
| | 0 | 947 | | return false; |
| | | 948 | | } |
| | 21 | 949 | | } |
| | | 950 | | |
| | | 951 | | /// <summary> |
| | | 952 | | /// Merges metadata from source into target. |
| | | 953 | | /// </summary> |
| | | 954 | | /// <param name="source">The source for new metadata.</param> |
| | | 955 | | /// <param name="target">The target to insert new metadata into.</param> |
| | | 956 | | /// <param name="lockedFields">The fields that are locked and should not be updated.</param> |
| | | 957 | | /// <param name="replaceData"><c>true</c> if existing data should be replaced.</param> |
| | | 958 | | /// <param name="mergeMetadataSettings"><c>true</c> if the metadata settings in target should be updated to matc |
| | | 959 | | /// <exception cref="ArgumentException">Thrown if source or target are null.</exception> |
| | | 960 | | protected virtual void MergeData( |
| | | 961 | | MetadataResult<TItemType> source, |
| | | 962 | | MetadataResult<TItemType> target, |
| | | 963 | | MetadataField[] lockedFields, |
| | | 964 | | bool replaceData, |
| | | 965 | | bool mergeMetadataSettings) |
| | | 966 | | { |
| | 0 | 967 | | MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); |
| | 0 | 968 | | } |
| | | 969 | | |
| | | 970 | | internal static void MergeBaseItemData( |
| | | 971 | | MetadataResult<TItemType> sourceResult, |
| | | 972 | | MetadataResult<TItemType> targetResult, |
| | | 973 | | MetadataField[] lockedFields, |
| | | 974 | | bool replaceData, |
| | | 975 | | bool mergeMetadataSettings) |
| | | 976 | | { |
| | 129 | 977 | | var source = sourceResult.Item; |
| | 129 | 978 | | var target = targetResult.Item; |
| | | 979 | | |
| | 129 | 980 | | ArgumentNullException.ThrowIfNull(sourceResult); |
| | 129 | 981 | | ArgumentNullException.ThrowIfNull(targetResult); |
| | | 982 | | |
| | 129 | 983 | | if (!lockedFields.Contains(MetadataField.Name)) |
| | | 984 | | { |
| | 126 | 985 | | if (replaceData || string.IsNullOrEmpty(target.Name)) |
| | | 986 | | { |
| | | 987 | | // Safeguard against incoming data having an empty name |
| | 125 | 988 | | if (!string.IsNullOrWhiteSpace(source.Name)) |
| | | 989 | | { |
| | 3 | 990 | | target.Name = source.Name; |
| | | 991 | | } |
| | | 992 | | } |
| | | 993 | | } |
| | | 994 | | |
| | 129 | 995 | | if (replaceData || string.IsNullOrEmpty(target.OriginalTitle)) |
| | | 996 | | { |
| | 128 | 997 | | target.OriginalTitle = source.OriginalTitle; |
| | | 998 | | } |
| | | 999 | | |
| | 129 | 1000 | | if (replaceData || !target.CommunityRating.HasValue) |
| | | 1001 | | { |
| | 128 | 1002 | | target.CommunityRating = source.CommunityRating; |
| | | 1003 | | } |
| | | 1004 | | |
| | 129 | 1005 | | if (replaceData || !target.EndDate.HasValue) |
| | | 1006 | | { |
| | 128 | 1007 | | target.EndDate = source.EndDate; |
| | | 1008 | | } |
| | | 1009 | | |
| | 129 | 1010 | | if (!lockedFields.Contains(MetadataField.Genres)) |
| | | 1011 | | { |
| | 127 | 1012 | | if (replaceData || target.Genres.Length == 0) |
| | | 1013 | | { |
| | 126 | 1014 | | target.Genres = source.Genres; |
| | | 1015 | | } |
| | | 1016 | | } |
| | | 1017 | | |
| | 129 | 1018 | | if (replaceData || !target.IndexNumber.HasValue) |
| | | 1019 | | { |
| | 128 | 1020 | | target.IndexNumber = source.IndexNumber; |
| | | 1021 | | } |
| | | 1022 | | |
| | 129 | 1023 | | if (!lockedFields.Contains(MetadataField.OfficialRating)) |
| | | 1024 | | { |
| | 126 | 1025 | | if (replaceData || string.IsNullOrEmpty(target.OfficialRating)) |
| | | 1026 | | { |
| | 125 | 1027 | | target.OfficialRating = source.OfficialRating; |
| | | 1028 | | } |
| | | 1029 | | } |
| | | 1030 | | |
| | 129 | 1031 | | if (replaceData || string.IsNullOrEmpty(target.CustomRating)) |
| | | 1032 | | { |
| | 128 | 1033 | | target.CustomRating = source.CustomRating; |
| | | 1034 | | } |
| | | 1035 | | |
| | 129 | 1036 | | if (replaceData || string.IsNullOrEmpty(target.Tagline)) |
| | | 1037 | | { |
| | 128 | 1038 | | target.Tagline = source.Tagline; |
| | | 1039 | | } |
| | | 1040 | | |
| | 129 | 1041 | | if (!lockedFields.Contains(MetadataField.Overview)) |
| | | 1042 | | { |
| | 126 | 1043 | | if (replaceData || string.IsNullOrEmpty(target.Overview)) |
| | | 1044 | | { |
| | 125 | 1045 | | target.Overview = source.Overview; |
| | | 1046 | | } |
| | | 1047 | | } |
| | | 1048 | | |
| | 129 | 1049 | | if (replaceData || !target.ParentIndexNumber.HasValue) |
| | | 1050 | | { |
| | 128 | 1051 | | target.ParentIndexNumber = source.ParentIndexNumber; |
| | | 1052 | | } |
| | | 1053 | | |
| | 129 | 1054 | | if (!lockedFields.Contains(MetadataField.Cast)) |
| | | 1055 | | { |
| | 128 | 1056 | | if (replaceData || targetResult.People is null || targetResult.People.Count == 0) |
| | | 1057 | | { |
| | 124 | 1058 | | targetResult.People = sourceResult.People; |
| | | 1059 | | } |
| | 4 | 1060 | | else if (sourceResult.People is not null && sourceResult.People.Count > 0) |
| | | 1061 | | { |
| | 4 | 1062 | | MergePeople(sourceResult.People, targetResult.People); |
| | | 1063 | | } |
| | | 1064 | | } |
| | | 1065 | | |
| | 129 | 1066 | | if (replaceData || !target.PremiereDate.HasValue) |
| | | 1067 | | { |
| | 128 | 1068 | | target.PremiereDate = source.PremiereDate; |
| | | 1069 | | } |
| | | 1070 | | |
| | 129 | 1071 | | if (replaceData || !target.ProductionYear.HasValue) |
| | | 1072 | | { |
| | 128 | 1073 | | target.ProductionYear = source.ProductionYear; |
| | | 1074 | | } |
| | | 1075 | | |
| | 129 | 1076 | | if (!lockedFields.Contains(MetadataField.Runtime)) |
| | | 1077 | | { |
| | 129 | 1078 | | if (replaceData || !target.RunTimeTicks.HasValue) |
| | | 1079 | | { |
| | 129 | 1080 | | if (target is not Audio && target is not Video) |
| | | 1081 | | { |
| | 49 | 1082 | | target.RunTimeTicks = source.RunTimeTicks; |
| | | 1083 | | } |
| | | 1084 | | } |
| | | 1085 | | } |
| | | 1086 | | |
| | 129 | 1087 | | if (!lockedFields.Contains(MetadataField.Studios)) |
| | | 1088 | | { |
| | 127 | 1089 | | if (replaceData || target.Studios.Length == 0) |
| | | 1090 | | { |
| | 126 | 1091 | | target.Studios = source.Studios; |
| | | 1092 | | } |
| | | 1093 | | else |
| | | 1094 | | { |
| | 1 | 1095 | | target.Studios = target.Studios.Concat(source.Studios).Distinct(StringComparer.OrdinalIgnoreCase).To |
| | | 1096 | | } |
| | | 1097 | | } |
| | | 1098 | | |
| | 129 | 1099 | | if (!lockedFields.Contains(MetadataField.Tags)) |
| | | 1100 | | { |
| | 127 | 1101 | | if (replaceData || target.Tags.Length == 0) |
| | | 1102 | | { |
| | 126 | 1103 | | target.Tags = source.Tags; |
| | | 1104 | | } |
| | | 1105 | | else |
| | | 1106 | | { |
| | 1 | 1107 | | target.Tags = target.Tags.Concat(source.Tags).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); |
| | | 1108 | | } |
| | | 1109 | | } |
| | | 1110 | | |
| | 129 | 1111 | | if (!lockedFields.Contains(MetadataField.ProductionLocations)) |
| | | 1112 | | { |
| | 127 | 1113 | | if (replaceData || target.ProductionLocations.Length == 0) |
| | | 1114 | | { |
| | 126 | 1115 | | target.ProductionLocations = source.ProductionLocations; |
| | | 1116 | | } |
| | | 1117 | | else |
| | | 1118 | | { |
| | 1 | 1119 | | target.ProductionLocations = target.ProductionLocations.Concat(source.ProductionLocations).Distinct( |
| | | 1120 | | } |
| | | 1121 | | } |
| | | 1122 | | |
| | 266 | 1123 | | foreach (var id in source.ProviderIds) |
| | | 1124 | | { |
| | 4 | 1125 | | var key = id.Key; |
| | | 1126 | | |
| | | 1127 | | // Don't replace existing Id's. |
| | 4 | 1128 | | if (replaceData) |
| | | 1129 | | { |
| | 1 | 1130 | | target.ProviderIds[key] = id.Value; |
| | | 1131 | | } |
| | | 1132 | | else |
| | | 1133 | | { |
| | 3 | 1134 | | target.ProviderIds.TryAdd(key, id.Value); |
| | | 1135 | | } |
| | | 1136 | | } |
| | | 1137 | | |
| | 129 | 1138 | | if (replaceData || !target.CriticRating.HasValue) |
| | | 1139 | | { |
| | 128 | 1140 | | target.CriticRating = source.CriticRating; |
| | | 1141 | | } |
| | | 1142 | | |
| | 129 | 1143 | | if (replaceData || target.RemoteTrailers.Count == 0) |
| | | 1144 | | { |
| | 128 | 1145 | | target.RemoteTrailers = source.RemoteTrailers; |
| | | 1146 | | } |
| | | 1147 | | else |
| | | 1148 | | { |
| | 1 | 1149 | | target.RemoteTrailers = target.RemoteTrailers.Concat(source.RemoteTrailers).DistinctBy(t => t.Url).ToArr |
| | | 1150 | | } |
| | | 1151 | | |
| | 129 | 1152 | | MergeAlbumArtist(source, target, replaceData); |
| | 129 | 1153 | | MergeVideoInfo(source, target, replaceData); |
| | 129 | 1154 | | MergeDisplayOrder(source, target, replaceData); |
| | | 1155 | | |
| | 129 | 1156 | | if (replaceData || string.IsNullOrEmpty(target.ForcedSortName)) |
| | | 1157 | | { |
| | 128 | 1158 | | var forcedSortName = source.ForcedSortName; |
| | 128 | 1159 | | if (!string.IsNullOrEmpty(forcedSortName)) |
| | | 1160 | | { |
| | 3 | 1161 | | target.ForcedSortName = forcedSortName; |
| | | 1162 | | } |
| | | 1163 | | } |
| | | 1164 | | |
| | 129 | 1165 | | if (mergeMetadataSettings) |
| | | 1166 | | { |
| | 2 | 1167 | | if (replaceData || !target.IsLocked) |
| | | 1168 | | { |
| | 2 | 1169 | | target.IsLocked = target.IsLocked || source.IsLocked; |
| | | 1170 | | } |
| | | 1171 | | |
| | 2 | 1172 | | if (target.LockedFields.Length == 0) |
| | | 1173 | | { |
| | 0 | 1174 | | target.LockedFields = source.LockedFields; |
| | | 1175 | | } |
| | | 1176 | | else |
| | | 1177 | | { |
| | 2 | 1178 | | target.LockedFields = target.LockedFields.Concat(source.LockedFields).Distinct().ToArray(); |
| | | 1179 | | } |
| | | 1180 | | |
| | 2 | 1181 | | if (source.DateCreated != DateTime.MinValue) |
| | | 1182 | | { |
| | 1 | 1183 | | target.DateCreated = source.DateCreated; |
| | | 1184 | | } |
| | | 1185 | | |
| | 2 | 1186 | | if (replaceData || source.DateModified != DateTime.MinValue) |
| | | 1187 | | { |
| | 2 | 1188 | | target.DateModified = source.DateModified; |
| | | 1189 | | } |
| | | 1190 | | |
| | 2 | 1191 | | if (replaceData || string.IsNullOrEmpty(target.PreferredMetadataCountryCode)) |
| | | 1192 | | { |
| | 2 | 1193 | | target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode; |
| | | 1194 | | } |
| | | 1195 | | |
| | 2 | 1196 | | if (replaceData || string.IsNullOrEmpty(target.PreferredMetadataLanguage)) |
| | | 1197 | | { |
| | 2 | 1198 | | target.PreferredMetadataLanguage = source.PreferredMetadataLanguage; |
| | | 1199 | | } |
| | | 1200 | | } |
| | 129 | 1201 | | } |
| | | 1202 | | |
| | | 1203 | | private static void MergePeople(IReadOnlyList<PersonInfo> source, IReadOnlyList<PersonInfo> target) |
| | | 1204 | | { |
| | 4 | 1205 | | var sourceByName = source.ToLookup(p => p.Name.RemoveDiacritics(), StringComparer.OrdinalIgnoreCase); |
| | 4 | 1206 | | var targetByName = target.ToLookup(p => p.Name.RemoveDiacritics(), StringComparer.OrdinalIgnoreCase); |
| | | 1207 | | |
| | 16 | 1208 | | foreach (var name in targetByName.Select(g => g.Key)) |
| | | 1209 | | { |
| | 4 | 1210 | | var targetPeople = targetByName[name].ToArray(); |
| | 4 | 1211 | | var sourcePeople = sourceByName[name].ToArray(); |
| | | 1212 | | |
| | 4 | 1213 | | if (sourcePeople.Length == 0) |
| | | 1214 | | { |
| | | 1215 | | continue; |
| | | 1216 | | } |
| | | 1217 | | |
| | 12 | 1218 | | for (int i = 0; i < targetPeople.Length; i++) |
| | | 1219 | | { |
| | 3 | 1220 | | var person = targetPeople[i]; |
| | 3 | 1221 | | var personInSource = i < sourcePeople.Length ? sourcePeople[i] : sourcePeople[0]; |
| | | 1222 | | |
| | 10 | 1223 | | foreach (var providerId in personInSource.ProviderIds) |
| | | 1224 | | { |
| | 2 | 1225 | | person.ProviderIds.TryAdd(providerId.Key, providerId.Value); |
| | | 1226 | | } |
| | | 1227 | | |
| | 3 | 1228 | | if (string.IsNullOrWhiteSpace(person.ImageUrl)) |
| | | 1229 | | { |
| | 2 | 1230 | | person.ImageUrl = personInSource.ImageUrl; |
| | | 1231 | | } |
| | | 1232 | | |
| | 3 | 1233 | | if (!string.IsNullOrWhiteSpace(personInSource.Role) && string.IsNullOrWhiteSpace(person.Role)) |
| | | 1234 | | { |
| | 0 | 1235 | | person.Role = personInSource.Role; |
| | | 1236 | | } |
| | | 1237 | | |
| | 3 | 1238 | | if (personInSource.SortOrder.HasValue && !person.SortOrder.HasValue) |
| | | 1239 | | { |
| | 0 | 1240 | | person.SortOrder = personInSource.SortOrder; |
| | | 1241 | | } |
| | | 1242 | | } |
| | | 1243 | | } |
| | 4 | 1244 | | } |
| | | 1245 | | |
| | | 1246 | | private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData) |
| | | 1247 | | { |
| | 129 | 1248 | | if (source is IHasDisplayOrder sourceHasDisplayOrder |
| | 129 | 1249 | | && target is IHasDisplayOrder targetHasDisplayOrder) |
| | | 1250 | | { |
| | 49 | 1251 | | if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder)) |
| | | 1252 | | { |
| | 48 | 1253 | | var displayOrder = sourceHasDisplayOrder.DisplayOrder; |
| | 48 | 1254 | | if (!string.IsNullOrWhiteSpace(displayOrder)) |
| | | 1255 | | { |
| | 3 | 1256 | | targetHasDisplayOrder.DisplayOrder = displayOrder; |
| | | 1257 | | } |
| | | 1258 | | } |
| | | 1259 | | } |
| | 129 | 1260 | | } |
| | | 1261 | | |
| | | 1262 | | private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData) |
| | | 1263 | | { |
| | 129 | 1264 | | if (source is IHasAlbumArtist sourceHasAlbumArtist |
| | 129 | 1265 | | && target is IHasAlbumArtist targetHasAlbumArtist) |
| | | 1266 | | { |
| | 28 | 1267 | | if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0) |
| | | 1268 | | { |
| | 27 | 1269 | | targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists; |
| | | 1270 | | } |
| | 1 | 1271 | | else if (sourceHasAlbumArtist.AlbumArtists.Count > 0) |
| | | 1272 | | { |
| | 1 | 1273 | | targetHasAlbumArtist.AlbumArtists = targetHasAlbumArtist.AlbumArtists.Concat(sourceHasAlbumArtist.Al |
| | | 1274 | | } |
| | | 1275 | | } |
| | 102 | 1276 | | } |
| | | 1277 | | |
| | | 1278 | | private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData) |
| | | 1279 | | { |
| | 129 | 1280 | | if (source is Video sourceCast && target is Video targetCast) |
| | | 1281 | | { |
| | 52 | 1282 | | if (sourceCast.Video3DFormat.HasValue && (replaceData || !targetCast.Video3DFormat.HasValue)) |
| | | 1283 | | { |
| | 2 | 1284 | | targetCast.Video3DFormat = sourceCast.Video3DFormat; |
| | | 1285 | | } |
| | | 1286 | | } |
| | 129 | 1287 | | } |
| | | 1288 | | } |
| | | 1289 | | } |