| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Xml; |
| | | 6 | | using System.Xml.Linq; |
| | | 7 | | using Jellyfin.Server.Migrations.Stages; |
| | | 8 | | using MediaBrowser.Common.Configuration; |
| | | 9 | | using MediaBrowser.Model.Configuration; |
| | | 10 | | using MediaBrowser.Model.Entities; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Replaces the nullable encoder preset written by older versions with the default preset. |
| | | 17 | | /// </summary> |
| | | 18 | | [JellyfinMigration("2026-09-15T10:43:05", nameof(FixNullEncoderPreset), Stage = JellyfinMigrationStageTypes.PreInitialis |
| | | 19 | | internal class FixNullEncoderPreset : IAsyncMigrationRoutine |
| | | 20 | | { |
| | 1 | 21 | | private static readonly XNamespace _xsiNamespace = "http://www.w3.org/2001/XMLSchema-instance"; |
| | | 22 | | |
| | | 23 | | private readonly IApplicationPaths _applicationPaths; |
| | | 24 | | private readonly ILogger<FixNullEncoderPreset> _logger; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="FixNullEncoderPreset"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="applicationPaths">The application paths.</param> |
| | | 30 | | /// <param name="logger">The logger.</param> |
| | | 31 | | public FixNullEncoderPreset(IApplicationPaths applicationPaths, ILogger<FixNullEncoderPreset> logger) |
| | | 32 | | { |
| | 4 | 33 | | _applicationPaths = applicationPaths; |
| | 4 | 34 | | _logger = logger; |
| | 4 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public Task PerformAsync(CancellationToken cancellationToken) |
| | | 39 | | { |
| | 3 | 40 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 41 | | |
| | 3 | 42 | | var path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "encoding.xml"); |
| | 3 | 43 | | if (!File.Exists(path)) |
| | | 44 | | { |
| | 0 | 45 | | return Task.CompletedTask; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | XDocument document; |
| | | 49 | | try |
| | | 50 | | { |
| | 3 | 51 | | document = XDocument.Load(path, LoadOptions.PreserveWhitespace); |
| | 2 | 52 | | } |
| | 1 | 53 | | catch (XmlException ex) |
| | | 54 | | { |
| | 1 | 55 | | _logger.LogWarning(ex, "Could not parse encoding configuration; skipping null encoder preset migration"); |
| | 1 | 56 | | return Task.CompletedTask; |
| | | 57 | | } |
| | | 58 | | |
| | 2 | 59 | | var encoderPreset = document.Root?.Element(nameof(EncodingOptions.EncoderPreset)); |
| | 2 | 60 | | if (encoderPreset is null) |
| | | 61 | | { |
| | 0 | 62 | | return Task.CompletedTask; |
| | | 63 | | } |
| | | 64 | | |
| | 2 | 65 | | var nilAttribute = encoderPreset.Attribute(_xsiNamespace + "nil"); |
| | 2 | 66 | | if (!string.Equals(nilAttribute?.Value, "true", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(enc |
| | | 67 | | { |
| | 1 | 68 | | return Task.CompletedTask; |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | nilAttribute?.Remove(); |
| | 1 | 72 | | encoderPreset.Value = nameof(EncoderPreset.auto); |
| | 1 | 73 | | document.Save(path, SaveOptions.DisableFormatting); |
| | 1 | 74 | | _logger.LogInformation("Replaced null encoder preset with auto in encoding configuration"); |
| | 1 | 75 | | return Task.CompletedTask; |
| | 1 | 76 | | } |
| | | 77 | | } |