MigrateDisplayPreferencesDb.cs 7.2 KB

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