| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Server.ServerSetupApp; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Library; |
| | | 9 | | using MediaBrowser.Model.Globalization; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Migration to fix broken library subtitle download languages. |
| | | 16 | | /// </summary> |
| | | 17 | | [JellyfinMigration("2026-02-06T20:00:00", nameof(FixLibrarySubtitleDownloadLanguages))] |
| | | 18 | | internal class FixLibrarySubtitleDownloadLanguages : IAsyncMigrationRoutine |
| | | 19 | | { |
| | | 20 | | private readonly ILocalizationManager _localizationManager; |
| | | 21 | | private readonly ILibraryManager _libraryManager; |
| | | 22 | | private readonly ILogger _logger; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="FixLibrarySubtitleDownloadLanguages"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="localizationManager">The Localization manager.</param> |
| | | 28 | | /// <param name="startupLogger">The startup logger for Startup UI integration.</param> |
| | | 29 | | /// <param name="libraryManager">The Library manager.</param> |
| | | 30 | | /// <param name="logger">The logger.</param> |
| | | 31 | | public FixLibrarySubtitleDownloadLanguages( |
| | | 32 | | ILocalizationManager localizationManager, |
| | | 33 | | IStartupLogger<FixLibrarySubtitleDownloadLanguages> startupLogger, |
| | | 34 | | ILibraryManager libraryManager, |
| | | 35 | | ILogger<FixLibrarySubtitleDownloadLanguages> logger) |
| | | 36 | | { |
| | 0 | 37 | | _localizationManager = localizationManager; |
| | 0 | 38 | | _libraryManager = libraryManager; |
| | 0 | 39 | | _logger = startupLogger.With(logger); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public Task PerformAsync(CancellationToken cancellationToken) |
| | | 44 | | { |
| | 0 | 45 | | _logger.LogInformation("Starting to fix library subtitle download languages."); |
| | | 46 | | |
| | 0 | 47 | | var virtualFolders = _libraryManager.GetVirtualFolders(false); |
| | | 48 | | |
| | 0 | 49 | | foreach (var virtualFolder in virtualFolders) |
| | | 50 | | { |
| | 0 | 51 | | var options = virtualFolder.LibraryOptions; |
| | 0 | 52 | | if (options?.SubtitleDownloadLanguages is null || options.SubtitleDownloadLanguages.Length == 0) |
| | | 53 | | { |
| | | 54 | | continue; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Some virtual folders don't have a proper item id. |
| | 0 | 58 | | if (!Guid.TryParse(virtualFolder.ItemId, out var folderId)) |
| | | 59 | | { |
| | | 60 | | continue; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | var collectionFolder = _libraryManager.GetItemById<CollectionFolder>(folderId); |
| | 0 | 64 | | if (collectionFolder is null) |
| | | 65 | | { |
| | 0 | 66 | | _logger.LogWarning("Could not find collection folder for virtual folder '{LibraryName}' with id '{Folder |
| | 0 | 67 | | continue; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | var fixedLanguages = new List<string>(); |
| | | 71 | | |
| | 0 | 72 | | foreach (var language in options.SubtitleDownloadLanguages) |
| | | 73 | | { |
| | 0 | 74 | | var foundLanguage = _localizationManager.FindLanguageInfo(language)?.ThreeLetterISOLanguageName; |
| | 0 | 75 | | if (foundLanguage is not null) |
| | | 76 | | { |
| | | 77 | | // Converted ISO 639-2/B to T (ger to deu) |
| | 0 | 78 | | if (!string.Equals(foundLanguage, language, StringComparison.OrdinalIgnoreCase)) |
| | | 79 | | { |
| | 0 | 80 | | _logger.LogInformation("Converted '{Language}' to '{ResolvedLanguage}' in library '{LibraryName} |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | if (fixedLanguages.Contains(foundLanguage, StringComparer.OrdinalIgnoreCase)) |
| | | 84 | | { |
| | 0 | 85 | | _logger.LogInformation("Language '{Language}' already exists for library '{LibraryName}'. Skippi |
| | 0 | 86 | | continue; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | fixedLanguages.Add(foundLanguage); |
| | | 90 | | } |
| | | 91 | | else |
| | | 92 | | { |
| | 0 | 93 | | _logger.LogInformation("Could not resolve language '{Language}' in library '{LibraryName}'. Skipping |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | options.SubtitleDownloadLanguages = [.. fixedLanguages]; |
| | 0 | 98 | | collectionFolder.UpdateLibraryOptions(options); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | _logger.LogInformation("Library subtitle download languages fixed."); |
| | | 102 | | |
| | 0 | 103 | | return Task.CompletedTask; |
| | | 104 | | } |
| | | 105 | | } |