2019-01-13 20:54:44 +01:00
using System ;
2025-04-10 02:40:16 +02:00
using System.Globalization ;
2013-09-10 14:56:00 -04:00
using System.Threading ;
using System.Threading.Tasks ;
2025-04-10 02:40:16 +02:00
using Jellyfin.Data.Enums ;
using MediaBrowser.Controller.Entities ;
2019-01-13 20:21:32 +01:00
using MediaBrowser.Controller.Library ;
2016-08-19 01:58:35 -04:00
using MediaBrowser.Controller.Persistence ;
2019-01-13 20:21:32 +01:00
using Microsoft.Extensions.Logging ;
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
namespace Emby.Server.Implementations.Library.Validators ;
/// <summary>
/// Class GenresValidator.
/// </summary>
public class GenresValidator
2013-09-10 14:56:00 -04:00
{
2019-11-01 18:38:54 +01:00
/// <summary>
2025-04-30 09:29:13 +02:00
/// The library manager.
2019-11-01 18:38:54 +01:00
/// </summary>
2025-04-30 09:29:13 +02:00
private readonly ILibraryManager _libraryManager ;
private readonly IItemRepository _itemRepo ;
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
/// <summary>
/// The logger.
/// </summary>
private readonly ILogger < GenresValidator > _logger ;
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="GenresValidator"/> class.
/// </summary>
/// <param name="libraryManager">The library manager.</param>
/// <param name="logger">The logger.</param>
/// <param name="itemRepo">The item repository.</param>
public GenresValidator ( ILibraryManager libraryManager , ILogger < GenresValidator > logger , IItemRepository itemRepo )
{
_libraryManager = libraryManager ;
_logger = logger ;
_itemRepo = itemRepo ;
}
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
/// <summary>
/// Runs the specified progress.
/// </summary>
/// <param name="progress">The progress.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task Run ( IProgress < double > progress , CancellationToken cancellationToken )
{
var names = _itemRepo . GetGenreNames ( ) ;
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
var numComplete = 0 ;
var count = names . Count ;
2013-09-10 14:56:00 -04:00
2025-04-30 09:29:13 +02:00
foreach ( var name in names )
{
try
2013-09-10 14:56:00 -04:00
{
2025-04-30 09:29:13 +02:00
var item = _libraryManager . GetGenre ( name ) ;
2016-08-19 01:58:35 -04:00
2025-04-30 09:29:13 +02:00
await item . RefreshMetadata ( cancellationToken ) . ConfigureAwait ( false ) ;
2013-09-10 14:56:00 -04:00
}
2025-04-30 09:29:13 +02:00
catch ( OperationCanceledException )
2025-04-10 02:40:16 +02:00
{
2025-04-30 09:29:13 +02:00
// Don't clutter the log
throw ;
}
catch ( Exception ex )
2025-04-10 02:40:16 +02:00
{
2025-04-30 09:29:13 +02:00
_logger . LogError ( ex , "Error refreshing {GenreName}" , name ) ;
2025-04-10 02:40:16 +02:00
}
2025-04-30 09:29:13 +02:00
numComplete + + ;
double percent = numComplete ;
percent / = count ;
percent * = 100 ;
progress . Report ( percent ) ;
2013-09-10 14:56:00 -04:00
}
2025-04-30 09:29:13 +02:00
var deadEntities = _libraryManager . GetItemList ( new InternalItemsQuery
{
IncludeItemTypes = [ BaseItemKind . Genre , BaseItemKind . MusicGenre ] ,
IsDeadGenre = true ,
IsLocked = false
} ) ;
foreach ( var item in deadEntities )
{
_logger . LogInformation ( "Deleting dead {ItemType} {ItemId} {ItemName}" , item . GetType ( ) . Name , item . Id . ToString ( "N" , CultureInfo . InvariantCulture ) , item . Name ) ;
_libraryManager . DeleteItem (
item ,
new DeleteOptions
{
DeleteFileLocation = false
} ,
false ) ;
}
progress . Report ( 100 ) ;
2013-09-10 14:56:00 -04:00
}
}