| | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections.Immutable; |
| | 6 | | using System.Data; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.IO; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Text; |
| | 12 | | using Emby.Server.Implementations.Data; |
| | 13 | | using Jellyfin.Database.Implementations; |
| | 14 | | using Jellyfin.Database.Implementations.Entities; |
| | 15 | | using Jellyfin.Extensions; |
| | 16 | | using Jellyfin.Server.Implementations.Item; |
| | 17 | | using MediaBrowser.Controller; |
| | 18 | | using MediaBrowser.Controller.Entities; |
| | 19 | | using MediaBrowser.Model.Entities; |
| | 20 | | using Microsoft.Data.Sqlite; |
| | 21 | | using Microsoft.EntityFrameworkCore; |
| | 22 | | using Microsoft.Extensions.Logging; |
| | 23 | | using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity; |
| | 24 | | using Chapter = Jellyfin.Database.Implementations.Entities.Chapter; |
| | 25 | |
|
| | 26 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The migration routine for migrating the userdata database to EF Core. |
| | 30 | | /// </summary> |
| | 31 | | [JellyfinMigration("2025-04-20T20:00:00", nameof(MigrateLibraryDb))] |
| | 32 | | internal class MigrateLibraryDb : IDatabaseMigrationRoutine |
| | 33 | | { |
| | 34 | | private const string DbFilename = "library.db"; |
| | 35 | |
|
| | 36 | | private readonly ILogger<MigrateLibraryDb> _logger; |
| | 37 | | private readonly IServerApplicationPaths _paths; |
| | 38 | | private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider; |
| | 39 | | private readonly IDbContextFactory<JellyfinDbContext> _provider; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Initializes a new instance of the <see cref="MigrateLibraryDb"/> class. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="logger">The logger.</param> |
| | 45 | | /// <param name="provider">The database provider.</param> |
| | 46 | | /// <param name="paths">The server application paths.</param> |
| | 47 | | /// <param name="jellyfinDatabaseProvider">The database provider for special access.</param> |
| | 48 | | /// <param name="serviceProvider">The Service provider.</param> |
| | 49 | | public MigrateLibraryDb( |
| | 50 | | ILogger<MigrateLibraryDb> logger, |
| | 51 | | IDbContextFactory<JellyfinDbContext> provider, |
| | 52 | | IServerApplicationPaths paths, |
| | 53 | | IJellyfinDatabaseProvider jellyfinDatabaseProvider, |
| | 54 | | IServiceProvider serviceProvider) |
| | 55 | | { |
| 0 | 56 | | _logger = logger; |
| 0 | 57 | | _provider = provider; |
| 0 | 58 | | _paths = paths; |
| 0 | 59 | | _jellyfinDatabaseProvider = jellyfinDatabaseProvider; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <inheritdoc/> |
| | 63 | | public void Perform() |
| | 64 | | { |
| 0 | 65 | | _logger.LogInformation("Migrating the userdata from library.db may take a while, do not stop Jellyfin."); |
| | 66 | |
|
| 0 | 67 | | var dataPath = _paths.DataPath; |
| 0 | 68 | | var libraryDbPath = Path.Combine(dataPath, DbFilename); |
| 0 | 69 | | if (!File.Exists(libraryDbPath)) |
| | 70 | | { |
| 0 | 71 | | _logger.LogError("Cannot migrate {LibraryDb} as it does not exist..", libraryDbPath); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | using var connection = new SqliteConnection($"Filename={libraryDbPath};Mode=ReadOnly"); |
| | 76 | |
|
| 0 | 77 | | var fullOperationTimer = new Stopwatch(); |
| 0 | 78 | | fullOperationTimer.Start(); |
| | 79 | |
|
| 0 | 80 | | using (var operation = GetPreparedDbContext("Cleanup database")) |
| | 81 | | { |
| 0 | 82 | | operation.JellyfinDbContext.AttachmentStreamInfos.ExecuteDelete(); |
| 0 | 83 | | operation.JellyfinDbContext.BaseItems.ExecuteDelete(); |
| 0 | 84 | | operation.JellyfinDbContext.ItemValues.ExecuteDelete(); |
| 0 | 85 | | operation.JellyfinDbContext.UserData.ExecuteDelete(); |
| 0 | 86 | | operation.JellyfinDbContext.MediaStreamInfos.ExecuteDelete(); |
| 0 | 87 | | operation.JellyfinDbContext.Peoples.ExecuteDelete(); |
| 0 | 88 | | operation.JellyfinDbContext.PeopleBaseItemMap.ExecuteDelete(); |
| 0 | 89 | | operation.JellyfinDbContext.Chapters.ExecuteDelete(); |
| 0 | 90 | | operation.JellyfinDbContext.AncestorIds.ExecuteDelete(); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | var legacyBaseItemWithUserKeys = new Dictionary<string, BaseItemEntity>(); |
| 0 | 94 | | connection.Open(); |
| | 95 | |
|
| 0 | 96 | | var baseItemIds = new HashSet<Guid>(); |
| 0 | 97 | | using (var operation = GetPreparedDbContext("moving TypedBaseItem")) |
| | 98 | | { |
| | 99 | | const string typedBaseItemsQuery = |
| | 100 | | """ |
| | 101 | | SELECT guid, type, data, StartDate, EndDate, ChannelId, IsMovie, |
| | 102 | | IsSeries, EpisodeTitle, IsRepeat, CommunityRating, CustomRating, IndexNumber, IsLocked, PreferredMetadataLan |
| | 103 | | PreferredMetadataCountryCode, Width, Height, DateLastRefreshed, Name, Path, PremiereDate, Overview, ParentIn |
| | 104 | | ProductionYear, OfficialRating, ForcedSortName, RunTimeTicks, Size, DateCreated, DateModified, Genres, Paren |
| | 105 | | Audio, ExternalServiceId, IsInMixedFolder, DateLastSaved, LockedFields, Studios, Tags, TrailerTypes, Origina |
| | 106 | | DateLastMediaAdded, Album, LUFS, NormalizationGain, CriticRating, IsVirtualItem, SeriesName, UserDataKey, Se |
| | 107 | | PresentationUniqueKey, InheritedParentalRatingValue, ExternalSeriesId, Tagline, ProviderIds, Images, Product |
| | 108 | | ExtraType, Artists, AlbumArtists, ExternalId, SeriesPresentationUniqueKey, ShowId, OwnerId, MediaType, SortN |
| | 109 | | """; |
| 0 | 110 | | using (new TrackedMigrationStep("Loading TypedBaseItems", _logger)) |
| | 111 | | { |
| 0 | 112 | | foreach (SqliteDataReader dto in connection.Query(typedBaseItemsQuery)) |
| | 113 | | { |
| 0 | 114 | | var baseItem = GetItem(dto); |
| 0 | 115 | | operation.JellyfinDbContext.BaseItems.Add(baseItem.BaseItem); |
| 0 | 116 | | baseItemIds.Add(baseItem.BaseItem.Id); |
| 0 | 117 | | foreach (var dataKey in baseItem.LegacyUserDataKey) |
| | 118 | | { |
| 0 | 119 | | legacyBaseItemWithUserKeys[dataKey] = baseItem.BaseItem; |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.BaseItems.Local.Count} BaseItem entrie |
| | 125 | | { |
| 0 | 126 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | using (var operation = GetPreparedDbContext("moving ItemValues")) |
| | 131 | | { |
| | 132 | | // do not migrate inherited types as they are now properly mapped in search and lookup. |
| | 133 | | const string itemValueQuery = |
| | 134 | | """ |
| | 135 | | SELECT ItemId, Type, Value, CleanValue FROM ItemValues |
| | 136 | | WHERE Type <> 6 AND EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = ItemValues.I |
| | 137 | | """; |
| | 138 | |
|
| | 139 | | // EFCores local lookup sucks. We cannot use context.ItemValues.Local here because its just super slow. |
| 0 | 140 | | var localItems = new Dictionary<(int Type, string Value), (Database.Implementations.Entities.ItemValue ItemV |
| 0 | 141 | | using (new TrackedMigrationStep("loading ItemValues", _logger)) |
| | 142 | | { |
| 0 | 143 | | foreach (SqliteDataReader dto in connection.Query(itemValueQuery)) |
| | 144 | | { |
| 0 | 145 | | var itemId = dto.GetGuid(0); |
| 0 | 146 | | var entity = GetItemValue(dto); |
| 0 | 147 | | var key = ((int)entity.Type, entity.Value); |
| 0 | 148 | | if (!localItems.TryGetValue(key, out var existing)) |
| | 149 | | { |
| 0 | 150 | | localItems[key] = existing = (entity, []); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | existing.ItemIds.Add(itemId); |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | foreach (var item in localItems) |
| | 157 | | { |
| 0 | 158 | | operation.JellyfinDbContext.ItemValues.Add(item.Value.ItemValue); |
| 0 | 159 | | operation.JellyfinDbContext.ItemValuesMap.AddRange(item.Value.ItemIds.Distinct().Select(f => new Ite |
| 0 | 160 | | { |
| 0 | 161 | | Item = null!, |
| 0 | 162 | | ItemValue = null!, |
| 0 | 163 | | ItemId = f, |
| 0 | 164 | | ItemValueId = item.Value.ItemValue.ItemValueId |
| 0 | 165 | | })); |
| | 166 | | } |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.ItemValues.Local.Count} ItemValues ent |
| | 170 | | { |
| 0 | 171 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 172 | | } |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | using (var operation = GetPreparedDbContext("moving UserData")) |
| | 176 | | { |
| 0 | 177 | | var queryResult = connection.Query( |
| 0 | 178 | | """ |
| 0 | 179 | | SELECT key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStrea |
| 0 | 180 | |
|
| 0 | 181 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.UserDataKey = UserDatas.key) |
| 0 | 182 | | """); |
| | 183 | |
|
| 0 | 184 | | using (new TrackedMigrationStep("loading UserData", _logger)) |
| | 185 | | { |
| 0 | 186 | | var users = operation.JellyfinDbContext.Users.AsNoTracking().ToImmutableArray(); |
| 0 | 187 | | var userIdBlacklist = new HashSet<int>(); |
| | 188 | |
|
| 0 | 189 | | foreach (var entity in queryResult) |
| | 190 | | { |
| 0 | 191 | | var userData = GetUserData(users, entity, userIdBlacklist); |
| 0 | 192 | | if (userData is null) |
| | 193 | | { |
| 0 | 194 | | var userDataId = entity.GetString(0); |
| 0 | 195 | | var internalUserId = entity.GetInt32(1); |
| | 196 | |
|
| 0 | 197 | | if (!userIdBlacklist.Contains(internalUserId)) |
| | 198 | | { |
| 0 | 199 | | _logger.LogError("Was not able to migrate user data with key {0} because its id {InternalId} |
| 0 | 200 | | userIdBlacklist.Add(internalUserId); |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | continue; |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | if (!legacyBaseItemWithUserKeys.TryGetValue(userData.CustomDataKey!, out var refItem)) |
| | 207 | | { |
| 0 | 208 | | _logger.LogError("Was not able to migrate user data with key {0} because it does not reference a |
| 0 | 209 | | continue; |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | userData.ItemId = refItem.Id; |
| 0 | 213 | | operation.JellyfinDbContext.UserData.Add(userData); |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | users.Clear(); |
| 0 | 217 | | } |
| | 218 | |
|
| 0 | 219 | | legacyBaseItemWithUserKeys.Clear(); |
| | 220 | |
|
| 0 | 221 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.UserData.Local.Count} UserData entries |
| | 222 | | { |
| 0 | 223 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 224 | | } |
| | 225 | | } |
| | 226 | |
|
| 0 | 227 | | using (var operation = GetPreparedDbContext("moving MediaStreamInfos")) |
| | 228 | | { |
| | 229 | | const string mediaStreamQuery = |
| | 230 | | """ |
| | 231 | | SELECT ItemId, StreamIndex, StreamType, Codec, Language, ChannelLayout, Profile, AspectRatio, Path, |
| | 232 | | IsInterlaced, BitRate, Channels, SampleRate, IsDefault, IsForced, IsExternal, Height, Width, |
| | 233 | | AverageFrameRate, RealFrameRate, Level, PixelFormat, BitDepth, IsAnamorphic, RefFrames, CodecTag, |
| | 234 | | Comment, NalLengthSize, IsAvc, Title, TimeBase, CodecTimeBase, ColorPrimaries, ColorSpace, ColorTransfer, |
| | 235 | | DvVersionMajor, DvVersionMinor, DvProfile, DvLevel, RpuPresentFlag, ElPresentFlag, BlPresentFlag, DvBlSignal |
| | 236 | | FROM MediaStreams |
| | 237 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = MediaStreams.ItemId) |
| | 238 | | """; |
| | 239 | |
|
| 0 | 240 | | using (new TrackedMigrationStep("loading MediaStreamInfos", _logger)) |
| | 241 | | { |
| 0 | 242 | | foreach (SqliteDataReader dto in connection.Query(mediaStreamQuery)) |
| | 243 | | { |
| 0 | 244 | | operation.JellyfinDbContext.MediaStreamInfos.Add(GetMediaStream(dto)); |
| | 245 | | } |
| | 246 | | } |
| | 247 | |
|
| 0 | 248 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.MediaStreamInfos.Local.Count} MediaStr |
| | 249 | | { |
| 0 | 250 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 251 | | } |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | using (var operation = GetPreparedDbContext("moving AttachmentStreamInfos")) |
| | 255 | | { |
| | 256 | | const string mediaAttachmentQuery = |
| | 257 | | """ |
| | 258 | | SELECT ItemId, AttachmentIndex, Codec, CodecTag, Comment, filename, MIMEType |
| | 259 | | FROM mediaattachments |
| | 260 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = mediaattachments.ItemId) |
| | 261 | | """; |
| | 262 | |
|
| 0 | 263 | | using (new TrackedMigrationStep("loading AttachmentStreamInfos", _logger)) |
| | 264 | | { |
| 0 | 265 | | foreach (SqliteDataReader dto in connection.Query(mediaAttachmentQuery)) |
| | 266 | | { |
| 0 | 267 | | operation.JellyfinDbContext.AttachmentStreamInfos.Add(GetMediaAttachment(dto)); |
| | 268 | | } |
| | 269 | | } |
| | 270 | |
|
| 0 | 271 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.AttachmentStreamInfos.Local.Count} Att |
| | 272 | | { |
| 0 | 273 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 274 | | } |
| | 275 | | } |
| | 276 | |
|
| 0 | 277 | | using (var operation = GetPreparedDbContext("moving People")) |
| | 278 | | { |
| | 279 | | const string personsQuery = |
| | 280 | | """ |
| | 281 | | SELECT ItemId, Name, Role, PersonType, SortOrder FROM People |
| | 282 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = People.ItemId) |
| | 283 | | """; |
| | 284 | |
|
| 0 | 285 | | var peopleCache = new Dictionary<string, (People Person, List<PeopleBaseItemMap> Items)>(); |
| | 286 | |
|
| 0 | 287 | | using (new TrackedMigrationStep("loading People", _logger)) |
| | 288 | | { |
| 0 | 289 | | foreach (SqliteDataReader reader in connection.Query(personsQuery)) |
| | 290 | | { |
| 0 | 291 | | var itemId = reader.GetGuid(0); |
| 0 | 292 | | if (!baseItemIds.Contains(itemId)) |
| | 293 | | { |
| 0 | 294 | | _logger.LogError("Dont save person {0} because its not in use by any BaseItem", reader.GetString |
| 0 | 295 | | continue; |
| | 296 | | } |
| | 297 | |
|
| 0 | 298 | | var entity = GetPerson(reader); |
| 0 | 299 | | if (!peopleCache.TryGetValue(entity.Name, out var personCache)) |
| | 300 | | { |
| 0 | 301 | | peopleCache[entity.Name] = personCache = (entity, []); |
| | 302 | | } |
| | 303 | |
|
| 0 | 304 | | if (reader.TryGetString(2, out var role)) |
| | 305 | | { |
| | 306 | | } |
| | 307 | |
|
| 0 | 308 | | int? sortOrder = reader.IsDBNull(4) ? null : reader.GetInt32(4); |
| | 309 | |
|
| 0 | 310 | | personCache.Items.Add(new PeopleBaseItemMap() |
| 0 | 311 | | { |
| 0 | 312 | | Item = null!, |
| 0 | 313 | | ItemId = itemId, |
| 0 | 314 | | People = null!, |
| 0 | 315 | | PeopleId = personCache.Person.Id, |
| 0 | 316 | | ListOrder = sortOrder, |
| 0 | 317 | | SortOrder = sortOrder, |
| 0 | 318 | | Role = role |
| 0 | 319 | | }); |
| | 320 | | } |
| | 321 | |
|
| 0 | 322 | | baseItemIds.Clear(); |
| | 323 | |
|
| 0 | 324 | | foreach (var item in peopleCache) |
| | 325 | | { |
| 0 | 326 | | operation.JellyfinDbContext.Peoples.Add(item.Value.Person); |
| 0 | 327 | | operation.JellyfinDbContext.PeopleBaseItemMap.AddRange(item.Value.Items.DistinctBy(e => (e.ItemId, e |
| | 328 | | } |
| | 329 | |
|
| 0 | 330 | | peopleCache.Clear(); |
| 0 | 331 | | } |
| | 332 | |
|
| 0 | 333 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.Peoples.Local.Count} People entries an |
| | 334 | | { |
| 0 | 335 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 336 | | } |
| | 337 | | } |
| | 338 | |
|
| 0 | 339 | | using (var operation = GetPreparedDbContext("moving Chapters")) |
| | 340 | | { |
| | 341 | | const string chapterQuery = |
| | 342 | | """ |
| | 343 | | SELECT ItemId,StartPositionTicks,Name,ImagePath,ImageDateModified,ChapterIndex from Chapters2 |
| | 344 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = Chapters2.ItemId) |
| | 345 | | """; |
| | 346 | |
|
| 0 | 347 | | using (new TrackedMigrationStep("loading Chapters", _logger)) |
| | 348 | | { |
| 0 | 349 | | foreach (SqliteDataReader dto in connection.Query(chapterQuery)) |
| | 350 | | { |
| 0 | 351 | | var chapter = GetChapter(dto); |
| 0 | 352 | | operation.JellyfinDbContext.Chapters.Add(chapter); |
| | 353 | | } |
| | 354 | | } |
| | 355 | |
|
| 0 | 356 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.Chapters.Local.Count} Chapters entries |
| | 357 | | { |
| 0 | 358 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| 0 | 362 | | using (var operation = GetPreparedDbContext("moving AncestorIds")) |
| | 363 | | { |
| | 364 | | const string ancestorIdsQuery = |
| | 365 | | """ |
| | 366 | | SELECT ItemId, AncestorId, AncestorIdText FROM AncestorIds |
| | 367 | | WHERE |
| | 368 | | EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = AncestorIds.ItemId) |
| | 369 | | AND |
| | 370 | | EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = AncestorIds.AncestorId) |
| | 371 | | """; |
| | 372 | |
|
| 0 | 373 | | using (new TrackedMigrationStep("loading AncestorIds", _logger)) |
| | 374 | | { |
| 0 | 375 | | foreach (SqliteDataReader dto in connection.Query(ancestorIdsQuery)) |
| | 376 | | { |
| 0 | 377 | | var ancestorId = GetAncestorId(dto); |
| 0 | 378 | | operation.JellyfinDbContext.AncestorIds.Add(ancestorId); |
| | 379 | | } |
| | 380 | | } |
| | 381 | |
|
| 0 | 382 | | using (new TrackedMigrationStep($"saving {operation.JellyfinDbContext.AncestorIds.Local.Count} AncestorId en |
| | 383 | | { |
| 0 | 384 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 385 | | } |
| | 386 | | } |
| | 387 | |
|
| 0 | 388 | | connection.Close(); |
| | 389 | |
|
| 0 | 390 | | _logger.LogInformation("Migration of the Library.db done."); |
| 0 | 391 | | _logger.LogInformation("Migrating Library db took {0}.", fullOperationTimer.Elapsed); |
| | 392 | |
|
| 0 | 393 | | SqliteConnection.ClearAllPools(); |
| | 394 | |
|
| 0 | 395 | | _logger.LogInformation("Move {0} to {1}.", libraryDbPath, libraryDbPath + ".old"); |
| 0 | 396 | | File.Move(libraryDbPath, libraryDbPath + ".old", true); |
| 0 | 397 | | } |
| | 398 | |
|
| | 399 | | private DatabaseMigrationStep GetPreparedDbContext(string operationName) |
| | 400 | | { |
| 0 | 401 | | var dbContext = _provider.CreateDbContext(); |
| 0 | 402 | | dbContext.ChangeTracker.AutoDetectChangesEnabled = false; |
| 0 | 403 | | dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
| 0 | 404 | | return new DatabaseMigrationStep(dbContext, operationName, _logger); |
| | 405 | | } |
| | 406 | |
|
| | 407 | | private UserData? GetUserData(ImmutableArray<User> users, SqliteDataReader dto, HashSet<int> userIdBlacklist) |
| | 408 | | { |
| 0 | 409 | | var internalUserId = dto.GetInt32(1); |
| 0 | 410 | | var user = users.FirstOrDefault(e => e.InternalId == internalUserId); |
| | 411 | |
|
| 0 | 412 | | if (user is null) |
| | 413 | | { |
| 0 | 414 | | if (userIdBlacklist.Contains(internalUserId)) |
| | 415 | | { |
| 0 | 416 | | return null; |
| | 417 | | } |
| | 418 | |
|
| 0 | 419 | | _logger.LogError("Tried to find user with index '{Idx}' but there are only '{MaxIdx}' users.", internalUserI |
| 0 | 420 | | return null; |
| | 421 | | } |
| | 422 | |
|
| 0 | 423 | | var oldKey = dto.GetString(0); |
| | 424 | |
|
| 0 | 425 | | return new UserData() |
| 0 | 426 | | { |
| 0 | 427 | | ItemId = Guid.NewGuid(), |
| 0 | 428 | | CustomDataKey = oldKey, |
| 0 | 429 | | UserId = user.Id, |
| 0 | 430 | | Rating = dto.IsDBNull(2) ? null : dto.GetDouble(2), |
| 0 | 431 | | Played = dto.GetBoolean(3), |
| 0 | 432 | | PlayCount = dto.GetInt32(4), |
| 0 | 433 | | IsFavorite = dto.GetBoolean(5), |
| 0 | 434 | | PlaybackPositionTicks = dto.GetInt64(6), |
| 0 | 435 | | LastPlayedDate = dto.IsDBNull(7) ? null : dto.GetDateTime(7), |
| 0 | 436 | | AudioStreamIndex = dto.IsDBNull(8) ? null : dto.GetInt32(8), |
| 0 | 437 | | SubtitleStreamIndex = dto.IsDBNull(9) ? null : dto.GetInt32(9), |
| 0 | 438 | | Likes = null, |
| 0 | 439 | | User = null!, |
| 0 | 440 | | Item = null! |
| 0 | 441 | | }; |
| | 442 | | } |
| | 443 | |
|
| | 444 | | private AncestorId GetAncestorId(SqliteDataReader reader) |
| | 445 | | { |
| 0 | 446 | | return new AncestorId() |
| 0 | 447 | | { |
| 0 | 448 | | ItemId = reader.GetGuid(0), |
| 0 | 449 | | ParentItemId = reader.GetGuid(1), |
| 0 | 450 | | Item = null!, |
| 0 | 451 | | ParentItem = null! |
| 0 | 452 | | }; |
| | 453 | | } |
| | 454 | |
|
| | 455 | | /// <summary> |
| | 456 | | /// Gets the chapter. |
| | 457 | | /// </summary> |
| | 458 | | /// <param name="reader">The reader.</param> |
| | 459 | | /// <returns>ChapterInfo.</returns> |
| | 460 | | private Chapter GetChapter(SqliteDataReader reader) |
| | 461 | | { |
| 0 | 462 | | var chapter = new Chapter |
| 0 | 463 | | { |
| 0 | 464 | | StartPositionTicks = reader.GetInt64(1), |
| 0 | 465 | | ChapterIndex = reader.GetInt32(5), |
| 0 | 466 | | Item = null!, |
| 0 | 467 | | ItemId = reader.GetGuid(0), |
| 0 | 468 | | }; |
| | 469 | |
|
| 0 | 470 | | if (reader.TryGetString(2, out var chapterName)) |
| | 471 | | { |
| 0 | 472 | | chapter.Name = chapterName; |
| | 473 | | } |
| | 474 | |
|
| 0 | 475 | | if (reader.TryGetString(3, out var imagePath)) |
| | 476 | | { |
| 0 | 477 | | chapter.ImagePath = imagePath; |
| | 478 | | } |
| | 479 | |
|
| 0 | 480 | | if (reader.TryReadDateTime(4, out var imageDateModified)) |
| | 481 | | { |
| 0 | 482 | | chapter.ImageDateModified = imageDateModified; |
| | 483 | | } |
| | 484 | |
|
| 0 | 485 | | return chapter; |
| | 486 | | } |
| | 487 | |
|
| | 488 | | private ItemValue GetItemValue(SqliteDataReader reader) |
| | 489 | | { |
| 0 | 490 | | return new ItemValue |
| 0 | 491 | | { |
| 0 | 492 | | ItemValueId = Guid.NewGuid(), |
| 0 | 493 | | Type = (ItemValueType)reader.GetInt32(1), |
| 0 | 494 | | Value = reader.GetString(2), |
| 0 | 495 | | CleanValue = reader.GetString(3), |
| 0 | 496 | | }; |
| | 497 | | } |
| | 498 | |
|
| | 499 | | private People GetPerson(SqliteDataReader reader) |
| | 500 | | { |
| 0 | 501 | | var item = new People |
| 0 | 502 | | { |
| 0 | 503 | | Id = Guid.NewGuid(), |
| 0 | 504 | | Name = reader.GetString(1), |
| 0 | 505 | | }; |
| | 506 | |
|
| 0 | 507 | | if (reader.TryGetString(3, out var type)) |
| | 508 | | { |
| 0 | 509 | | item.PersonType = type; |
| | 510 | | } |
| | 511 | |
|
| 0 | 512 | | return item; |
| | 513 | | } |
| | 514 | |
|
| | 515 | | /// <summary> |
| | 516 | | /// Gets the media stream. |
| | 517 | | /// </summary> |
| | 518 | | /// <param name="reader">The reader.</param> |
| | 519 | | /// <returns>MediaStream.</returns> |
| | 520 | | private MediaStreamInfo GetMediaStream(SqliteDataReader reader) |
| | 521 | | { |
| 0 | 522 | | var item = new MediaStreamInfo |
| 0 | 523 | | { |
| 0 | 524 | | StreamIndex = reader.GetInt32(1), |
| 0 | 525 | | StreamType = Enum.Parse<MediaStreamTypeEntity>(reader.GetString(2)), |
| 0 | 526 | | Item = null!, |
| 0 | 527 | | ItemId = reader.GetGuid(0), |
| 0 | 528 | | AspectRatio = null!, |
| 0 | 529 | | ChannelLayout = null!, |
| 0 | 530 | | Codec = null!, |
| 0 | 531 | | IsInterlaced = false, |
| 0 | 532 | | Language = null!, |
| 0 | 533 | | Path = null!, |
| 0 | 534 | | Profile = null!, |
| 0 | 535 | | }; |
| | 536 | |
|
| 0 | 537 | | if (reader.TryGetString(3, out var codec)) |
| | 538 | | { |
| 0 | 539 | | item.Codec = codec; |
| | 540 | | } |
| | 541 | |
|
| 0 | 542 | | if (reader.TryGetString(4, out var language)) |
| | 543 | | { |
| 0 | 544 | | item.Language = language; |
| | 545 | | } |
| | 546 | |
|
| 0 | 547 | | if (reader.TryGetString(5, out var channelLayout)) |
| | 548 | | { |
| 0 | 549 | | item.ChannelLayout = channelLayout; |
| | 550 | | } |
| | 551 | |
|
| 0 | 552 | | if (reader.TryGetString(6, out var profile)) |
| | 553 | | { |
| 0 | 554 | | item.Profile = profile; |
| | 555 | | } |
| | 556 | |
|
| 0 | 557 | | if (reader.TryGetString(7, out var aspectRatio)) |
| | 558 | | { |
| 0 | 559 | | item.AspectRatio = aspectRatio; |
| | 560 | | } |
| | 561 | |
|
| 0 | 562 | | if (reader.TryGetString(8, out var path)) |
| | 563 | | { |
| 0 | 564 | | item.Path = path; |
| | 565 | | } |
| | 566 | |
|
| 0 | 567 | | item.IsInterlaced = reader.GetBoolean(9); |
| | 568 | |
|
| 0 | 569 | | if (reader.TryGetInt32(10, out var bitrate)) |
| | 570 | | { |
| 0 | 571 | | item.BitRate = bitrate; |
| | 572 | | } |
| | 573 | |
|
| 0 | 574 | | if (reader.TryGetInt32(11, out var channels)) |
| | 575 | | { |
| 0 | 576 | | item.Channels = channels; |
| | 577 | | } |
| | 578 | |
|
| 0 | 579 | | if (reader.TryGetInt32(12, out var sampleRate)) |
| | 580 | | { |
| 0 | 581 | | item.SampleRate = sampleRate; |
| | 582 | | } |
| | 583 | |
|
| 0 | 584 | | item.IsDefault = reader.GetBoolean(13); |
| 0 | 585 | | item.IsForced = reader.GetBoolean(14); |
| 0 | 586 | | item.IsExternal = reader.GetBoolean(15); |
| | 587 | |
|
| 0 | 588 | | if (reader.TryGetInt32(16, out var width)) |
| | 589 | | { |
| 0 | 590 | | item.Width = width; |
| | 591 | | } |
| | 592 | |
|
| 0 | 593 | | if (reader.TryGetInt32(17, out var height)) |
| | 594 | | { |
| 0 | 595 | | item.Height = height; |
| | 596 | | } |
| | 597 | |
|
| 0 | 598 | | if (reader.TryGetSingle(18, out var averageFrameRate)) |
| | 599 | | { |
| 0 | 600 | | item.AverageFrameRate = averageFrameRate; |
| | 601 | | } |
| | 602 | |
|
| 0 | 603 | | if (reader.TryGetSingle(19, out var realFrameRate)) |
| | 604 | | { |
| 0 | 605 | | item.RealFrameRate = realFrameRate; |
| | 606 | | } |
| | 607 | |
|
| 0 | 608 | | if (reader.TryGetSingle(20, out var level)) |
| | 609 | | { |
| 0 | 610 | | item.Level = level; |
| | 611 | | } |
| | 612 | |
|
| 0 | 613 | | if (reader.TryGetString(21, out var pixelFormat)) |
| | 614 | | { |
| 0 | 615 | | item.PixelFormat = pixelFormat; |
| | 616 | | } |
| | 617 | |
|
| 0 | 618 | | if (reader.TryGetInt32(22, out var bitDepth)) |
| | 619 | | { |
| 0 | 620 | | item.BitDepth = bitDepth; |
| | 621 | | } |
| | 622 | |
|
| 0 | 623 | | if (reader.TryGetBoolean(23, out var isAnamorphic)) |
| | 624 | | { |
| 0 | 625 | | item.IsAnamorphic = isAnamorphic; |
| | 626 | | } |
| | 627 | |
|
| 0 | 628 | | if (reader.TryGetInt32(24, out var refFrames)) |
| | 629 | | { |
| 0 | 630 | | item.RefFrames = refFrames; |
| | 631 | | } |
| | 632 | |
|
| 0 | 633 | | if (reader.TryGetString(25, out var codecTag)) |
| | 634 | | { |
| 0 | 635 | | item.CodecTag = codecTag; |
| | 636 | | } |
| | 637 | |
|
| 0 | 638 | | if (reader.TryGetString(26, out var comment)) |
| | 639 | | { |
| 0 | 640 | | item.Comment = comment; |
| | 641 | | } |
| | 642 | |
|
| 0 | 643 | | if (reader.TryGetString(27, out var nalLengthSize)) |
| | 644 | | { |
| 0 | 645 | | item.NalLengthSize = nalLengthSize; |
| | 646 | | } |
| | 647 | |
|
| 0 | 648 | | if (reader.TryGetBoolean(28, out var isAVC)) |
| | 649 | | { |
| 0 | 650 | | item.IsAvc = isAVC; |
| | 651 | | } |
| | 652 | |
|
| 0 | 653 | | if (reader.TryGetString(29, out var title)) |
| | 654 | | { |
| 0 | 655 | | item.Title = title; |
| | 656 | | } |
| | 657 | |
|
| 0 | 658 | | if (reader.TryGetString(30, out var timeBase)) |
| | 659 | | { |
| 0 | 660 | | item.TimeBase = timeBase; |
| | 661 | | } |
| | 662 | |
|
| 0 | 663 | | if (reader.TryGetString(31, out var codecTimeBase)) |
| | 664 | | { |
| 0 | 665 | | item.CodecTimeBase = codecTimeBase; |
| | 666 | | } |
| | 667 | |
|
| 0 | 668 | | if (reader.TryGetString(32, out var colorPrimaries)) |
| | 669 | | { |
| 0 | 670 | | item.ColorPrimaries = colorPrimaries; |
| | 671 | | } |
| | 672 | |
|
| 0 | 673 | | if (reader.TryGetString(33, out var colorSpace)) |
| | 674 | | { |
| 0 | 675 | | item.ColorSpace = colorSpace; |
| | 676 | | } |
| | 677 | |
|
| 0 | 678 | | if (reader.TryGetString(34, out var colorTransfer)) |
| | 679 | | { |
| 0 | 680 | | item.ColorTransfer = colorTransfer; |
| | 681 | | } |
| | 682 | |
|
| 0 | 683 | | if (reader.TryGetInt32(35, out var dvVersionMajor)) |
| | 684 | | { |
| 0 | 685 | | item.DvVersionMajor = dvVersionMajor; |
| | 686 | | } |
| | 687 | |
|
| 0 | 688 | | if (reader.TryGetInt32(36, out var dvVersionMinor)) |
| | 689 | | { |
| 0 | 690 | | item.DvVersionMinor = dvVersionMinor; |
| | 691 | | } |
| | 692 | |
|
| 0 | 693 | | if (reader.TryGetInt32(37, out var dvProfile)) |
| | 694 | | { |
| 0 | 695 | | item.DvProfile = dvProfile; |
| | 696 | | } |
| | 697 | |
|
| 0 | 698 | | if (reader.TryGetInt32(38, out var dvLevel)) |
| | 699 | | { |
| 0 | 700 | | item.DvLevel = dvLevel; |
| | 701 | | } |
| | 702 | |
|
| 0 | 703 | | if (reader.TryGetInt32(39, out var rpuPresentFlag)) |
| | 704 | | { |
| 0 | 705 | | item.RpuPresentFlag = rpuPresentFlag; |
| | 706 | | } |
| | 707 | |
|
| 0 | 708 | | if (reader.TryGetInt32(40, out var elPresentFlag)) |
| | 709 | | { |
| 0 | 710 | | item.ElPresentFlag = elPresentFlag; |
| | 711 | | } |
| | 712 | |
|
| 0 | 713 | | if (reader.TryGetInt32(41, out var blPresentFlag)) |
| | 714 | | { |
| 0 | 715 | | item.BlPresentFlag = blPresentFlag; |
| | 716 | | } |
| | 717 | |
|
| 0 | 718 | | if (reader.TryGetInt32(42, out var dvBlSignalCompatibilityId)) |
| | 719 | | { |
| 0 | 720 | | item.DvBlSignalCompatibilityId = dvBlSignalCompatibilityId; |
| | 721 | | } |
| | 722 | |
|
| 0 | 723 | | item.IsHearingImpaired = reader.TryGetBoolean(43, out var result) && result; |
| | 724 | |
|
| | 725 | | // if (reader.TryGetInt32(44, out var rotation)) |
| | 726 | | // { |
| | 727 | | // item.Rotation = rotation; |
| | 728 | | // } |
| | 729 | |
|
| 0 | 730 | | return item; |
| | 731 | | } |
| | 732 | |
|
| | 733 | | /// <summary> |
| | 734 | | /// Gets the attachment. |
| | 735 | | /// </summary> |
| | 736 | | /// <param name="reader">The reader.</param> |
| | 737 | | /// <returns>MediaAttachment.</returns> |
| | 738 | | private AttachmentStreamInfo GetMediaAttachment(SqliteDataReader reader) |
| | 739 | | { |
| 0 | 740 | | var item = new AttachmentStreamInfo |
| 0 | 741 | | { |
| 0 | 742 | | Index = reader.GetInt32(1), |
| 0 | 743 | | Item = null!, |
| 0 | 744 | | ItemId = reader.GetGuid(0), |
| 0 | 745 | | }; |
| | 746 | |
|
| 0 | 747 | | if (reader.TryGetString(2, out var codec)) |
| | 748 | | { |
| 0 | 749 | | item.Codec = codec; |
| | 750 | | } |
| | 751 | |
|
| 0 | 752 | | if (reader.TryGetString(3, out var codecTag)) |
| | 753 | | { |
| 0 | 754 | | item.CodecTag = codecTag; |
| | 755 | | } |
| | 756 | |
|
| 0 | 757 | | if (reader.TryGetString(4, out var comment)) |
| | 758 | | { |
| 0 | 759 | | item.Comment = comment; |
| | 760 | | } |
| | 761 | |
|
| 0 | 762 | | if (reader.TryGetString(5, out var fileName)) |
| | 763 | | { |
| 0 | 764 | | item.Filename = fileName; |
| | 765 | | } |
| | 766 | |
|
| 0 | 767 | | if (reader.TryGetString(6, out var mimeType)) |
| | 768 | | { |
| 0 | 769 | | item.MimeType = mimeType; |
| | 770 | | } |
| | 771 | |
|
| 0 | 772 | | return item; |
| | 773 | | } |
| | 774 | |
|
| | 775 | | private (BaseItemEntity BaseItem, string[] LegacyUserDataKey) GetItem(SqliteDataReader reader) |
| | 776 | | { |
| 0 | 777 | | var entity = new BaseItemEntity() |
| 0 | 778 | | { |
| 0 | 779 | | Id = reader.GetGuid(0), |
| 0 | 780 | | Type = reader.GetString(1), |
| 0 | 781 | | }; |
| | 782 | |
|
| 0 | 783 | | var index = 2; |
| | 784 | |
|
| 0 | 785 | | if (reader.TryGetString(index++, out var data)) |
| | 786 | | { |
| 0 | 787 | | entity.Data = data; |
| | 788 | | } |
| | 789 | |
|
| 0 | 790 | | if (reader.TryReadDateTime(index++, out var startDate)) |
| | 791 | | { |
| 0 | 792 | | entity.StartDate = startDate; |
| | 793 | | } |
| | 794 | |
|
| 0 | 795 | | if (reader.TryReadDateTime(index++, out var endDate)) |
| | 796 | | { |
| 0 | 797 | | entity.EndDate = endDate; |
| | 798 | | } |
| | 799 | |
|
| 0 | 800 | | if (reader.TryGetGuid(index++, out var guid)) |
| | 801 | | { |
| 0 | 802 | | entity.ChannelId = guid; |
| | 803 | | } |
| | 804 | |
|
| 0 | 805 | | if (reader.TryGetBoolean(index++, out var isMovie)) |
| | 806 | | { |
| 0 | 807 | | entity.IsMovie = isMovie; |
| | 808 | | } |
| | 809 | |
|
| 0 | 810 | | if (reader.TryGetBoolean(index++, out var isSeries)) |
| | 811 | | { |
| 0 | 812 | | entity.IsSeries = isSeries; |
| | 813 | | } |
| | 814 | |
|
| 0 | 815 | | if (reader.TryGetString(index++, out var episodeTitle)) |
| | 816 | | { |
| 0 | 817 | | entity.EpisodeTitle = episodeTitle; |
| | 818 | | } |
| | 819 | |
|
| 0 | 820 | | if (reader.TryGetBoolean(index++, out var isRepeat)) |
| | 821 | | { |
| 0 | 822 | | entity.IsRepeat = isRepeat; |
| | 823 | | } |
| | 824 | |
|
| 0 | 825 | | if (reader.TryGetSingle(index++, out var communityRating)) |
| | 826 | | { |
| 0 | 827 | | entity.CommunityRating = communityRating; |
| | 828 | | } |
| | 829 | |
|
| 0 | 830 | | if (reader.TryGetString(index++, out var customRating)) |
| | 831 | | { |
| 0 | 832 | | entity.CustomRating = customRating; |
| | 833 | | } |
| | 834 | |
|
| 0 | 835 | | if (reader.TryGetInt32(index++, out var indexNumber)) |
| | 836 | | { |
| 0 | 837 | | entity.IndexNumber = indexNumber; |
| | 838 | | } |
| | 839 | |
|
| 0 | 840 | | if (reader.TryGetBoolean(index++, out var isLocked)) |
| | 841 | | { |
| 0 | 842 | | entity.IsLocked = isLocked; |
| | 843 | | } |
| | 844 | |
|
| 0 | 845 | | if (reader.TryGetString(index++, out var preferredMetadataLanguage)) |
| | 846 | | { |
| 0 | 847 | | entity.PreferredMetadataLanguage = preferredMetadataLanguage; |
| | 848 | | } |
| | 849 | |
|
| 0 | 850 | | if (reader.TryGetString(index++, out var preferredMetadataCountryCode)) |
| | 851 | | { |
| 0 | 852 | | entity.PreferredMetadataCountryCode = preferredMetadataCountryCode; |
| | 853 | | } |
| | 854 | |
|
| 0 | 855 | | if (reader.TryGetInt32(index++, out var width)) |
| | 856 | | { |
| 0 | 857 | | entity.Width = width; |
| | 858 | | } |
| | 859 | |
|
| 0 | 860 | | if (reader.TryGetInt32(index++, out var height)) |
| | 861 | | { |
| 0 | 862 | | entity.Height = height; |
| | 863 | | } |
| | 864 | |
|
| 0 | 865 | | if (reader.TryReadDateTime(index++, out var dateLastRefreshed)) |
| | 866 | | { |
| 0 | 867 | | entity.DateLastRefreshed = dateLastRefreshed; |
| | 868 | | } |
| | 869 | |
|
| 0 | 870 | | if (reader.TryGetString(index++, out var name)) |
| | 871 | | { |
| 0 | 872 | | entity.Name = name; |
| | 873 | | } |
| | 874 | |
|
| 0 | 875 | | if (reader.TryGetString(index++, out var restorePath)) |
| | 876 | | { |
| 0 | 877 | | entity.Path = restorePath; |
| | 878 | | } |
| | 879 | |
|
| 0 | 880 | | if (reader.TryReadDateTime(index++, out var premiereDate)) |
| | 881 | | { |
| 0 | 882 | | entity.PremiereDate = premiereDate; |
| | 883 | | } |
| | 884 | |
|
| 0 | 885 | | if (reader.TryGetString(index++, out var overview)) |
| | 886 | | { |
| 0 | 887 | | entity.Overview = overview; |
| | 888 | | } |
| | 889 | |
|
| 0 | 890 | | if (reader.TryGetInt32(index++, out var parentIndexNumber)) |
| | 891 | | { |
| 0 | 892 | | entity.ParentIndexNumber = parentIndexNumber; |
| | 893 | | } |
| | 894 | |
|
| 0 | 895 | | if (reader.TryGetInt32(index++, out var productionYear)) |
| | 896 | | { |
| 0 | 897 | | entity.ProductionYear = productionYear; |
| | 898 | | } |
| | 899 | |
|
| 0 | 900 | | if (reader.TryGetString(index++, out var officialRating)) |
| | 901 | | { |
| 0 | 902 | | entity.OfficialRating = officialRating; |
| | 903 | | } |
| | 904 | |
|
| 0 | 905 | | if (reader.TryGetString(index++, out var forcedSortName)) |
| | 906 | | { |
| 0 | 907 | | entity.ForcedSortName = forcedSortName; |
| | 908 | | } |
| | 909 | |
|
| 0 | 910 | | if (reader.TryGetInt64(index++, out var runTimeTicks)) |
| | 911 | | { |
| 0 | 912 | | entity.RunTimeTicks = runTimeTicks; |
| | 913 | | } |
| | 914 | |
|
| 0 | 915 | | if (reader.TryGetInt64(index++, out var size)) |
| | 916 | | { |
| 0 | 917 | | entity.Size = size; |
| | 918 | | } |
| | 919 | |
|
| 0 | 920 | | if (reader.TryReadDateTime(index++, out var dateCreated)) |
| | 921 | | { |
| 0 | 922 | | entity.DateCreated = dateCreated; |
| | 923 | | } |
| | 924 | |
|
| 0 | 925 | | if (reader.TryReadDateTime(index++, out var dateModified)) |
| | 926 | | { |
| 0 | 927 | | entity.DateModified = dateModified; |
| | 928 | | } |
| | 929 | |
|
| 0 | 930 | | if (reader.TryGetString(index++, out var genres)) |
| | 931 | | { |
| 0 | 932 | | entity.Genres = genres; |
| | 933 | | } |
| | 934 | |
|
| 0 | 935 | | if (reader.TryGetGuid(index++, out var parentId)) |
| | 936 | | { |
| 0 | 937 | | entity.ParentId = parentId; |
| | 938 | | } |
| | 939 | |
|
| 0 | 940 | | if (reader.TryGetGuid(index++, out var topParentId)) |
| | 941 | | { |
| 0 | 942 | | entity.TopParentId = topParentId; |
| | 943 | | } |
| | 944 | |
|
| 0 | 945 | | if (reader.TryGetString(index++, out var audioString) && Enum.TryParse<ProgramAudioEntity>(audioString, out var |
| | 946 | | { |
| 0 | 947 | | entity.Audio = audioType; |
| | 948 | | } |
| | 949 | |
|
| 0 | 950 | | if (reader.TryGetString(index++, out var serviceName)) |
| | 951 | | { |
| 0 | 952 | | entity.ExternalServiceId = serviceName; |
| | 953 | | } |
| | 954 | |
|
| 0 | 955 | | if (reader.TryGetBoolean(index++, out var isInMixedFolder)) |
| | 956 | | { |
| 0 | 957 | | entity.IsInMixedFolder = isInMixedFolder; |
| | 958 | | } |
| | 959 | |
|
| 0 | 960 | | if (reader.TryReadDateTime(index++, out var dateLastSaved)) |
| | 961 | | { |
| 0 | 962 | | entity.DateLastSaved = dateLastSaved; |
| | 963 | | } |
| | 964 | |
|
| 0 | 965 | | if (reader.TryGetString(index++, out var lockedFields)) |
| | 966 | | { |
| 0 | 967 | | entity.LockedFields = lockedFields.Split('|').Select(Enum.Parse<MetadataField>) |
| 0 | 968 | | .Select(e => new BaseItemMetadataField() |
| 0 | 969 | | { |
| 0 | 970 | | Id = (int)e, |
| 0 | 971 | | Item = entity, |
| 0 | 972 | | ItemId = entity.Id |
| 0 | 973 | | }) |
| 0 | 974 | | .ToArray(); |
| | 975 | | } |
| | 976 | |
|
| 0 | 977 | | if (reader.TryGetString(index++, out var studios)) |
| | 978 | | { |
| 0 | 979 | | entity.Studios = studios; |
| | 980 | | } |
| | 981 | |
|
| 0 | 982 | | if (reader.TryGetString(index++, out var tags)) |
| | 983 | | { |
| 0 | 984 | | entity.Tags = tags; |
| | 985 | | } |
| | 986 | |
|
| 0 | 987 | | if (reader.TryGetString(index++, out var trailerTypes)) |
| | 988 | | { |
| 0 | 989 | | entity.TrailerTypes = trailerTypes.Split('|').Select(Enum.Parse<TrailerType>) |
| 0 | 990 | | .Select(e => new BaseItemTrailerType() |
| 0 | 991 | | { |
| 0 | 992 | | Id = (int)e, |
| 0 | 993 | | Item = entity, |
| 0 | 994 | | ItemId = entity.Id |
| 0 | 995 | | }) |
| 0 | 996 | | .ToArray(); |
| | 997 | | } |
| | 998 | |
|
| 0 | 999 | | if (reader.TryGetString(index++, out var originalTitle)) |
| | 1000 | | { |
| 0 | 1001 | | entity.OriginalTitle = originalTitle; |
| | 1002 | | } |
| | 1003 | |
|
| 0 | 1004 | | if (reader.TryGetString(index++, out var primaryVersionId)) |
| | 1005 | | { |
| 0 | 1006 | | entity.PrimaryVersionId = primaryVersionId; |
| | 1007 | | } |
| | 1008 | |
|
| 0 | 1009 | | if (reader.TryReadDateTime(index++, out var dateLastMediaAdded)) |
| | 1010 | | { |
| 0 | 1011 | | entity.DateLastMediaAdded = dateLastMediaAdded; |
| | 1012 | | } |
| | 1013 | |
|
| 0 | 1014 | | if (reader.TryGetString(index++, out var album)) |
| | 1015 | | { |
| 0 | 1016 | | entity.Album = album; |
| | 1017 | | } |
| | 1018 | |
|
| 0 | 1019 | | if (reader.TryGetSingle(index++, out var lUFS)) |
| | 1020 | | { |
| 0 | 1021 | | entity.LUFS = lUFS; |
| | 1022 | | } |
| | 1023 | |
|
| 0 | 1024 | | if (reader.TryGetSingle(index++, out var normalizationGain)) |
| | 1025 | | { |
| 0 | 1026 | | entity.NormalizationGain = normalizationGain; |
| | 1027 | | } |
| | 1028 | |
|
| 0 | 1029 | | if (reader.TryGetSingle(index++, out var criticRating)) |
| | 1030 | | { |
| 0 | 1031 | | entity.CriticRating = criticRating; |
| | 1032 | | } |
| | 1033 | |
|
| 0 | 1034 | | if (reader.TryGetBoolean(index++, out var isVirtualItem)) |
| | 1035 | | { |
| 0 | 1036 | | entity.IsVirtualItem = isVirtualItem; |
| | 1037 | | } |
| | 1038 | |
|
| 0 | 1039 | | if (reader.TryGetString(index++, out var seriesName)) |
| | 1040 | | { |
| 0 | 1041 | | entity.SeriesName = seriesName; |
| | 1042 | | } |
| | 1043 | |
|
| 0 | 1044 | | var userDataKeys = new List<string>(); |
| 0 | 1045 | | if (reader.TryGetString(index++, out var directUserDataKey)) |
| | 1046 | | { |
| 0 | 1047 | | userDataKeys.Add(directUserDataKey); |
| | 1048 | | } |
| | 1049 | |
|
| 0 | 1050 | | if (reader.TryGetString(index++, out var seasonName)) |
| | 1051 | | { |
| 0 | 1052 | | entity.SeasonName = seasonName; |
| | 1053 | | } |
| | 1054 | |
|
| 0 | 1055 | | if (reader.TryGetGuid(index++, out var seasonId)) |
| | 1056 | | { |
| 0 | 1057 | | entity.SeasonId = seasonId; |
| | 1058 | | } |
| | 1059 | |
|
| 0 | 1060 | | if (reader.TryGetGuid(index++, out var seriesId)) |
| | 1061 | | { |
| 0 | 1062 | | entity.SeriesId = seriesId; |
| | 1063 | | } |
| | 1064 | |
|
| 0 | 1065 | | if (reader.TryGetString(index++, out var presentationUniqueKey)) |
| | 1066 | | { |
| 0 | 1067 | | entity.PresentationUniqueKey = presentationUniqueKey; |
| | 1068 | | } |
| | 1069 | |
|
| 0 | 1070 | | if (reader.TryGetInt32(index++, out var parentalRating)) |
| | 1071 | | { |
| 0 | 1072 | | entity.InheritedParentalRatingValue = parentalRating; |
| | 1073 | | } |
| | 1074 | |
|
| 0 | 1075 | | if (reader.TryGetString(index++, out var externalSeriesId)) |
| | 1076 | | { |
| 0 | 1077 | | entity.ExternalSeriesId = externalSeriesId; |
| | 1078 | | } |
| | 1079 | |
|
| 0 | 1080 | | if (reader.TryGetString(index++, out var tagLine)) |
| | 1081 | | { |
| 0 | 1082 | | entity.Tagline = tagLine; |
| | 1083 | | } |
| | 1084 | |
|
| 0 | 1085 | | if (reader.TryGetString(index++, out var providerIds)) |
| | 1086 | | { |
| 0 | 1087 | | entity.Provider = providerIds.Split('|').Select(e => e.Split("=")) |
| 0 | 1088 | | .Select(e => new BaseItemProvider() |
| 0 | 1089 | | { |
| 0 | 1090 | | Item = null!, |
| 0 | 1091 | | ProviderId = e[0], |
| 0 | 1092 | | ProviderValue = e[1] |
| 0 | 1093 | | }).ToArray(); |
| | 1094 | | } |
| | 1095 | |
|
| 0 | 1096 | | if (reader.TryGetString(index++, out var imageInfos)) |
| | 1097 | | { |
| 0 | 1098 | | entity.Images = DeserializeImages(imageInfos).Select(f => Map(entity.Id, f)).ToArray(); |
| | 1099 | | } |
| | 1100 | |
|
| 0 | 1101 | | if (reader.TryGetString(index++, out var productionLocations)) |
| | 1102 | | { |
| 0 | 1103 | | entity.ProductionLocations = productionLocations; |
| | 1104 | | } |
| | 1105 | |
|
| 0 | 1106 | | if (reader.TryGetString(index++, out var extraIds)) |
| | 1107 | | { |
| 0 | 1108 | | entity.ExtraIds = extraIds; |
| | 1109 | | } |
| | 1110 | |
|
| 0 | 1111 | | if (reader.TryGetInt32(index++, out var totalBitrate)) |
| | 1112 | | { |
| 0 | 1113 | | entity.TotalBitrate = totalBitrate; |
| | 1114 | | } |
| | 1115 | |
|
| 0 | 1116 | | if (reader.TryGetString(index++, out var extraTypeString) && Enum.TryParse<BaseItemExtraType>(extraTypeString, o |
| | 1117 | | { |
| 0 | 1118 | | entity.ExtraType = extraType; |
| | 1119 | | } |
| | 1120 | |
|
| 0 | 1121 | | if (reader.TryGetString(index++, out var artists)) |
| | 1122 | | { |
| 0 | 1123 | | entity.Artists = artists; |
| | 1124 | | } |
| | 1125 | |
|
| 0 | 1126 | | if (reader.TryGetString(index++, out var albumArtists)) |
| | 1127 | | { |
| 0 | 1128 | | entity.AlbumArtists = albumArtists; |
| | 1129 | | } |
| | 1130 | |
|
| 0 | 1131 | | if (reader.TryGetString(index++, out var externalId)) |
| | 1132 | | { |
| 0 | 1133 | | entity.ExternalId = externalId; |
| | 1134 | | } |
| | 1135 | |
|
| 0 | 1136 | | if (reader.TryGetString(index++, out var seriesPresentationUniqueKey)) |
| | 1137 | | { |
| 0 | 1138 | | entity.SeriesPresentationUniqueKey = seriesPresentationUniqueKey; |
| | 1139 | | } |
| | 1140 | |
|
| 0 | 1141 | | if (reader.TryGetString(index++, out var showId)) |
| | 1142 | | { |
| 0 | 1143 | | entity.ShowId = showId; |
| | 1144 | | } |
| | 1145 | |
|
| 0 | 1146 | | if (reader.TryGetString(index++, out var ownerId)) |
| | 1147 | | { |
| 0 | 1148 | | entity.OwnerId = ownerId; |
| | 1149 | | } |
| | 1150 | |
|
| 0 | 1151 | | if (reader.TryGetString(index++, out var mediaType)) |
| | 1152 | | { |
| 0 | 1153 | | entity.MediaType = mediaType; |
| | 1154 | | } |
| | 1155 | |
|
| 0 | 1156 | | if (reader.TryGetString(index++, out var sortName)) |
| | 1157 | | { |
| 0 | 1158 | | entity.SortName = sortName; |
| | 1159 | | } |
| | 1160 | |
|
| 0 | 1161 | | if (reader.TryGetString(index++, out var cleanName)) |
| | 1162 | | { |
| 0 | 1163 | | entity.CleanName = cleanName; |
| | 1164 | | } |
| | 1165 | |
|
| 0 | 1166 | | if (reader.TryGetString(index++, out var unratedType)) |
| | 1167 | | { |
| 0 | 1168 | | entity.UnratedType = unratedType; |
| | 1169 | | } |
| | 1170 | |
|
| 0 | 1171 | | var baseItem = BaseItemRepository.DeserialiseBaseItem(entity, _logger, null, false); |
| 0 | 1172 | | var dataKeys = baseItem.GetUserDataKeys(); |
| 0 | 1173 | | userDataKeys.AddRange(dataKeys); |
| | 1174 | |
|
| 0 | 1175 | | return (entity, userDataKeys.ToArray()); |
| | 1176 | | } |
| | 1177 | |
|
| | 1178 | | private static BaseItemImageInfo Map(Guid baseItemId, ItemImageInfo e) |
| | 1179 | | { |
| 0 | 1180 | | return new BaseItemImageInfo() |
| 0 | 1181 | | { |
| 0 | 1182 | | ItemId = baseItemId, |
| 0 | 1183 | | Id = Guid.NewGuid(), |
| 0 | 1184 | | Path = e.Path, |
| 0 | 1185 | | Blurhash = e.BlurHash != null ? Encoding.UTF8.GetBytes(e.BlurHash) : null, |
| 0 | 1186 | | DateModified = e.DateModified, |
| 0 | 1187 | | Height = e.Height, |
| 0 | 1188 | | Width = e.Width, |
| 0 | 1189 | | ImageType = (ImageInfoImageType)e.Type, |
| 0 | 1190 | | Item = null! |
| 0 | 1191 | | }; |
| | 1192 | | } |
| | 1193 | |
|
| | 1194 | | internal ItemImageInfo[] DeserializeImages(string value) |
| | 1195 | | { |
| 0 | 1196 | | if (string.IsNullOrWhiteSpace(value)) |
| | 1197 | | { |
| 0 | 1198 | | return Array.Empty<ItemImageInfo>(); |
| | 1199 | | } |
| | 1200 | |
|
| | 1201 | | // TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the data |
| 0 | 1202 | | var valueSpan = value.AsSpan(); |
| 0 | 1203 | | var count = valueSpan.Count('|') + 1; |
| | 1204 | |
|
| 0 | 1205 | | var position = 0; |
| 0 | 1206 | | var result = new ItemImageInfo[count]; |
| 0 | 1207 | | foreach (var part in valueSpan.Split('|')) |
| | 1208 | | { |
| 0 | 1209 | | var image = ItemImageInfoFromValueString(part); |
| | 1210 | |
|
| 0 | 1211 | | if (image is not null) |
| | 1212 | | { |
| 0 | 1213 | | result[position++] = image; |
| | 1214 | | } |
| | 1215 | | } |
| | 1216 | |
|
| 0 | 1217 | | if (position == count) |
| | 1218 | | { |
| 0 | 1219 | | return result; |
| | 1220 | | } |
| | 1221 | |
|
| 0 | 1222 | | if (position == 0) |
| | 1223 | | { |
| 0 | 1224 | | return Array.Empty<ItemImageInfo>(); |
| | 1225 | | } |
| | 1226 | |
|
| | 1227 | | // Extremely unlikely, but somehow one or more of the image strings were malformed. Cut the array. |
| 0 | 1228 | | return result[..position]; |
| | 1229 | | } |
| | 1230 | |
|
| | 1231 | | internal ItemImageInfo? ItemImageInfoFromValueString(ReadOnlySpan<char> value) |
| | 1232 | | { |
| | 1233 | | const char Delimiter = '*'; |
| | 1234 | |
|
| 0 | 1235 | | var nextSegment = value.IndexOf(Delimiter); |
| 0 | 1236 | | if (nextSegment == -1) |
| | 1237 | | { |
| 0 | 1238 | | return null; |
| | 1239 | | } |
| | 1240 | |
|
| 0 | 1241 | | ReadOnlySpan<char> path = value[..nextSegment]; |
| 0 | 1242 | | value = value[(nextSegment + 1)..]; |
| 0 | 1243 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1244 | | if (nextSegment == -1) |
| | 1245 | | { |
| 0 | 1246 | | return null; |
| | 1247 | | } |
| | 1248 | |
|
| 0 | 1249 | | ReadOnlySpan<char> dateModified = value[..nextSegment]; |
| 0 | 1250 | | value = value[(nextSegment + 1)..]; |
| 0 | 1251 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1252 | | if (nextSegment == -1) |
| | 1253 | | { |
| 0 | 1254 | | nextSegment = value.Length; |
| | 1255 | | } |
| | 1256 | |
|
| 0 | 1257 | | ReadOnlySpan<char> imageType = value[..nextSegment]; |
| | 1258 | |
|
| 0 | 1259 | | var image = new ItemImageInfo |
| 0 | 1260 | | { |
| 0 | 1261 | | Path = path.ToString() |
| 0 | 1262 | | }; |
| | 1263 | |
|
| 0 | 1264 | | if (long.TryParse(dateModified, CultureInfo.InvariantCulture, out var ticks) |
| 0 | 1265 | | && ticks >= DateTime.MinValue.Ticks |
| 0 | 1266 | | && ticks <= DateTime.MaxValue.Ticks) |
| | 1267 | | { |
| 0 | 1268 | | image.DateModified = new DateTime(ticks, DateTimeKind.Utc); |
| | 1269 | | } |
| | 1270 | | else |
| | 1271 | | { |
| 0 | 1272 | | return null; |
| | 1273 | | } |
| | 1274 | |
|
| 0 | 1275 | | if (Enum.TryParse(imageType, true, out ImageType type)) |
| | 1276 | | { |
| 0 | 1277 | | image.Type = type; |
| | 1278 | | } |
| | 1279 | | else |
| | 1280 | | { |
| 0 | 1281 | | return null; |
| | 1282 | | } |
| | 1283 | |
|
| | 1284 | | // Optional parameters: width*height*blurhash |
| 0 | 1285 | | if (nextSegment + 1 < value.Length - 1) |
| | 1286 | | { |
| 0 | 1287 | | value = value[(nextSegment + 1)..]; |
| 0 | 1288 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1289 | | if (nextSegment == -1 || nextSegment == value.Length) |
| | 1290 | | { |
| 0 | 1291 | | return image; |
| | 1292 | | } |
| | 1293 | |
|
| 0 | 1294 | | ReadOnlySpan<char> widthSpan = value[..nextSegment]; |
| | 1295 | |
|
| 0 | 1296 | | value = value[(nextSegment + 1)..]; |
| 0 | 1297 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1298 | | if (nextSegment == -1) |
| | 1299 | | { |
| 0 | 1300 | | nextSegment = value.Length; |
| | 1301 | | } |
| | 1302 | |
|
| 0 | 1303 | | ReadOnlySpan<char> heightSpan = value[..nextSegment]; |
| | 1304 | |
|
| 0 | 1305 | | if (int.TryParse(widthSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var width) |
| 0 | 1306 | | && int.TryParse(heightSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var height)) |
| | 1307 | | { |
| 0 | 1308 | | image.Width = width; |
| 0 | 1309 | | image.Height = height; |
| | 1310 | | } |
| | 1311 | |
|
| 0 | 1312 | | if (nextSegment < value.Length - 1) |
| | 1313 | | { |
| 0 | 1314 | | value = value[(nextSegment + 1)..]; |
| 0 | 1315 | | var length = value.Length; |
| | 1316 | |
|
| 0 | 1317 | | Span<char> blurHashSpan = stackalloc char[length]; |
| 0 | 1318 | | for (int i = 0; i < length; i++) |
| | 1319 | | { |
| 0 | 1320 | | var c = value[i]; |
| 0 | 1321 | | blurHashSpan[i] = c switch |
| 0 | 1322 | | { |
| 0 | 1323 | | '/' => Delimiter, |
| 0 | 1324 | | '\\' => '|', |
| 0 | 1325 | | _ => c |
| 0 | 1326 | | }; |
| | 1327 | | } |
| | 1328 | |
|
| 0 | 1329 | | image.BlurHash = new string(blurHashSpan); |
| | 1330 | | } |
| | 1331 | | } |
| | 1332 | |
|
| 0 | 1333 | | return image; |
| | 1334 | | } |
| | 1335 | |
|
| | 1336 | | private class TrackedMigrationStep : IDisposable |
| | 1337 | | { |
| | 1338 | | private readonly string _operationName; |
| | 1339 | | private readonly ILogger _logger; |
| | 1340 | | private readonly Stopwatch _operationTimer; |
| | 1341 | | private bool _disposed; |
| | 1342 | |
|
| | 1343 | | public TrackedMigrationStep(string operationName, ILogger logger) |
| | 1344 | | { |
| 0 | 1345 | | _operationName = operationName; |
| 0 | 1346 | | _logger = logger; |
| 0 | 1347 | | _operationTimer = Stopwatch.StartNew(); |
| 0 | 1348 | | logger.LogInformation("Start {OperationName}", operationName); |
| 0 | 1349 | | } |
| | 1350 | |
|
| | 1351 | | public bool Disposed |
| | 1352 | | { |
| 0 | 1353 | | get => _disposed; |
| 0 | 1354 | | set => _disposed = value; |
| | 1355 | | } |
| | 1356 | |
|
| | 1357 | | public virtual void Dispose() |
| | 1358 | | { |
| 0 | 1359 | | if (Disposed) |
| | 1360 | | { |
| 0 | 1361 | | return; |
| | 1362 | | } |
| | 1363 | |
|
| 0 | 1364 | | Disposed = true; |
| 0 | 1365 | | _logger.LogInformation("{OperationName} took '{Time}'", _operationName, _operationTimer.Elapsed); |
| 0 | 1366 | | } |
| | 1367 | | } |
| | 1368 | |
|
| | 1369 | | private sealed class DatabaseMigrationStep : TrackedMigrationStep |
| | 1370 | | { |
| 0 | 1371 | | public DatabaseMigrationStep(JellyfinDbContext jellyfinDbContext, string operationName, ILogger logger) : base(o |
| | 1372 | | { |
| | 1373 | | JellyfinDbContext = jellyfinDbContext; |
| 0 | 1374 | | } |
| | 1375 | |
|
| | 1376 | | public JellyfinDbContext JellyfinDbContext { get; } |
| | 1377 | |
|
| | 1378 | | public override void Dispose() |
| | 1379 | | { |
| 0 | 1380 | | if (Disposed) |
| | 1381 | | { |
| 0 | 1382 | | return; |
| | 1383 | | } |
| | 1384 | |
|
| 0 | 1385 | | JellyfinDbContext.Dispose(); |
| 0 | 1386 | | base.Dispose(); |
| 0 | 1387 | | } |
| | 1388 | | } |
| | 1389 | | } |