MigrateDisplayPreferencesDb.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (dto == null)
  75. {
  76. continue;
  77. }
  78. var chromecastVersion = dto.CustomPrefs.TryGetValue("chromecastVersion", out var version)
  79. ? chromecastDict[version]
  80. : ChromecastVersion.Stable;
  81. var displayPreferences = new DisplayPreferences(new Guid(result[1].ToBlob()), result[2].ToString())
  82. {
  83. IndexBy = Enum.TryParse<IndexingKind>(dto.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null,
  84. ShowBackdrop = dto.ShowBackdrop,
  85. ShowSidebar = dto.ShowSidebar,
  86. ScrollDirection = dto.ScrollDirection,
  87. ChromecastVersion = chromecastVersion,
  88. SkipForwardLength = dto.CustomPrefs.TryGetValue("skipForwardLength", out var length)
  89. ? int.Parse(length, CultureInfo.InvariantCulture)
  90. : 30000,
  91. SkipBackwardLength = dto.CustomPrefs.TryGetValue("skipBackLength", out length)
  92. ? int.Parse(length, CultureInfo.InvariantCulture)
  93. : 10000,
  94. EnableNextVideoInfoOverlay = dto.CustomPrefs.TryGetValue("enableNextVideoInfoOverlay", out var enabled)
  95. ? bool.Parse(enabled)
  96. : true,
  97. DashboardTheme = dto.CustomPrefs.TryGetValue("dashboardtheme", out var theme) ? theme : string.Empty,
  98. TvHome = dto.CustomPrefs.TryGetValue("tvhome", out var home) ? home : string.Empty
  99. };
  100. for (int i = 0; i < 7; i++)
  101. {
  102. dto.CustomPrefs.TryGetValue("homesection" + i, out var homeSection);
  103. displayPreferences.HomeSections.Add(new HomeSection
  104. {
  105. Order = i,
  106. Type = Enum.TryParse<HomeSectionType>(homeSection, true, out var type) ? type : defaults[i]
  107. });
  108. }
  109. var defaultLibraryPrefs = new ItemDisplayPreferences(displayPreferences.UserId, Guid.Empty, displayPreferences.Client)
  110. {
  111. SortBy = dto.SortBy ?? "SortName",
  112. SortOrder = dto.SortOrder,
  113. RememberIndexing = dto.RememberIndexing,
  114. RememberSorting = dto.RememberSorting,
  115. };
  116. dbContext.Add(defaultLibraryPrefs);
  117. foreach (var key in dto.CustomPrefs.Keys.Where(key => key.StartsWith("landing-", StringComparison.Ordinal)))
  118. {
  119. if (!Guid.TryParse(key.AsSpan().Slice("landing-".Length), out var itemId))
  120. {
  121. continue;
  122. }
  123. var libraryDisplayPreferences = new ItemDisplayPreferences(displayPreferences.UserId, itemId, displayPreferences.Client)
  124. {
  125. SortBy = dto.SortBy ?? "SortName",
  126. SortOrder = dto.SortOrder,
  127. RememberIndexing = dto.RememberIndexing,
  128. RememberSorting = dto.RememberSorting,
  129. };
  130. if (Enum.TryParse<ViewType>(dto.ViewType, true, out var viewType))
  131. {
  132. libraryDisplayPreferences.ViewType = viewType;
  133. }
  134. dbContext.ItemDisplayPreferences.Add(libraryDisplayPreferences);
  135. }
  136. dbContext.Add(displayPreferences);
  137. }
  138. dbContext.SaveChanges();
  139. }
  140. try
  141. {
  142. File.Move(dbFilePath, dbFilePath + ".old");
  143. var journalPath = dbFilePath + "-journal";
  144. if (File.Exists(journalPath))
  145. {
  146. File.Move(journalPath, dbFilePath + ".old-journal");
  147. }
  148. }
  149. catch (IOException e)
  150. {
  151. _logger.LogError(e, "Error renaming legacy display preferences database to 'displaypreferences.db.old'");
  152. }
  153. }
  154. }
  155. }