| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Database.Implementations.Entities; |
| | | 8 | | using Jellyfin.Extensions; |
| | | 9 | | using MediaBrowser.Controller; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | using LinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Migrates LinkedChildren data from JSON Data column to the LinkedChildren table. |
| | | 19 | | /// </summary> |
| | | 20 | | [JellyfinMigration("2026-01-13T12:00:00", nameof(MigrateLinkedChildren))] |
| | | 21 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 22 | | internal class MigrateLinkedChildren : IDatabaseMigrationRoutine |
| | | 23 | | { |
| | | 24 | | private readonly ILogger<MigrateLinkedChildren> _logger; |
| | | 25 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 26 | | private readonly ILibraryManager _libraryManager; |
| | | 27 | | private readonly IServerApplicationHost _appHost; |
| | | 28 | | private readonly IServerApplicationPaths _appPaths; |
| | | 29 | | |
| | | 30 | | public MigrateLinkedChildren( |
| | | 31 | | ILoggerFactory loggerFactory, |
| | | 32 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 33 | | ILibraryManager libraryManager, |
| | | 34 | | IServerApplicationHost appHost, |
| | | 35 | | IServerApplicationPaths appPaths) |
| | | 36 | | { |
| | 0 | 37 | | _logger = loggerFactory.CreateLogger<MigrateLinkedChildren>(); |
| | 0 | 38 | | _dbProvider = dbProvider; |
| | 0 | 39 | | _libraryManager = libraryManager; |
| | 0 | 40 | | _appHost = appHost; |
| | 0 | 41 | | _appPaths = appPaths; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | | 45 | | public void Perform() |
| | | 46 | | { |
| | 0 | 47 | | using var context = _dbProvider.CreateDbContext(); |
| | | 48 | | |
| | 0 | 49 | | var containerTypes = new[] |
| | 0 | 50 | | { |
| | 0 | 51 | | "MediaBrowser.Controller.Entities.Movies.BoxSet", |
| | 0 | 52 | | "MediaBrowser.Controller.Playlists.Playlist", |
| | 0 | 53 | | "MediaBrowser.Controller.Entities.CollectionFolder" |
| | 0 | 54 | | }; |
| | | 55 | | |
| | 0 | 56 | | var videoTypes = new[] |
| | 0 | 57 | | { |
| | 0 | 58 | | "MediaBrowser.Controller.Entities.Video", |
| | 0 | 59 | | "MediaBrowser.Controller.Entities.Movies.Movie", |
| | 0 | 60 | | "MediaBrowser.Controller.Entities.TV.Episode" |
| | 0 | 61 | | }; |
| | | 62 | | |
| | 0 | 63 | | var itemsWithData = context.BaseItems |
| | 0 | 64 | | .Where(b => b.Data != null && (containerTypes.Contains(b.Type) || videoTypes.Contains(b.Type))) |
| | 0 | 65 | | .Select(b => new { b.Id, b.Data, b.Type }) |
| | 0 | 66 | | .ToList(); |
| | | 67 | | |
| | 0 | 68 | | _logger.LogInformation("Found {Count} potential items with LinkedChildren data to process.", itemsWithData.Count |
| | | 69 | | |
| | 0 | 70 | | var pathToIdMap = context.BaseItems |
| | 0 | 71 | | .Where(b => b.Path != null) |
| | 0 | 72 | | .Select(b => new { b.Id, b.Path }) |
| | 0 | 73 | | .GroupBy(b => b.Path!) |
| | 0 | 74 | | .ToDictionary(g => g.Key, g => g.First().Id); |
| | | 75 | | |
| | 0 | 76 | | var linkedChildrenToAdd = new List<LinkedChildEntity>(); |
| | 0 | 77 | | var processedCount = 0; |
| | | 78 | | const int progressLogStep = 1000; |
| | 0 | 79 | | var totalItems = itemsWithData.Count; |
| | | 80 | | |
| | 0 | 81 | | foreach (var item in itemsWithData) |
| | | 82 | | { |
| | 0 | 83 | | if (string.IsNullOrEmpty(item.Data)) |
| | | 84 | | { |
| | | 85 | | continue; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | if (processedCount > 0 && processedCount % progressLogStep == 0) |
| | | 89 | | { |
| | 0 | 90 | | _logger.LogInformation("Processing LinkedChildren: {Processed}/{Total} items", processedCount, totalItem |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | try |
| | | 94 | | { |
| | 0 | 95 | | using var doc = JsonDocument.Parse(item.Data); |
| | | 96 | | |
| | 0 | 97 | | var isVideo = videoTypes.Contains(item.Type); |
| | | 98 | | |
| | | 99 | | // Handle Video alternate versions |
| | 0 | 100 | | if (isVideo) |
| | | 101 | | { |
| | 0 | 102 | | ProcessVideoAlternateVersions(doc.RootElement, item.Id, pathToIdMap, linkedChildrenToAdd); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // Handle LinkedChildren (for containers and other items) |
| | 0 | 106 | | if (!doc.RootElement.TryGetProperty("LinkedChildren", out var linkedChildrenElement) || linkedChildrenEl |
| | | 107 | | { |
| | 0 | 108 | | processedCount++; |
| | 0 | 109 | | continue; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | var isPlaylist = item.Type == "MediaBrowser.Controller.Playlists.Playlist"; |
| | 0 | 113 | | var sortOrder = 0; |
| | 0 | 114 | | foreach (var childElement in linkedChildrenElement.EnumerateArray()) |
| | | 115 | | { |
| | 0 | 116 | | Guid? childId = null; |
| | 0 | 117 | | if (childElement.TryGetProperty("ItemId", out var itemIdProp) && itemIdProp.ValueKind != JsonValueKi |
| | | 118 | | { |
| | 0 | 119 | | var itemIdStr = itemIdProp.GetString(); |
| | 0 | 120 | | if (!string.IsNullOrEmpty(itemIdStr) && Guid.TryParse(itemIdStr, out var parsedId)) |
| | | 121 | | { |
| | 0 | 122 | | childId = parsedId; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 127 | | { |
| | 0 | 128 | | if (childElement.TryGetProperty("Path", out var pathProp)) |
| | | 129 | | { |
| | 0 | 130 | | var path = pathProp.GetString(); |
| | 0 | 131 | | if (!string.IsNullOrEmpty(path) && pathToIdMap.TryGetValue(path, out var resolvedId)) |
| | | 132 | | { |
| | 0 | 133 | | childId = resolvedId; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 139 | | { |
| | 0 | 140 | | if (childElement.TryGetProperty("LibraryItemId", out var libIdProp)) |
| | | 141 | | { |
| | 0 | 142 | | var libIdStr = libIdProp.GetString(); |
| | 0 | 143 | | if (!string.IsNullOrEmpty(libIdStr) && Guid.TryParse(libIdStr, out var parsedLibId)) |
| | | 144 | | { |
| | 0 | 145 | | childId = parsedLibId; |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 151 | | { |
| | | 152 | | continue; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | var childType = LinkedChildType.Manual; |
| | 0 | 156 | | if (childElement.TryGetProperty("Type", out var typeProp)) |
| | | 157 | | { |
| | 0 | 158 | | if (typeProp.ValueKind == JsonValueKind.Number) |
| | | 159 | | { |
| | 0 | 160 | | childType = (LinkedChildType)typeProp.GetInt32(); |
| | | 161 | | } |
| | 0 | 162 | | else if (typeProp.ValueKind == JsonValueKind.String) |
| | | 163 | | { |
| | 0 | 164 | | var typeStr = typeProp.GetString(); |
| | 0 | 165 | | if (Enum.TryParse<LinkedChildType>(typeStr, out var parsedType)) |
| | | 166 | | { |
| | 0 | 167 | | childType = parsedType; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 173 | | { |
| | 0 | 174 | | ParentId = item.Id, |
| | 0 | 175 | | ChildId = childId.Value, |
| | 0 | 176 | | ChildType = childType, |
| | 0 | 177 | | SortOrder = isPlaylist ? sortOrder : null |
| | 0 | 178 | | }); |
| | | 179 | | |
| | 0 | 180 | | sortOrder++; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | processedCount++; |
| | 0 | 184 | | } |
| | 0 | 185 | | catch (JsonException ex) |
| | | 186 | | { |
| | 0 | 187 | | _logger.LogWarning(ex, "Failed to parse JSON for item {ItemId}", item.Id); |
| | 0 | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | if (linkedChildrenToAdd.Count > 0) |
| | | 192 | | { |
| | 0 | 193 | | _logger.LogInformation("Inserting {Count} LinkedChildren records.", linkedChildrenToAdd.Count); |
| | | 194 | | |
| | 0 | 195 | | var existingKeys = context.LinkedChildren |
| | 0 | 196 | | .Select(lc => new { lc.ParentId, lc.ChildId }) |
| | 0 | 197 | | .ToHashSet(); |
| | | 198 | | |
| | 0 | 199 | | var toInsert = linkedChildrenToAdd |
| | 0 | 200 | | .Where(lc => !existingKeys.Contains(new { lc.ParentId, lc.ChildId })) |
| | 0 | 201 | | .ToList(); |
| | | 202 | | |
| | 0 | 203 | | if (toInsert.Count > 0) |
| | | 204 | | { |
| | | 205 | | // Deduplicate by composite key (ParentId, ChildId) |
| | | 206 | | // Priority: LocalAlternateVersion > LinkedAlternateVersion > Other |
| | 0 | 207 | | toInsert = toInsert |
| | 0 | 208 | | .OrderBy(lc => lc.ChildType switch |
| | 0 | 209 | | { |
| | 0 | 210 | | LinkedChildType.LocalAlternateVersion => 0, |
| | 0 | 211 | | LinkedChildType.LinkedAlternateVersion => 1, |
| | 0 | 212 | | _ => 2 |
| | 0 | 213 | | }) |
| | 0 | 214 | | .DistinctBy(lc => new { lc.ParentId, lc.ChildId }) |
| | 0 | 215 | | .ToList(); |
| | | 216 | | |
| | 0 | 217 | | var childIds = toInsert.Select(lc => lc.ChildId).Distinct().ToList(); |
| | 0 | 218 | | var existingChildIds = context.BaseItems |
| | 0 | 219 | | .WhereOneOrMany(childIds, b => b.Id) |
| | 0 | 220 | | .Select(b => b.Id) |
| | 0 | 221 | | .ToHashSet(); |
| | | 222 | | |
| | 0 | 223 | | toInsert = toInsert.Where(lc => existingChildIds.Contains(lc.ChildId)).ToList(); |
| | | 224 | | |
| | 0 | 225 | | context.LinkedChildren.AddRange(toInsert); |
| | 0 | 226 | | context.SaveChanges(); |
| | | 227 | | |
| | 0 | 228 | | _logger.LogInformation("Successfully inserted {Count} LinkedChildren records.", toInsert.Count); |
| | | 229 | | } |
| | | 230 | | else |
| | | 231 | | { |
| | 0 | 232 | | _logger.LogInformation("All LinkedChildren records already exist, nothing to insert."); |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | else |
| | | 236 | | { |
| | 0 | 237 | | _logger.LogInformation("No LinkedChildren data found to migrate."); |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | _logger.LogInformation("LinkedChildren migration completed. Processed {Count} items.", processedCount); |
| | | 241 | | |
| | 0 | 242 | | CleanupWrongTypeAlternateVersions(context); |
| | 0 | 243 | | CleanupOrphanedAlternateVersionBaseItems(context); |
| | 0 | 244 | | CleanupItemsFromDeletedLibraries(context); |
| | 0 | 245 | | CleanupStaleFileEntries(context); |
| | 0 | 246 | | CleanupOrphanedLinkedChildren(context); |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | private void CleanupWrongTypeAlternateVersions(JellyfinDbContext context) |
| | | 250 | | { |
| | 0 | 251 | | _logger.LogInformation("Cleaning up alternate version items with wrong type..."); |
| | | 252 | | |
| | | 253 | | // Find all LocalAlternateVersion relationships where the child is a generic Video |
| | | 254 | | // but the parent is a more specific type (like Movie). |
| | | 255 | | // Since IDs are computed from type + path, just updating the Type column would break ID lookups. |
| | | 256 | | // Instead, delete them and let the runtime recreate them with the correct type during the next library scan. |
| | 0 | 257 | | var wrongTypeChildIds = context.LinkedChildren |
| | 0 | 258 | | .Where(lc => lc.ChildType == LinkedChildType.LocalAlternateVersion) |
| | 0 | 259 | | .Join( |
| | 0 | 260 | | context.BaseItems, |
| | 0 | 261 | | lc => lc.ParentId, |
| | 0 | 262 | | parent => parent.Id, |
| | 0 | 263 | | (lc, parent) => new { lc.ChildId, ParentType = parent.Type }) |
| | 0 | 264 | | .Join( |
| | 0 | 265 | | context.BaseItems, |
| | 0 | 266 | | x => x.ChildId, |
| | 0 | 267 | | child => child.Id, |
| | 0 | 268 | | (x, child) => new { x.ChildId, x.ParentType, ChildType = child.Type }) |
| | 0 | 269 | | .Where(x => x.ChildType != x.ParentType) |
| | 0 | 270 | | .Select(x => x.ChildId) |
| | 0 | 271 | | .Distinct() |
| | 0 | 272 | | .ToList(); |
| | | 273 | | |
| | 0 | 274 | | if (wrongTypeChildIds.Count == 0) |
| | | 275 | | { |
| | 0 | 276 | | _logger.LogInformation("No wrong-type alternate version items found."); |
| | 0 | 277 | | return; |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | _logger.LogInformation("Found {Count} wrong-type alternate version items to remove.", wrongTypeChildIds.Count); |
| | | 281 | | |
| | 0 | 282 | | var itemsToDelete = wrongTypeChildIds |
| | 0 | 283 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 284 | | .Where(item => item is not null) |
| | 0 | 285 | | .ToList(); |
| | 0 | 286 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 287 | | |
| | 0 | 288 | | _logger.LogInformation("Removed {Count} wrong-type alternate version items. They will be recreated with the corr |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | private void CleanupOrphanedAlternateVersionBaseItems(JellyfinDbContext context) |
| | | 292 | | { |
| | 0 | 293 | | _logger.LogInformation("Starting cleanup of orphaned alternate version BaseItems..."); |
| | | 294 | | |
| | | 295 | | // Find BaseItems that have OwnerId set (they belonged to another item) and are not extras, |
| | | 296 | | // but no LinkedChild entry references them — meaning they're orphaned alternate versions. |
| | | 297 | | // This happens when a version file is renamed: the old BaseItem remains in the DB |
| | | 298 | | // with a stale OwnerId but nothing links to it anymore. |
| | 0 | 299 | | var orphanedVersionIds = context.BaseItems |
| | 0 | 300 | | .Where(b => b.OwnerId.HasValue && b.ExtraType == null) |
| | 0 | 301 | | .Where(b => !context.LinkedChildren.Any(lc => lc.ChildId.Equals(b.Id))) |
| | 0 | 302 | | .Select(b => b.Id) |
| | 0 | 303 | | .ToList(); |
| | | 304 | | |
| | 0 | 305 | | if (orphanedVersionIds.Count == 0) |
| | | 306 | | { |
| | 0 | 307 | | _logger.LogInformation("No orphaned alternate version BaseItems found."); |
| | 0 | 308 | | return; |
| | | 309 | | } |
| | | 310 | | |
| | 0 | 311 | | _logger.LogInformation("Found {Count} orphaned alternate version BaseItems to remove.", orphanedVersionIds.Count |
| | | 312 | | |
| | 0 | 313 | | var itemsToDelete = orphanedVersionIds |
| | 0 | 314 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 315 | | .Where(item => item is not null) |
| | 0 | 316 | | .ToList(); |
| | 0 | 317 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 318 | | |
| | 0 | 319 | | _logger.LogInformation("Removed {Count} orphaned alternate version BaseItems.", itemsToDelete.Count); |
| | 0 | 320 | | } |
| | | 321 | | |
| | | 322 | | private void CleanupItemsFromDeletedLibraries(JellyfinDbContext context) |
| | | 323 | | { |
| | 0 | 324 | | _logger.LogInformation("Starting cleanup of items from deleted libraries..."); |
| | | 325 | | |
| | | 326 | | // Find BaseItems whose TopParentId points to a library (collection folder) that no longer exists. |
| | | 327 | | // This happens when a library is removed but the scan didn't fully clean up all items under it. |
| | 0 | 328 | | var orphanedIds = context.BaseItems |
| | 0 | 329 | | .Where(b => b.TopParentId.HasValue) |
| | 0 | 330 | | .Where(b => !context.BaseItems.Any(lib => lib.Id.Equals(b.TopParentId!.Value))) |
| | 0 | 331 | | .Select(b => b.Id) |
| | 0 | 332 | | .ToList(); |
| | | 333 | | |
| | 0 | 334 | | if (orphanedIds.Count == 0) |
| | | 335 | | { |
| | 0 | 336 | | _logger.LogInformation("No items from deleted libraries found."); |
| | 0 | 337 | | return; |
| | | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | _logger.LogInformation("Found {Count} items from deleted libraries to remove.", orphanedIds.Count); |
| | | 341 | | |
| | 0 | 342 | | var itemsToDelete = orphanedIds |
| | 0 | 343 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 344 | | .Where(item => item is not null) |
| | 0 | 345 | | .ToList(); |
| | 0 | 346 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 347 | | |
| | 0 | 348 | | _logger.LogInformation("Removed {Count} items from deleted libraries.", itemsToDelete.Count); |
| | 0 | 349 | | } |
| | | 350 | | |
| | | 351 | | private void CleanupStaleFileEntries(JellyfinDbContext context) |
| | | 352 | | { |
| | 0 | 353 | | _logger.LogInformation("Starting cleanup of items with missing files..."); |
| | | 354 | | |
| | | 355 | | // Get all library media locations and partition into accessible vs inaccessible. |
| | | 356 | | // This mirrors the scanner's safeguard: if a library root is inaccessible |
| | | 357 | | // (e.g. NAS offline), we skip items under it to avoid false deletions. |
| | 0 | 358 | | var virtualFolders = _libraryManager.GetVirtualFolders(); |
| | 0 | 359 | | var accessiblePaths = new List<string>(); |
| | 0 | 360 | | var inaccessiblePaths = new List<string>(); |
| | | 361 | | |
| | 0 | 362 | | foreach (var folder in virtualFolders) |
| | | 363 | | { |
| | 0 | 364 | | foreach (var location in folder.Locations) |
| | | 365 | | { |
| | 0 | 366 | | if (Directory.Exists(location) && Directory.EnumerateFileSystemEntries(location).Any()) |
| | | 367 | | { |
| | 0 | 368 | | accessiblePaths.Add(location); |
| | | 369 | | } |
| | | 370 | | else |
| | | 371 | | { |
| | 0 | 372 | | inaccessiblePaths.Add(location); |
| | 0 | 373 | | _logger.LogWarning( |
| | 0 | 374 | | "Library location {Path} is inaccessible or empty, skipping file existence checks for items unde |
| | 0 | 375 | | location); |
| | | 376 | | } |
| | | 377 | | } |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | var allLibraryPaths = accessiblePaths.Concat(inaccessiblePaths).ToList(); |
| | | 381 | | |
| | | 382 | | // Get all non-folder, non-virtual items with paths from the DB |
| | 0 | 383 | | var itemsWithPaths = context.BaseItems |
| | 0 | 384 | | .Where(b => b.Path != null && b.Path != string.Empty) |
| | 0 | 385 | | .Where(b => !b.IsFolder && !b.IsVirtualItem) |
| | 0 | 386 | | .Select(b => new { b.Id, b.Path }) |
| | 0 | 387 | | .ToList(); |
| | | 388 | | |
| | 0 | 389 | | var internalMetadataPath = _appPaths.InternalMetadataPath; |
| | | 390 | | |
| | 0 | 391 | | var staleIds = new List<Guid>(); |
| | 0 | 392 | | foreach (var item in itemsWithPaths) |
| | | 393 | | { |
| | | 394 | | // Expand virtual path placeholders (%AppDataPath%, %MetadataPath%) to real paths |
| | 0 | 395 | | var path = _appHost.ExpandVirtualPath(item.Path!); |
| | | 396 | | |
| | | 397 | | // Skip items stored under internal metadata (images, subtitles, trickplay, etc.) |
| | 0 | 398 | | if (path.StartsWith(internalMetadataPath, StringComparison.OrdinalIgnoreCase)) |
| | | 399 | | { |
| | | 400 | | continue; |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | if (accessiblePaths.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase))) |
| | | 404 | | { |
| | | 405 | | // Item is under an accessible library location — check if it still exists |
| | | 406 | | // Directory check covers BDMV/DVD items whose Path points to a folder |
| | 0 | 407 | | if (!File.Exists(path) && !Directory.Exists(path)) |
| | | 408 | | { |
| | 0 | 409 | | staleIds.Add(item.Id); |
| | | 410 | | } |
| | | 411 | | } |
| | 0 | 412 | | else if (!allLibraryPaths.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase))) |
| | | 413 | | { |
| | | 414 | | // Item is not under ANY library location (accessible or not) — |
| | | 415 | | // it's orphaned from all libraries (e.g. media path was removed from config) |
| | 0 | 416 | | staleIds.Add(item.Id); |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | // Otherwise: item is under an inaccessible location — skip (storage may be offline) |
| | | 420 | | } |
| | | 421 | | |
| | 0 | 422 | | if (staleIds.Count == 0) |
| | | 423 | | { |
| | 0 | 424 | | _logger.LogInformation("No stale items found."); |
| | 0 | 425 | | return; |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | _logger.LogInformation("Found {Count} stale items to remove.", staleIds.Count); |
| | | 429 | | |
| | 0 | 430 | | var itemsToDelete = staleIds |
| | 0 | 431 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 432 | | .Where(item => item is not null) |
| | 0 | 433 | | .ToList(); |
| | 0 | 434 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 435 | | |
| | 0 | 436 | | _logger.LogInformation("Removed {Count} stale items.", itemsToDelete.Count); |
| | 0 | 437 | | } |
| | | 438 | | |
| | | 439 | | private void CleanupOrphanedLinkedChildren(JellyfinDbContext context) |
| | | 440 | | { |
| | 0 | 441 | | _logger.LogInformation("Starting cleanup of orphaned LinkedChildren records..."); |
| | | 442 | | |
| | | 443 | | // Find all LinkedChildren where the ChildId doesn't exist in BaseItems |
| | 0 | 444 | | var orphanedLinkedChildren = context.LinkedChildren |
| | 0 | 445 | | .Where(lc => !context.BaseItems.Any(b => b.Id.Equals(lc.ChildId))) |
| | 0 | 446 | | .ToList(); |
| | | 447 | | |
| | 0 | 448 | | if (orphanedLinkedChildren.Count == 0) |
| | | 449 | | { |
| | 0 | 450 | | _logger.LogInformation("No orphaned LinkedChildren found."); |
| | 0 | 451 | | return; |
| | | 452 | | } |
| | | 453 | | |
| | 0 | 454 | | _logger.LogInformation("Found {Count} orphaned LinkedChildren records to remove.", orphanedLinkedChildren.Count) |
| | | 455 | | |
| | 0 | 456 | | var orphanedByParent = context.LinkedChildren |
| | 0 | 457 | | .Where(lc => !context.BaseItems.Any(b => b.Id.Equals(lc.ParentId))) |
| | 0 | 458 | | .ToList(); |
| | | 459 | | |
| | 0 | 460 | | if (orphanedByParent.Count > 0) |
| | | 461 | | { |
| | 0 | 462 | | _logger.LogInformation("Found {Count} LinkedChildren with non-existent parent.", orphanedByParent.Count); |
| | 0 | 463 | | orphanedLinkedChildren.AddRange(orphanedByParent); |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | // Remove all orphaned records |
| | 0 | 467 | | var distinctOrphaned = orphanedLinkedChildren.DistinctBy(lc => new { lc.ParentId, lc.ChildId }).ToList(); |
| | 0 | 468 | | context.LinkedChildren.RemoveRange(distinctOrphaned); |
| | 0 | 469 | | context.SaveChanges(); |
| | | 470 | | |
| | 0 | 471 | | _logger.LogInformation("Successfully removed {Count} orphaned LinkedChildren records.", distinctOrphaned.Count); |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | private void ProcessVideoAlternateVersions( |
| | | 475 | | JsonElement root, |
| | | 476 | | Guid parentId, |
| | | 477 | | Dictionary<string, Guid> pathToIdMap, |
| | | 478 | | List<LinkedChildEntity> linkedChildrenToAdd) |
| | | 479 | | { |
| | 0 | 480 | | int sortOrder = 0; |
| | | 481 | | |
| | 0 | 482 | | if (root.TryGetProperty("LocalAlternateVersions", out var localAlternateVersionsElement) |
| | 0 | 483 | | && localAlternateVersionsElement.ValueKind == JsonValueKind.Array) |
| | | 484 | | { |
| | 0 | 485 | | foreach (var pathElement in localAlternateVersionsElement.EnumerateArray()) |
| | | 486 | | { |
| | 0 | 487 | | if (pathElement.ValueKind != JsonValueKind.String) |
| | | 488 | | { |
| | | 489 | | continue; |
| | | 490 | | } |
| | | 491 | | |
| | 0 | 492 | | var path = pathElement.GetString(); |
| | 0 | 493 | | if (string.IsNullOrEmpty(path)) |
| | | 494 | | { |
| | | 495 | | continue; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | // Try to resolve the path to an ItemId |
| | 0 | 499 | | if (pathToIdMap.TryGetValue(path, out var childId)) |
| | | 500 | | { |
| | 0 | 501 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 502 | | { |
| | 0 | 503 | | ParentId = parentId, |
| | 0 | 504 | | ChildId = childId, |
| | 0 | 505 | | ChildType = LinkedChildType.LocalAlternateVersion, |
| | 0 | 506 | | SortOrder = sortOrder++ |
| | 0 | 507 | | }); |
| | | 508 | | |
| | 0 | 509 | | _logger.LogDebug( |
| | 0 | 510 | | "Migrating LocalAlternateVersion: Parent={ParentId}, Child={ChildId}, Path={Path}", |
| | 0 | 511 | | parentId, |
| | 0 | 512 | | childId, |
| | 0 | 513 | | path); |
| | | 514 | | } |
| | | 515 | | else |
| | | 516 | | { |
| | 0 | 517 | | _logger.LogWarning( |
| | 0 | 518 | | "Could not resolve LocalAlternateVersion path to ItemId: {Path} for parent {ParentId}", |
| | 0 | 519 | | path, |
| | 0 | 520 | | parentId); |
| | | 521 | | } |
| | | 522 | | } |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | if (root.TryGetProperty("LinkedAlternateVersions", out var linkedAlternateVersionsElement) |
| | 0 | 526 | | && linkedAlternateVersionsElement.ValueKind == JsonValueKind.Array) |
| | | 527 | | { |
| | 0 | 528 | | foreach (var linkedChildElement in linkedAlternateVersionsElement.EnumerateArray()) |
| | | 529 | | { |
| | 0 | 530 | | Guid? childId = null; |
| | | 531 | | |
| | | 532 | | // Try to get ItemId |
| | 0 | 533 | | if (linkedChildElement.TryGetProperty("ItemId", out var itemIdProp) && itemIdProp.ValueKind != JsonValue |
| | | 534 | | { |
| | 0 | 535 | | var itemIdStr = itemIdProp.GetString(); |
| | 0 | 536 | | if (!string.IsNullOrEmpty(itemIdStr) && Guid.TryParse(itemIdStr, out var parsedId)) |
| | | 537 | | { |
| | 0 | 538 | | childId = parsedId; |
| | | 539 | | } |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | // Try to get from Path if ItemId not available |
| | 0 | 543 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 544 | | { |
| | 0 | 545 | | if (linkedChildElement.TryGetProperty("Path", out var pathProp)) |
| | | 546 | | { |
| | 0 | 547 | | var path = pathProp.GetString(); |
| | 0 | 548 | | if (!string.IsNullOrEmpty(path) && pathToIdMap.TryGetValue(path, out var resolvedId)) |
| | | 549 | | { |
| | 0 | 550 | | childId = resolvedId; |
| | | 551 | | } |
| | | 552 | | } |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | // Try LibraryItemId as fallback |
| | 0 | 556 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 557 | | { |
| | 0 | 558 | | if (linkedChildElement.TryGetProperty("LibraryItemId", out var libIdProp)) |
| | | 559 | | { |
| | 0 | 560 | | var libIdStr = libIdProp.GetString(); |
| | 0 | 561 | | if (!string.IsNullOrEmpty(libIdStr) && Guid.TryParse(libIdStr, out var parsedLibId)) |
| | | 562 | | { |
| | 0 | 563 | | childId = parsedLibId; |
| | | 564 | | } |
| | | 565 | | } |
| | | 566 | | } |
| | | 567 | | |
| | 0 | 568 | | if (!childId.HasValue || childId.Value.IsEmpty()) |
| | | 569 | | { |
| | 0 | 570 | | _logger.LogWarning("Could not resolve LinkedAlternateVersion child ID for parent {ParentId}", parent |
| | 0 | 571 | | continue; |
| | | 572 | | } |
| | | 573 | | |
| | 0 | 574 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 575 | | { |
| | 0 | 576 | | ParentId = parentId, |
| | 0 | 577 | | ChildId = childId.Value, |
| | 0 | 578 | | ChildType = LinkedChildType.LinkedAlternateVersion, |
| | 0 | 579 | | SortOrder = sortOrder++ |
| | 0 | 580 | | }); |
| | | 581 | | |
| | 0 | 582 | | _logger.LogDebug( |
| | 0 | 583 | | "Migrating LinkedAlternateVersion: Parent={ParentId}, Child={ChildId}", |
| | 0 | 584 | | parentId, |
| | 0 | 585 | | childId.Value); |
| | | 586 | | } |
| | | 587 | | } |
| | 0 | 588 | | } |
| | | 589 | | } |