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