| | | 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.Entities.Audio; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using MediaBrowser.Controller.Persistence; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace Emby.Server.Implementations.Library.Validators; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Class ArtistsValidator. |
| | | 17 | | /// </summary> |
| | | 18 | | public class ArtistsValidator |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The library manager. |
| | | 22 | | /// </summary> |
| | | 23 | | private readonly ILibraryManager _libraryManager; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The logger. |
| | | 27 | | /// </summary> |
| | | 28 | | private readonly ILogger<ArtistsValidator> _logger; |
| | | 29 | | private readonly IItemRepository _itemRepo; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="ArtistsValidator" /> 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 ArtistsValidator(ILibraryManager libraryManager, ILogger<ArtistsValidator> logger, IItemRepository itemRepo) |
| | | 38 | | { |
| | 17 | 39 | | _libraryManager = libraryManager; |
| | 17 | 40 | | _logger = logger; |
| | 17 | 41 | | _itemRepo = itemRepo; |
| | 17 | 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 | | { |
| | | 52 | | var names = _itemRepo.GetAllArtistNames(); |
| | | 53 | | |
| | | 54 | | var numComplete = 0; |
| | | 55 | | var count = names.Count; |
| | | 56 | | |
| | | 57 | | foreach (var name in names) |
| | | 58 | | { |
| | | 59 | | try |
| | | 60 | | { |
| | | 61 | | var item = _libraryManager.GetArtist(name); |
| | | 62 | | |
| | | 63 | | await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); |
| | | 64 | | } |
| | | 65 | | catch (OperationCanceledException) |
| | | 66 | | { |
| | | 67 | | // Don't clutter the log |
| | | 68 | | throw; |
| | | 69 | | } |
| | | 70 | | catch (Exception ex) |
| | | 71 | | { |
| | | 72 | | _logger.LogError(ex, "Error refreshing {ArtistName}", name); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | numComplete++; |
| | | 76 | | double percent = numComplete; |
| | | 77 | | percent /= count; |
| | | 78 | | percent *= 100; |
| | | 79 | | |
| | | 80 | | progress.Report(percent); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery |
| | | 84 | | { |
| | | 85 | | IncludeItemTypes = [BaseItemKind.MusicArtist], |
| | | 86 | | IsDeadArtist = true, |
| | | 87 | | IsLocked = false |
| | | 88 | | }).Cast<MusicArtist>().ToList(); |
| | | 89 | | |
| | | 90 | | foreach (var item in deadEntities) |
| | | 91 | | { |
| | | 92 | | if (!item.IsAccessedByName) |
| | | 93 | | { |
| | | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString |
| | | 98 | | |
| | | 99 | | _libraryManager.DeleteItem( |
| | | 100 | | item, |
| | | 101 | | new DeleteOptions |
| | | 102 | | { |
| | | 103 | | DeleteFileLocation = false |
| | | 104 | | }, |
| | | 105 | | false); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | progress.Report(100); |
| | | 109 | | } |
| | | 110 | | } |