RenameEnableGroupingIntoCollections.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Emby.Server.Implementations;
  7. using Microsoft.Extensions.Logging;
  8. namespace Jellyfin.Server.Migrations.PreStartupRoutines;
  9. /// <inheritdoc />
  10. public class RenameEnableGroupingIntoCollections : IMigrationRoutine
  11. {
  12. private readonly ServerApplicationPaths _applicationPaths;
  13. private readonly ILogger<RenameEnableGroupingIntoCollections> _logger;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="RenameEnableGroupingIntoCollections"/> class.
  16. /// </summary>
  17. /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
  18. /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
  19. public RenameEnableGroupingIntoCollections(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
  20. {
  21. _applicationPaths = applicationPaths;
  22. _logger = loggerFactory.CreateLogger<RenameEnableGroupingIntoCollections>();
  23. }
  24. /// <inheritdoc />
  25. public Guid Id => Guid.Parse("E73B777D-CD5C-4E71-957A-B86B3660B7CF");
  26. /// <inheritdoc />
  27. public string Name => nameof(RenameEnableGroupingIntoCollections);
  28. /// <inheritdoc />
  29. public bool PerformOnNewInstall => false;
  30. /// <inheritdoc />
  31. public void Perform()
  32. {
  33. string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "system.xml");
  34. if (!File.Exists(path))
  35. {
  36. _logger.LogWarning("Configuration file not found: {Path}", path);
  37. return;
  38. }
  39. try
  40. {
  41. XDocument xmlDocument = XDocument.Load(path);
  42. var element = xmlDocument.Descendants("EnableGroupingIntoCollections").FirstOrDefault();
  43. if (element is not null)
  44. {
  45. element.Name = "EnableGroupingMoviesIntoCollections";
  46. _logger.LogInformation("The tag <EnableGroupingIntoCollections> was successfully renamed to <EnableGroupingMoviesIntoCollections>.");
  47. xmlDocument.Save(path);
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. _logger.LogError(ex, "An error occurred while updating the XML file: {Message}", ex.Message);
  53. }
  54. }
  55. }