| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 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 | | IProgress<double> subProgress = new Progress<double>((val) => progress.Report(val / 2)); |
| | | 59 | | |
| | | 60 | | _logger.LogDebug("Will refresh {Amount} people", numPeople); |
| | | 61 | | |
| | | 62 | | foreach (var person in people) |
| | | 63 | | { |
| | | 64 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | | 68 | | var item = _libraryManager.GetPerson(person); |
| | | 69 | | if (item is null) |
| | | 70 | | { |
| | | 71 | | _logger.LogWarning("Failed to get person: {Name}", person); |
| | | 72 | | continue; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | var options = new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | | 76 | | { |
| | | 77 | | ImageRefreshMode = MetadataRefreshMode.ValidationOnly, |
| | | 78 | | MetadataRefreshMode = MetadataRefreshMode.ValidationOnly |
| | | 79 | | }; |
| | | 80 | | |
| | | 81 | | await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false); |
| | | 82 | | } |
| | | 83 | | catch (OperationCanceledException) |
| | | 84 | | { |
| | | 85 | | throw; |
| | | 86 | | } |
| | | 87 | | catch (Exception ex) |
| | | 88 | | { |
| | | 89 | | _logger.LogError(ex, "Error validating IBN entry {Person}", person); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // Update progress |
| | | 93 | | numComplete++; |
| | | 94 | | double percent = numComplete; |
| | | 95 | | percent /= numPeople; |
| | | 96 | | |
| | | 97 | | subProgress.Report(100 * percent); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery |
| | | 101 | | { |
| | | 102 | | IncludeItemTypes = [BaseItemKind.Person], |
| | | 103 | | IsDeadPerson = true, |
| | | 104 | | IsLocked = false |
| | | 105 | | }); |
| | | 106 | | |
| | | 107 | | subProgress = new Progress<double>((val) => progress.Report((val / 2) + 50)); |
| | | 108 | | |
| | | 109 | | var i = 0; |
| | | 110 | | foreach (var item in deadEntities.Chunk(500)) |
| | | 111 | | { |
| | | 112 | | _libraryManager.DeleteItemsUnsafeFast(item); |
| | | 113 | | subProgress.Report(100f / deadEntities.Count * (i++ * 100)); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | progress.Report(100); |
| | | 117 | | |
| | | 118 | | _logger.LogInformation("People validation complete"); |
| | | 119 | | } |
| | | 120 | | } |