| | | 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.Persistence; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace Emby.Server.Implementations.Library.Validators; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Class GenresValidator. |
| | | 15 | | /// </summary> |
| | | 16 | | public class GenresValidator |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// The library manager. |
| | | 20 | | /// </summary> |
| | | 21 | | private readonly ILibraryManager _libraryManager; |
| | | 22 | | private readonly IItemRepository _itemRepo; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The logger. |
| | | 26 | | /// </summary> |
| | | 27 | | private readonly ILogger<GenresValidator> _logger; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="GenresValidator"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="libraryManager">The library manager.</param> |
| | | 33 | | /// <param name="logger">The logger.</param> |
| | | 34 | | /// <param name="itemRepo">The item repository.</param> |
| | | 35 | | public GenresValidator(ILibraryManager libraryManager, ILogger<GenresValidator> logger, IItemRepository itemRepo) |
| | | 36 | | { |
| | 17 | 37 | | _libraryManager = libraryManager; |
| | 17 | 38 | | _logger = logger; |
| | 17 | 39 | | _itemRepo = itemRepo; |
| | 17 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Runs the specified progress. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="progress">The progress.</param> |
| | | 46 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 47 | | /// <returns>Task.</returns> |
| | | 48 | | public async Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 49 | | { |
| | | 50 | | var names = _itemRepo.GetGenreNames(); |
| | | 51 | | |
| | | 52 | | var numComplete = 0; |
| | | 53 | | var count = names.Count; |
| | | 54 | | |
| | | 55 | | foreach (var name in names) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | | 59 | | var item = _libraryManager.GetGenre(name); |
| | | 60 | | |
| | | 61 | | await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); |
| | | 62 | | } |
| | | 63 | | catch (OperationCanceledException) |
| | | 64 | | { |
| | | 65 | | // Don't clutter the log |
| | | 66 | | throw; |
| | | 67 | | } |
| | | 68 | | catch (Exception ex) |
| | | 69 | | { |
| | | 70 | | _logger.LogError(ex, "Error refreshing {GenreName}", name); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | numComplete++; |
| | | 74 | | double percent = numComplete; |
| | | 75 | | percent /= count; |
| | | 76 | | percent *= 100; |
| | | 77 | | |
| | | 78 | | progress.Report(percent); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery |
| | | 82 | | { |
| | | 83 | | IncludeItemTypes = [BaseItemKind.Genre, BaseItemKind.MusicGenre], |
| | | 84 | | IsDeadGenre = true, |
| | | 85 | | IsLocked = false |
| | | 86 | | }); |
| | | 87 | | |
| | | 88 | | foreach (var item in deadEntities) |
| | | 89 | | { |
| | | 90 | | _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString |
| | | 91 | | |
| | | 92 | | _libraryManager.DeleteItem( |
| | | 93 | | item, |
| | | 94 | | new DeleteOptions |
| | | 95 | | { |
| | | 96 | | DeleteFileLocation = false |
| | | 97 | | }, |
| | | 98 | | false); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | progress.Report(100); |
| | | 102 | | } |
| | | 103 | | } |