| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Jellyfin.Data.Enums; |
| | 6 | | using MediaBrowser.Controller.Entities; |
| | 7 | | using MediaBrowser.Controller.Library; |
| | 8 | | using MediaBrowser.Controller.Providers; |
| | 9 | | using MediaBrowser.Model.IO; |
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | |
|
| | 12 | | namespace Emby.Server.Implementations.Library.Validators; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Class PeopleValidator. |
| | 16 | | /// </summary> |
| | 17 | | public class PeopleValidator |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The _library manager. |
| | 21 | | /// </summary> |
| | 22 | | private readonly ILibraryManager _libraryManager; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// The _logger. |
| | 26 | | /// </summary> |
| | 27 | | private readonly ILogger _logger; |
| | 28 | |
|
| | 29 | | private readonly IFileSystem _fileSystem; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Initializes a new instance of the <see cref="PeopleValidator" /> class. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="libraryManager">The library manager.</param> |
| | 35 | | /// <param name="logger">The logger.</param> |
| | 36 | | /// <param name="fileSystem">The file system.</param> |
| | 37 | | public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem) |
| | 38 | | { |
| 0 | 39 | | _libraryManager = libraryManager; |
| 0 | 40 | | _logger = logger; |
| 0 | 41 | | _fileSystem = fileSystem; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Validates the people. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 48 | | /// <param name="progress">The progress.</param> |
| | 49 | | /// <returns>Task.</returns> |
| | 50 | | public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress) |
| | 51 | | { |
| | 52 | | var people = _libraryManager.GetPeopleNames(new InternalPeopleQuery()); |
| | 53 | |
|
| | 54 | | var numComplete = 0; |
| | 55 | |
|
| | 56 | | var numPeople = people.Count; |
| | 57 | |
|
| | 58 | | _logger.LogDebug("Will refresh {Amount} people", numPeople); |
| | 59 | |
|
| | 60 | | foreach (var person in people) |
| | 61 | | { |
| | 62 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 63 | |
|
| | 64 | | try |
| | 65 | | { |
| | 66 | | var item = _libraryManager.GetPerson(person); |
| | 67 | | if (item is null) |
| | 68 | | { |
| | 69 | | _logger.LogWarning("Failed to get person: {Name}", person); |
| | 70 | | continue; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | var options = new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | 74 | | { |
| | 75 | | ImageRefreshMode = MetadataRefreshMode.ValidationOnly, |
| | 76 | | MetadataRefreshMode = MetadataRefreshMode.ValidationOnly |
| | 77 | | }; |
| | 78 | |
|
| | 79 | | await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false); |
| | 80 | | } |
| | 81 | | catch (OperationCanceledException) |
| | 82 | | { |
| | 83 | | throw; |
| | 84 | | } |
| | 85 | | catch (Exception ex) |
| | 86 | | { |
| | 87 | | _logger.LogError(ex, "Error validating IBN entry {Person}", person); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // Update progress |
| | 91 | | numComplete++; |
| | 92 | | double percent = numComplete; |
| | 93 | | percent /= numPeople; |
| | 94 | |
|
| | 95 | | progress.Report(100 * percent); |
| | 96 | | } |
| | 97 | |
|
| | 98 | | var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery |
| | 99 | | { |
| | 100 | | IncludeItemTypes = [BaseItemKind.Person], |
| | 101 | | IsDeadPerson = true, |
| | 102 | | IsLocked = false |
| | 103 | | }); |
| | 104 | |
|
| | 105 | | foreach (var item in deadEntities) |
| | 106 | | { |
| | 107 | | _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString |
| | 108 | |
|
| | 109 | | _libraryManager.DeleteItem( |
| | 110 | | item, |
| | 111 | | new DeleteOptions |
| | 112 | | { |
| | 113 | | DeleteFileLocation = false |
| | 114 | | }, |
| | 115 | | false); |
| | 116 | | } |
| | 117 | |
|
| | 118 | | progress.Report(100); |
| | 119 | |
|
| | 120 | | _logger.LogInformation("People validation complete"); |
| | 121 | | } |
| | 122 | | } |