MigrateDisplayPreferencesDb.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text.Json;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Data.Entities;
  7. using Jellyfin.Data.Enums;
  8. using Jellyfin.Server.Implementations;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Model.Entities;
  11. using Microsoft.Extensions.Logging;
  12. using SQLitePCL.pretty;
  13. namespace Jellyfin.Server.Migrations.Routines
  14. {
  15. /// <summary>
  16. /// The migration routine for migrating the display preferences database to EF Core.
  17. /// </summary>
  18. public class MigrateDisplayPreferencesDb : IMigrationRoutine
  19. {
  20. private const string DbFilename = "displaypreferences.db";
  21. private readonly ILogger<MigrateDisplayPreferencesDb> _logger;
  22. private readonly IServerApplicationPaths _paths;
  23. private readonly JellyfinDbProvider _provider;
  24. private readonly JsonSerializerOptions _jsonOptions;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="MigrateDisplayPreferencesDb"/> class.
  27. /// </summary>
  28. /// <param name="logger">The logger.</param>
  29. /// <param name="paths">The server application paths.</param>
  30. /// <param name="provider">The database provider.</param>
  31. public MigrateDisplayPreferencesDb(ILogger<MigrateDisplayPreferencesDb> logger, IServerApplicationPaths paths, JellyfinDbProvider provider)
  32. {
  33. _logger = logger;
  34. _paths = paths;
  35. _provider = provider;
  36. _jsonOptions = new JsonSerializerOptions();
  37. _jsonOptions.Converters.Add(new JsonStringEnumConverter());
  38. }
  39. /// <inheritdoc />
  40. public Guid Id => Guid.Parse("06387815-C3CC-421F-A888-FB5F9992BEA8");
  41. /// <inheritdoc />
  42. public string Name => "MigrateDisplayPreferencesDatabase";
  43. /// <inheritdoc />
  44. public bool PerformOnNewInstall => false;
  45. /// <inheritdoc />
  46. public void Perform()
  47. {
  48. HomeSectionType[] defaults =
  49. {
  50. HomeSectionType.SmallLibraryTiles,
  51. HomeSectionType.Resume,
  52. HomeSectionType.ResumeAudio,
  53. HomeSectionType.LiveTv,
  54. HomeSectionType.NextUp,
  55. HomeSectionType.LatestMedia,
  56. HomeSectionType.None,
  57. };
  58. var dbFilePath = Path.Combine(_paths.DataPath, DbFilename);
  59. using (var connection = SQLite3.Open(dbFilePath, ConnectionFlags.ReadOnly, null))
  60. {
  61. var dbContext = _provider.CreateContext();
  62. var results = connection.Query("SELECT * FROM userdisplaypreferences");
  63. foreach (var result in results)
  64. {
  65. var dto = JsonSerializer.Deserialize<DisplayPreferencesDto>(result[3].ToString(), _jsonOptions);
  66. var chromecastVersion = dto.CustomPrefs.TryGetValue("chromecastVersion", out var version)
  67. ? Enum.TryParse<ChromecastVersion>(version, true, out var parsed)
  68. ? parsed
  69. : ChromecastVersion.Stable
  70. : ChromecastVersion.Stable;
  71. var displayPreferences = new DisplayPreferences(result[2].ToString(), new Guid(result[1].ToBlob()))
  72. {
  73. ViewType = Enum.TryParse<ViewType>(dto.ViewType, true, out var viewType) ? viewType : (ViewType?)null,
  74. IndexBy = Enum.TryParse<IndexingKind>(dto.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null,
  75. ShowBackdrop = dto.ShowBackdrop,
  76. ShowSidebar = dto.ShowSidebar,
  77. SortBy = dto.SortBy,
  78. SortOrder = dto.SortOrder,
  79. RememberIndexing = dto.RememberIndexing,
  80. RememberSorting = dto.RememberSorting,
  81. ScrollDirection = dto.ScrollDirection,
  82. ChromecastVersion = chromecastVersion,
  83. SkipForwardLength = dto.CustomPrefs.TryGetValue("skipForwardLength", out var length)
  84. ? int.Parse(length, CultureInfo.InvariantCulture)
  85. : 30000,
  86. SkipBackwardLength = dto.CustomPrefs.TryGetValue("skipBackLength", out length)
  87. ? int.Parse(length, CultureInfo.InvariantCulture)
  88. : 30000
  89. };
  90. for (int i = 0; i < 7; i++)
  91. {
  92. dto.CustomPrefs.TryGetValue("homesection" + i, out var homeSection);
  93. displayPreferences.HomeSections.Add(new HomeSection
  94. {
  95. Order = i,
  96. Type = Enum.TryParse<HomeSectionType>(homeSection, true, out var type) ? type : defaults[i]
  97. });
  98. }
  99. dbContext.Add(displayPreferences);
  100. }
  101. dbContext.SaveChanges();
  102. }
  103. try
  104. {
  105. File.Move(dbFilePath, dbFilePath + ".old");
  106. var journalPath = dbFilePath + "-journal";
  107. if (File.Exists(journalPath))
  108. {
  109. File.Move(journalPath, dbFilePath + ".old-journal");
  110. }
  111. }
  112. catch (IOException e)
  113. {
  114. _logger.LogError(e, "Error renaming legacy display preferences database to 'displaypreferences.db.old'");
  115. }
  116. }
  117. }
  118. }