| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Library; |
| | | 9 | | using MediaBrowser.Controller.Persistence; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Emby.Server.Implementations.Library.Validators; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Class StudiosValidator. |
| | | 16 | | /// </summary> |
| | | 17 | | public class StudiosValidator |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The library manager. |
| | | 21 | | /// </summary> |
| | | 22 | | private readonly ILibraryManager _libraryManager; |
| | | 23 | | |
| | | 24 | | private readonly IItemRepository _itemRepo; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The logger. |
| | | 28 | | /// </summary> |
| | | 29 | | private readonly ILogger<StudiosValidator> _logger; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="StudiosValidator" /> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="libraryManager">The library manager.</param> |
| | | 35 | | /// <param name="logger">The logger.</param> |
| | | 36 | | /// <param name="itemRepo">The item repository.</param> |
| | | 37 | | public StudiosValidator(ILibraryManager libraryManager, ILogger<StudiosValidator> logger, IItemRepository itemRepo) |
| | | 38 | | { |
| | 16 | 39 | | _libraryManager = libraryManager; |
| | 16 | 40 | | _logger = logger; |
| | 16 | 41 | | _itemRepo = itemRepo; |
| | 16 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Runs the specified progress. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="progress">The progress.</param> |
| | | 48 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 49 | | /// <returns>Task.</returns> |
| | | 50 | | public async Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 51 | | { |
| | 16 | 52 | | var names = _itemRepo.GetStudioNames(); |
| | 16 | 53 | | var existingStudioIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 16 | 54 | | { |
| | 16 | 55 | | IncludeItemTypes = [BaseItemKind.Studio] |
| | 16 | 56 | | }).ToHashSet(); |
| | | 57 | | |
| | 16 | 58 | | var existingStudios = _libraryManager.GetItemList(new InternalItemsQuery |
| | 16 | 59 | | { |
| | 16 | 60 | | IncludeItemTypes = [BaseItemKind.Studio] |
| | 16 | 61 | | }).Cast<Studio>() |
| | 16 | 62 | | .GroupBy(s => s.Name, StringComparer.OrdinalIgnoreCase) |
| | 16 | 63 | | .ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase); |
| | | 64 | | |
| | 16 | 65 | | var numComplete = 0; |
| | 16 | 66 | | var count = names.Count; |
| | 16 | 67 | | var refreshed = 0; |
| | | 68 | | |
| | 32 | 69 | | foreach (var name in names) |
| | | 70 | | { |
| | | 71 | | try |
| | | 72 | | { |
| | 0 | 73 | | Studio? item = null; |
| | 0 | 74 | | if (existingStudios.TryGetValue(name, out var existingStudio)) |
| | | 75 | | { |
| | 0 | 76 | | item = existingStudio; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // Fall back to GetStudio if not found (creates new item if needed) |
| | 0 | 80 | | item ??= _libraryManager.GetStudio(name); |
| | | 81 | | |
| | 0 | 82 | | if (!existingStudioIds.Contains(item.Id)) |
| | | 83 | | { |
| | 0 | 84 | | await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); |
| | 0 | 85 | | refreshed++; |
| | | 86 | | } |
| | 0 | 87 | | } |
| | 0 | 88 | | catch (OperationCanceledException) |
| | | 89 | | { |
| | | 90 | | // Don't clutter the log |
| | 0 | 91 | | throw; |
| | | 92 | | } |
| | 0 | 93 | | catch (Exception ex) |
| | | 94 | | { |
| | 0 | 95 | | _logger.LogError(ex, "Error refreshing {StudioName}", name); |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | numComplete++; |
| | 0 | 99 | | double percent = numComplete; |
| | 0 | 100 | | percent /= count; |
| | 0 | 101 | | percent *= 100; |
| | | 102 | | |
| | 0 | 103 | | progress.Report(percent); |
| | 0 | 104 | | } |
| | | 105 | | |
| | 16 | 106 | | _logger.LogInformation("Refreshed metadata for {RefreshedCount} new studios out of {TotalCount} total", refreshe |
| | | 107 | | |
| | 16 | 108 | | var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery |
| | 16 | 109 | | { |
| | 16 | 110 | | IncludeItemTypes = [BaseItemKind.Studio], |
| | 16 | 111 | | IsDeadStudio = true, |
| | 16 | 112 | | IsLocked = false |
| | 16 | 113 | | }); |
| | | 114 | | |
| | 32 | 115 | | foreach (var item in deadEntities) |
| | | 116 | | { |
| | 0 | 117 | | _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString |
| | | 118 | | } |
| | | 119 | | |
| | 16 | 120 | | _libraryManager.DeleteItemsUnsafeFast(deadEntities, deleteSourceFiles: true); |
| | | 121 | | |
| | 16 | 122 | | progress.Report(100); |
| | 16 | 123 | | } |
| | | 124 | | } |