MigrateDisplayPreferencesDb.cs 7.4 KB

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