| | | 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.Persistence; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace Emby.Server.Implementations.Library.Validators; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Class MusicGenresValidator. |
| | | 15 | | /// </summary> |
| | | 16 | | public class MusicGenresValidator |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// The library manager. |
| | | 20 | | /// </summary> |
| | | 21 | | private readonly ILibraryManager _libraryManager; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The logger. |
| | | 25 | | /// </summary> |
| | | 26 | | private readonly ILogger<MusicGenresValidator> _logger; |
| | | 27 | | private readonly IItemRepository _itemRepo; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="MusicGenresValidator" /> 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 MusicGenresValidator(ILibraryManager libraryManager, ILogger<MusicGenresValidator> logger, IItemRepository it |
| | | 36 | | { |
| | 16 | 37 | | _libraryManager = libraryManager; |
| | 16 | 38 | | _logger = logger; |
| | 16 | 39 | | _itemRepo = itemRepo; |
| | 16 | 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 | | { |
| | 16 | 50 | | var names = _itemRepo.GetMusicGenreNames(); |
| | 16 | 51 | | var existingMusicGenreIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 16 | 52 | | { |
| | 16 | 53 | | IncludeItemTypes = [BaseItemKind.MusicGenre] |
| | 16 | 54 | | }).ToHashSet(); |
| | | 55 | | |
| | 16 | 56 | | var numComplete = 0; |
| | 16 | 57 | | var count = names.Count; |
| | 16 | 58 | | var refreshed = 0; |
| | | 59 | | |
| | 32 | 60 | | foreach (var name in names) |
| | | 61 | | { |
| | | 62 | | try |
| | | 63 | | { |
| | 0 | 64 | | var item = _libraryManager.GetMusicGenre(name); |
| | 0 | 65 | | if (!existingMusicGenreIds.Contains(item.Id)) |
| | | 66 | | { |
| | 0 | 67 | | await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); |
| | 0 | 68 | | refreshed++; |
| | | 69 | | } |
| | 0 | 70 | | } |
| | 0 | 71 | | catch (OperationCanceledException) |
| | | 72 | | { |
| | | 73 | | // Don't clutter the log |
| | 0 | 74 | | throw; |
| | | 75 | | } |
| | 0 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 0 | 78 | | _logger.LogError(ex, "Error refreshing {GenreName}", name); |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | numComplete++; |
| | 0 | 82 | | double percent = numComplete; |
| | 0 | 83 | | percent /= count; |
| | 0 | 84 | | percent *= 100; |
| | | 85 | | |
| | 0 | 86 | | progress.Report(percent); |
| | 0 | 87 | | } |
| | | 88 | | |
| | 16 | 89 | | _logger.LogInformation("Refreshed metadata for {RefreshedCount} new music genres out of {TotalCount} total", ref |
| | | 90 | | |
| | 16 | 91 | | progress.Report(100); |
| | 16 | 92 | | } |
| | | 93 | | } |