| | | 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.Entities; |
| | | 11 | | using MediaBrowser.Controller.Library; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using LinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType; |
| | | 15 | | |
| | | 16 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Migrates LinkedChildren data from JSON Data column to the LinkedChildren table. |
| | | 20 | | /// </summary> |
| | | 21 | | [JellyfinMigration("2026-01-13T12:00:00", nameof(MigrateLinkedChildren))] |
| | | 22 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 23 | | internal class MigrateLinkedChildren : IDatabaseMigrationRoutine |
| | | 24 | | { |
| | | 25 | | private readonly ILogger<MigrateLinkedChildren> _logger; |
| | | 26 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 27 | | private readonly ILibraryManager _libraryManager; |
| | | 28 | | private readonly IServerApplicationHost _appHost; |
| | | 29 | | private readonly IServerApplicationPaths _appPaths; |
| | | 30 | | |
| | | 31 | | public MigrateLinkedChildren( |
| | | 32 | | ILoggerFactory loggerFactory, |
| | | 33 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 34 | | ILibraryManager libraryManager, |
| | | 35 | | IServerApplicationHost appHost, |
| | | 36 | | IServerApplicationPaths appPaths) |
| | | 37 | | { |
| | 0 | 38 | | _logger = loggerFactory.CreateLogger<MigrateLinkedChildren>(); |
| | 0 | 39 | | _dbProvider = dbProvider; |
| | 0 | 40 | | _libraryManager = libraryManager; |
| | 0 | 41 | | _appHost = appHost; |
| | 0 | 42 | | _appPaths = appPaths; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | | 46 | | public void Perform() |
| | | 47 | | { |
| | 0 | 48 | | using var context = _dbProvider.CreateDbContext(); |
| | | 49 | | |
| | 0 | 50 | | var containerTypes = new[] |
| | 0 | 51 | | { |
| | 0 | 52 | | "MediaBrowser.Controller.Entities.Movies.BoxSet", |
| | 0 | 53 | | "MediaBrowser.Controller.Playlists.Playlist", |
| | 0 | 54 | | "MediaBrowser.Controller.Entities.CollectionFolder" |
| | 0 | 55 | | }; |
| | | 56 | | |
| | 0 | 57 | | var videoTypes = new[] |
| | 0 | 58 | | { |
| | 0 | 59 | | "MediaBrowser.Controller.Entities.Video", |
| | 0 | 60 | | "MediaBrowser.Controller.Entities.Movies.Movie", |
| | 0 | 61 | | "MediaBrowser.Controller.Entities.TV.Episode" |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | var itemsWithData = context.BaseItems |
| | 0 | 65 | | .Where(b => b.Data != null && (containerTypes.Contains(b.Type) || videoTypes.Contains(b.Type))) |
| | 0 | 66 | | .Select(b => new { b.Id, b.Data, b.Type, b.Path, b.IsFolder }) |
| | 0 | 67 | | .ToList(); |
| | | 68 | | |
| | 0 | 69 | | _logger.LogInformation("Found {Count} potential items with LinkedChildren data to process.", itemsWithData.Count |
| | | 70 | | |
| | 0 | 71 | | var pathToIdMap = context.BaseItems |
| | 0 | 72 | | .Where(b => b.Path != null) |
| | 0 | 73 | | .Select(b => new { b.Id, b.Path }) |
| | 0 | 74 | | .GroupBy(b => b.Path!) |
| | 0 | 75 | | .ToDictionary(g => g.Key, g => g.First().Id); |
| | | 76 | | |
| | | 77 | | // Needed to tell a stale cached ItemId apart from one that still points at a real item. |
| | 0 | 78 | | var allItemIds = context.BaseItems.Select(b => b.Id).ToHashSet(); |
| | | 79 | | |
| | 0 | 80 | | var playlistParentIds = itemsWithData |
| | 0 | 81 | | .Where(b => b.Type == "MediaBrowser.Controller.Playlists.Playlist") |
| | 0 | 82 | | .Select(b => b.Id) |
| | 0 | 83 | | .ToHashSet(); |
| | | 84 | | |
| | 0 | 85 | | var droppedChildren = 0; |
| | 0 | 86 | | var linkedChildrenToAdd = new List<LinkedChildEntity>(); |
| | 0 | 87 | | var processedCount = 0; |
| | | 88 | | const int progressLogStep = 1000; |
| | 0 | 89 | | var totalItems = itemsWithData.Count; |
| | | 90 | | |
| | 0 | 91 | | foreach (var item in itemsWithData) |
| | | 92 | | { |
| | 0 | 93 | | if (string.IsNullOrEmpty(item.Data)) |
| | | 94 | | { |
| | | 95 | | continue; |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if (processedCount > 0 && processedCount % progressLogStep == 0) |
| | | 99 | | { |
| | 0 | 100 | | _logger.LogInformation("Processing LinkedChildren: {Processed}/{Total} items", processedCount, totalItem |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | try |
| | | 104 | | { |
| | 0 | 105 | | using var doc = JsonDocument.Parse(item.Data); |
| | | 106 | | |
| | 0 | 107 | | var isVideo = videoTypes.Contains(item.Type); |
| | | 108 | | |
| | | 109 | | // Handle Video alternate versions |
| | 0 | 110 | | if (isVideo) |
| | | 111 | | { |
| | 0 | 112 | | ProcessVideoAlternateVersions(doc.RootElement, item.Id, pathToIdMap, allItemIds, linkedChildrenToAdd |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Handle LinkedChildren (for containers and other items) |
| | 0 | 116 | | if (!doc.RootElement.TryGetProperty("LinkedChildren", out var linkedChildrenElement) || linkedChildrenEl |
| | | 117 | | { |
| | 0 | 118 | | processedCount++; |
| | 0 | 119 | | continue; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // Legacy entries may hold a path relative to the container that holds them, so the |
| | | 123 | | // container's own location has to be a real path, not a virtual one. |
| | 0 | 124 | | var itemPath = item.Path is null ? null : _appHost.ExpandVirtualPath(item.Path); |
| | 0 | 125 | | var containingFolderPath = item.IsFolder ? itemPath : Path.GetDirectoryName(itemPath); |
| | 0 | 126 | | var sortOrder = 0; |
| | 0 | 127 | | foreach (var childElement in linkedChildrenElement.EnumerateArray()) |
| | | 128 | | { |
| | 0 | 129 | | var childId = ResolveChildId(childElement, containingFolderPath, pathToIdMap, allItemIds); |
| | 0 | 130 | | if (!childId.HasValue) |
| | | 131 | | { |
| | 0 | 132 | | droppedChildren++; |
| | 0 | 133 | | _logger.LogWarning( |
| | 0 | 134 | | "Dropping unresolvable LinkedChild of {ParentId}: ItemId {ItemId}, path {ChildPath}", |
| | 0 | 135 | | item.Id, |
| | 0 | 136 | | GetStringProperty(childElement, "ItemId") ?? "none", |
| | 0 | 137 | | GetStringProperty(childElement, "Path") ?? "none"); |
| | 0 | 138 | | continue; |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | var childType = LinkedChildType.Manual; |
| | 0 | 142 | | if (childElement.TryGetProperty("Type", out var typeProp)) |
| | | 143 | | { |
| | 0 | 144 | | if (typeProp.ValueKind == JsonValueKind.Number) |
| | | 145 | | { |
| | 0 | 146 | | childType = (LinkedChildType)typeProp.GetInt32(); |
| | | 147 | | } |
| | 0 | 148 | | else if (typeProp.ValueKind == JsonValueKind.String) |
| | | 149 | | { |
| | 0 | 150 | | var typeStr = typeProp.GetString(); |
| | 0 | 151 | | if (Enum.TryParse<LinkedChildType>(typeStr, out var parsedType)) |
| | | 152 | | { |
| | 0 | 153 | | childType = parsedType; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 159 | | { |
| | 0 | 160 | | ParentId = item.Id, |
| | 0 | 161 | | ChildId = childId.Value, |
| | 0 | 162 | | ChildType = childType, |
| | 0 | 163 | | SortOrder = sortOrder |
| | 0 | 164 | | }); |
| | | 165 | | |
| | 0 | 166 | | sortOrder++; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | processedCount++; |
| | 0 | 170 | | } |
| | 0 | 171 | | catch (JsonException ex) |
| | | 172 | | { |
| | 0 | 173 | | _logger.LogWarning(ex, "Failed to parse JSON for item {ItemId}", item.Id); |
| | 0 | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | if (linkedChildrenToAdd.Count > 0) |
| | | 178 | | { |
| | 0 | 179 | | _logger.LogInformation("Inserting {Count} LinkedChildren records.", linkedChildrenToAdd.Count); |
| | | 180 | | |
| | 0 | 181 | | var existingKeys = context.LinkedChildren |
| | 0 | 182 | | .Select(lc => new { lc.ParentId, lc.ChildId }) |
| | 0 | 183 | | .ToHashSet(); |
| | | 184 | | |
| | | 185 | | // A playlist may list the same child more than once, so it cannot be keyed by |
| | | 186 | | // (ParentId, ChildId): skip a playlist wholesale if it already has rows instead, which |
| | | 187 | | // keeps the routine re-runnable without collapsing repeated entries. |
| | 0 | 188 | | var populatedParentIds = context.LinkedChildren |
| | 0 | 189 | | .Select(lc => lc.ParentId) |
| | 0 | 190 | | .Distinct() |
| | 0 | 191 | | .ToHashSet(); |
| | | 192 | | |
| | 0 | 193 | | var toInsert = linkedChildrenToAdd |
| | 0 | 194 | | .Where(lc => playlistParentIds.Contains(lc.ParentId) |
| | 0 | 195 | | ? !populatedParentIds.Contains(lc.ParentId) |
| | 0 | 196 | | : !existingKeys.Contains(new { lc.ParentId, lc.ChildId })) |
| | 0 | 197 | | .ToList(); |
| | | 198 | | |
| | 0 | 199 | | if (toInsert.Count > 0) |
| | | 200 | | { |
| | | 201 | | // Every container type other than a playlist keeps a single entry per child. |
| | | 202 | | // Priority: LocalAlternateVersion > LinkedAlternateVersion > Other |
| | 0 | 203 | | toInsert = |
| | 0 | 204 | | [ |
| | 0 | 205 | | .. toInsert.Where(lc => playlistParentIds.Contains(lc.ParentId)), |
| | 0 | 206 | | .. toInsert |
| | 0 | 207 | | .Where(lc => !playlistParentIds.Contains(lc.ParentId)) |
| | 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 | | ]; |
| | | 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 | | |
| | | 225 | | // Drop linked (user-merged) entries that point at items the parent owns (local |
| | | 226 | | // file-based alternates or extras). These stem from legacy data that merged an |
| | | 227 | | // owned item onto its own primary and would wrongly mark server-merged groups |
| | | 228 | | // as user-merged (splittable). |
| | 0 | 229 | | var linkedChildIds = toInsert |
| | 0 | 230 | | .Where(lc => lc.ChildType == LinkedChildType.LinkedAlternateVersion) |
| | 0 | 231 | | .Select(lc => lc.ChildId) |
| | 0 | 232 | | .Distinct() |
| | 0 | 233 | | .ToList(); |
| | | 234 | | |
| | 0 | 235 | | if (linkedChildIds.Count > 0) |
| | | 236 | | { |
| | 0 | 237 | | var ownerIdByChildId = context.BaseItems |
| | 0 | 238 | | .WhereOneOrMany(linkedChildIds, b => b.Id) |
| | 0 | 239 | | .Where(b => b.OwnerId.HasValue) |
| | 0 | 240 | | .Select(b => new { b.Id, b.OwnerId }) |
| | 0 | 241 | | .ToDictionary(b => b.Id, b => b.OwnerId!.Value); |
| | | 242 | | |
| | 0 | 243 | | var removedCount = toInsert.RemoveAll(lc => |
| | 0 | 244 | | lc.ChildType == LinkedChildType.LinkedAlternateVersion |
| | 0 | 245 | | && ownerIdByChildId.TryGetValue(lc.ChildId, out var ownerId) |
| | 0 | 246 | | && ownerId.Equals(lc.ParentId)); |
| | | 247 | | |
| | 0 | 248 | | if (removedCount > 0) |
| | | 249 | | { |
| | 0 | 250 | | _logger.LogInformation("Skipped {Count} LinkedAlternateVersion records pointing at items owned b |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | context.LinkedChildren.AddRange(toInsert); |
| | 0 | 255 | | context.SaveChanges(); |
| | | 256 | | |
| | 0 | 257 | | _logger.LogInformation("Successfully inserted {Count} LinkedChildren records.", toInsert.Count); |
| | | 258 | | } |
| | | 259 | | else |
| | | 260 | | { |
| | 0 | 261 | | _logger.LogInformation("All LinkedChildren records already exist, nothing to insert."); |
| | | 262 | | } |
| | | 263 | | } |
| | | 264 | | else |
| | | 265 | | { |
| | 0 | 266 | | _logger.LogInformation("No LinkedChildren data found to migrate."); |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | _logger.LogInformation( |
| | 0 | 270 | | "LinkedChildren migration completed. Processed {Count} items, dropped {DroppedCount} unresolvable children." |
| | 0 | 271 | | processedCount, |
| | 0 | 272 | | droppedChildren); |
| | | 273 | | |
| | 0 | 274 | | CleanupWrongTypeAlternateVersions(context); |
| | 0 | 275 | | CleanupOrphanedAlternateVersionBaseItems(context); |
| | 0 | 276 | | CleanupItemsFromDeletedLibraries(context); |
| | 0 | 277 | | CleanupStaleFileEntries(context); |
| | 0 | 278 | | CleanupOrphanedLinkedChildren(context); |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | private void CleanupWrongTypeAlternateVersions(JellyfinDbContext context) |
| | | 282 | | { |
| | 0 | 283 | | _logger.LogInformation("Cleaning up alternate version items with wrong type..."); |
| | | 284 | | |
| | | 285 | | // Find all LocalAlternateVersion relationships where the child is a generic Video |
| | | 286 | | // but the parent is a more specific type (like Movie). |
| | | 287 | | // Since IDs are computed from type + path, just updating the Type column would break ID lookups. |
| | | 288 | | // Instead, delete them and let the runtime recreate them with the correct type during the next library scan. |
| | 0 | 289 | | var wrongTypeChildIds = context.LinkedChildren |
| | 0 | 290 | | .Where(lc => lc.ChildType == LinkedChildType.LocalAlternateVersion) |
| | 0 | 291 | | .Join( |
| | 0 | 292 | | context.BaseItems, |
| | 0 | 293 | | lc => lc.ParentId, |
| | 0 | 294 | | parent => parent.Id, |
| | 0 | 295 | | (lc, parent) => new { lc.ChildId, ParentType = parent.Type }) |
| | 0 | 296 | | .Join( |
| | 0 | 297 | | context.BaseItems, |
| | 0 | 298 | | x => x.ChildId, |
| | 0 | 299 | | child => child.Id, |
| | 0 | 300 | | (x, child) => new { x.ChildId, x.ParentType, ChildType = child.Type }) |
| | 0 | 301 | | .Where(x => x.ChildType != x.ParentType) |
| | 0 | 302 | | .Select(x => x.ChildId) |
| | 0 | 303 | | .Distinct() |
| | 0 | 304 | | .ToList(); |
| | | 305 | | |
| | 0 | 306 | | if (wrongTypeChildIds.Count == 0) |
| | | 307 | | { |
| | 0 | 308 | | _logger.LogInformation("No wrong-type alternate version items found."); |
| | 0 | 309 | | return; |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | _logger.LogInformation("Found {Count} wrong-type alternate version items to remove.", wrongTypeChildIds.Count); |
| | | 313 | | |
| | 0 | 314 | | var itemsToDelete = wrongTypeChildIds |
| | 0 | 315 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 316 | | .Where(item => item is not null) |
| | 0 | 317 | | .ToList(); |
| | 0 | 318 | | var deleted = DeleteItems(itemsToDelete!); |
| | | 319 | | |
| | 0 | 320 | | _logger.LogInformation("Removed {Count} wrong-type alternate version items. They will be recreated with the corr |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | private void CleanupOrphanedAlternateVersionBaseItems(JellyfinDbContext context) |
| | | 324 | | { |
| | 0 | 325 | | _logger.LogInformation("Starting cleanup of orphaned alternate version BaseItems..."); |
| | | 326 | | |
| | | 327 | | // Find BaseItems that have OwnerId set (they belonged to another item) and are not extras, |
| | | 328 | | // but no LinkedChild entry references them — meaning they're orphaned alternate versions. |
| | | 329 | | // This happens when a version file is renamed: the old BaseItem remains in the DB |
| | | 330 | | // with a stale OwnerId but nothing links to it anymore. |
| | 0 | 331 | | var orphanedVersionIds = context.BaseItems |
| | 0 | 332 | | .Where(b => b.OwnerId.HasValue && b.ExtraType == null) |
| | 0 | 333 | | .Where(b => !context.LinkedChildren.Any(lc => lc.ChildId.Equals(b.Id))) |
| | 0 | 334 | | .Select(b => b.Id) |
| | 0 | 335 | | .ToList(); |
| | | 336 | | |
| | 0 | 337 | | if (orphanedVersionIds.Count == 0) |
| | | 338 | | { |
| | 0 | 339 | | _logger.LogInformation("No orphaned alternate version BaseItems found."); |
| | 0 | 340 | | return; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | _logger.LogInformation("Found {Count} orphaned alternate version BaseItems to remove.", orphanedVersionIds.Count |
| | | 344 | | |
| | 0 | 345 | | var itemsToDelete = orphanedVersionIds |
| | 0 | 346 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 347 | | .Where(item => item is not null) |
| | 0 | 348 | | .ToList(); |
| | 0 | 349 | | var deleted = DeleteItems(itemsToDelete!); |
| | | 350 | | |
| | 0 | 351 | | _logger.LogInformation("Removed {Count} orphaned alternate version BaseItems.", deleted); |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | private void CleanupItemsFromDeletedLibraries(JellyfinDbContext context) |
| | | 355 | | { |
| | 0 | 356 | | _logger.LogInformation("Starting cleanup of items from deleted libraries..."); |
| | | 357 | | |
| | | 358 | | // Find BaseItems whose TopParentId points to a library (collection folder) that no longer exists. |
| | | 359 | | // This happens when a library is removed but the scan didn't fully clean up all items under it. |
| | 0 | 360 | | var orphanedIds = context.BaseItems |
| | 0 | 361 | | .Where(b => b.TopParentId.HasValue) |
| | 0 | 362 | | .Where(b => !context.BaseItems.Any(lib => lib.Id.Equals(b.TopParentId!.Value))) |
| | 0 | 363 | | .Select(b => b.Id) |
| | 0 | 364 | | .ToList(); |
| | | 365 | | |
| | 0 | 366 | | if (orphanedIds.Count == 0) |
| | | 367 | | { |
| | 0 | 368 | | _logger.LogInformation("No items from deleted libraries found."); |
| | 0 | 369 | | return; |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | _logger.LogInformation("Found {Count} items from deleted libraries to remove.", orphanedIds.Count); |
| | | 373 | | |
| | 0 | 374 | | var itemsToDelete = orphanedIds |
| | 0 | 375 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 376 | | .Where(item => item is not null) |
| | 0 | 377 | | .ToList(); |
| | 0 | 378 | | var deleted = DeleteItems(itemsToDelete!); |
| | | 379 | | |
| | 0 | 380 | | _logger.LogInformation("Removed {Count} items from deleted libraries.", deleted); |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | private void CleanupStaleFileEntries(JellyfinDbContext context) |
| | | 384 | | { |
| | 0 | 385 | | _logger.LogInformation("Starting cleanup of items with missing files..."); |
| | | 386 | | |
| | | 387 | | // Get all library media locations and partition into accessible vs inaccessible. |
| | | 388 | | // This mirrors the scanner's safeguard: if a library root is inaccessible |
| | | 389 | | // (e.g. NAS offline), we skip items under it to avoid false deletions. |
| | 0 | 390 | | var virtualFolders = _libraryManager.GetVirtualFolders(); |
| | 0 | 391 | | var accessiblePaths = new List<string>(); |
| | 0 | 392 | | var inaccessiblePaths = new List<string>(); |
| | | 393 | | |
| | 0 | 394 | | foreach (var folder in virtualFolders) |
| | | 395 | | { |
| | 0 | 396 | | foreach (var location in folder.Locations) |
| | | 397 | | { |
| | 0 | 398 | | if (Directory.Exists(location) && Directory.EnumerateFileSystemEntries(location).Any()) |
| | | 399 | | { |
| | 0 | 400 | | accessiblePaths.Add(location); |
| | | 401 | | } |
| | | 402 | | else |
| | | 403 | | { |
| | 0 | 404 | | inaccessiblePaths.Add(location); |
| | 0 | 405 | | _logger.LogWarning( |
| | 0 | 406 | | "Library location {Path} is inaccessible or empty, skipping file existence checks for items unde |
| | 0 | 407 | | location); |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | var allLibraryPaths = accessiblePaths.Concat(inaccessiblePaths).ToList(); |
| | | 413 | | |
| | | 414 | | // Get all non-folder, non-virtual items with paths from the DB |
| | 0 | 415 | | var itemsWithPaths = context.BaseItems |
| | 0 | 416 | | .Where(b => b.Path != null && b.Path != string.Empty) |
| | 0 | 417 | | .Where(b => !b.IsFolder && !b.IsVirtualItem) |
| | 0 | 418 | | .Select(b => new { b.Id, b.Path }) |
| | 0 | 419 | | .ToList(); |
| | | 420 | | |
| | 0 | 421 | | var internalMetadataPath = _appPaths.InternalMetadataPath; |
| | | 422 | | |
| | | 423 | | // An item outside every library location is normally left over from a removed media path, but |
| | | 424 | | // it looks exactly the same as one whose storage failed to mount (a wrong bind mount on the |
| | | 425 | | // first container start, for example). Only act on it while every location is readable. |
| | 0 | 426 | | var canRemoveUnrootedItems = inaccessiblePaths.Count == 0; |
| | 0 | 427 | | var skippedUnrootedItems = 0; |
| | | 428 | | |
| | 0 | 429 | | var staleIds = new List<Guid>(); |
| | 0 | 430 | | foreach (var item in itemsWithPaths) |
| | | 431 | | { |
| | | 432 | | // Expand virtual path placeholders (%AppDataPath%, %MetadataPath%) to real paths |
| | 0 | 433 | | var path = _appHost.ExpandVirtualPath(item.Path!); |
| | | 434 | | |
| | | 435 | | // Skip items stored under internal metadata (images, subtitles, trickplay, etc.) |
| | 0 | 436 | | if (path.StartsWith(internalMetadataPath, StringComparison.OrdinalIgnoreCase)) |
| | | 437 | | { |
| | | 438 | | continue; |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | if (accessiblePaths.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase))) |
| | | 442 | | { |
| | | 443 | | // Item is under an accessible library location — check if it still exists |
| | | 444 | | // Directory check covers BDMV/DVD items whose Path points to a folder |
| | 0 | 445 | | if (!File.Exists(path) && !Directory.Exists(path)) |
| | | 446 | | { |
| | 0 | 447 | | _logger.LogDebug("Removing item {ItemId}: file {Path} no longer exists.", item.Id, path); |
| | 0 | 448 | | staleIds.Add(item.Id); |
| | | 449 | | } |
| | | 450 | | } |
| | 0 | 451 | | else if (!allLibraryPaths.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase))) |
| | | 452 | | { |
| | | 453 | | // Item is not under ANY library location (accessible or not) — |
| | | 454 | | // it's orphaned from all libraries (e.g. media path was removed from config) |
| | 0 | 455 | | if (canRemoveUnrootedItems) |
| | | 456 | | { |
| | 0 | 457 | | _logger.LogDebug("Removing item {ItemId}: path {Path} is outside every library location.", item.Id, |
| | 0 | 458 | | staleIds.Add(item.Id); |
| | | 459 | | } |
| | | 460 | | else |
| | | 461 | | { |
| | 0 | 462 | | skippedUnrootedItems++; |
| | | 463 | | } |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | // Otherwise: item is under an inaccessible location — skip (storage may be offline) |
| | | 467 | | } |
| | | 468 | | |
| | 0 | 469 | | if (skippedUnrootedItems > 0) |
| | | 470 | | { |
| | 0 | 471 | | _logger.LogWarning( |
| | 0 | 472 | | "Keeping {Count} items that are outside every library location because {LocationCount} library location( |
| | 0 | 473 | | skippedUnrootedItems, |
| | 0 | 474 | | inaccessiblePaths.Count); |
| | | 475 | | } |
| | | 476 | | |
| | 0 | 477 | | if (staleIds.Count == 0) |
| | | 478 | | { |
| | 0 | 479 | | _logger.LogInformation("No stale items found."); |
| | 0 | 480 | | return; |
| | | 481 | | } |
| | | 482 | | |
| | 0 | 483 | | _logger.LogInformation("Found {Count} stale items to remove.", staleIds.Count); |
| | | 484 | | |
| | 0 | 485 | | var itemsToDelete = staleIds |
| | 0 | 486 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 487 | | .Where(item => item is not null) |
| | 0 | 488 | | .ToList(); |
| | 0 | 489 | | var deleted = DeleteItems(itemsToDelete!); |
| | | 490 | | |
| | 0 | 491 | | _logger.LogInformation("Removed {Count} stale items.", deleted); |
| | 0 | 492 | | } |
| | | 493 | | |
| | | 494 | | private int DeleteItems(IReadOnlyCollection<BaseItem> items) |
| | | 495 | | { |
| | 0 | 496 | | if (items.Count == 0) |
| | | 497 | | { |
| | 0 | 498 | | return 0; |
| | | 499 | | } |
| | | 500 | | |
| | 0 | 501 | | var options = new DeleteOptions { DeleteFileLocation = false, DeleteFromExternalProvider = false }; |
| | 0 | 502 | | var deleted = 0; |
| | 0 | 503 | | foreach (var item in items) |
| | | 504 | | { |
| | | 505 | | try |
| | | 506 | | { |
| | 0 | 507 | | _libraryManager.DeleteItem(item, options); |
| | 0 | 508 | | deleted++; |
| | 0 | 509 | | } |
| | 0 | 510 | | catch (Exception ex) |
| | | 511 | | { |
| | 0 | 512 | | _logger.LogWarning(ex, "Skipping item {ItemId} ({ItemName}): delete failed.", item.Id, item.Name ?? "Unk |
| | 0 | 513 | | } |
| | | 514 | | } |
| | | 515 | | |
| | 0 | 516 | | return deleted; |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | private void CleanupOrphanedLinkedChildren(JellyfinDbContext context) |
| | | 520 | | { |
| | 0 | 521 | | _logger.LogInformation("Starting cleanup of orphaned LinkedChildren records..."); |
| | | 522 | | |
| | | 523 | | // Find all LinkedChildren where the ChildId doesn't exist in BaseItems |
| | 0 | 524 | | var orphanedLinkedChildren = context.LinkedChildren |
| | 0 | 525 | | .Where(lc => !context.BaseItems.Any(b => b.Id.Equals(lc.ChildId))) |
| | 0 | 526 | | .ToList(); |
| | | 527 | | |
| | 0 | 528 | | if (orphanedLinkedChildren.Count == 0) |
| | | 529 | | { |
| | 0 | 530 | | _logger.LogInformation("No orphaned LinkedChildren found."); |
| | 0 | 531 | | return; |
| | | 532 | | } |
| | | 533 | | |
| | 0 | 534 | | _logger.LogInformation("Found {Count} orphaned LinkedChildren records to remove.", orphanedLinkedChildren.Count) |
| | | 535 | | |
| | 0 | 536 | | var orphanedByParent = context.LinkedChildren |
| | 0 | 537 | | .Where(lc => !context.BaseItems.Any(b => b.Id.Equals(lc.ParentId))) |
| | 0 | 538 | | .ToList(); |
| | | 539 | | |
| | 0 | 540 | | if (orphanedByParent.Count > 0) |
| | | 541 | | { |
| | 0 | 542 | | _logger.LogInformation("Found {Count} LinkedChildren with non-existent parent.", orphanedByParent.Count); |
| | 0 | 543 | | orphanedLinkedChildren.AddRange(orphanedByParent); |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | // Remove all orphaned records. Both queries can return the same row, and a playlist may hold |
| | | 547 | | // several rows for one child, so the position is what identifies an entry here. |
| | 0 | 548 | | var distinctOrphaned = orphanedLinkedChildren.DistinctBy(lc => new { lc.ParentId, lc.SortOrder }).ToList(); |
| | 0 | 549 | | context.LinkedChildren.RemoveRange(distinctOrphaned); |
| | 0 | 550 | | context.SaveChanges(); |
| | | 551 | | |
| | 0 | 552 | | _logger.LogInformation("Successfully removed {Count} orphaned LinkedChildren records.", distinctOrphaned.Count); |
| | 0 | 553 | | } |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Resolves the item a legacy LinkedChild entry points at. |
| | | 557 | | /// </summary> |
| | | 558 | | private static Guid? ResolveChildId( |
| | | 559 | | JsonElement childElement, |
| | | 560 | | string? containingFolderPath, |
| | | 561 | | Dictionary<string, Guid> pathToIdMap, |
| | | 562 | | HashSet<Guid> allItemIds) |
| | | 563 | | { |
| | | 564 | | // Pre-12 data only cached ItemId and re-resolved it from the path whenever the cached value |
| | | 565 | | // went stale (BaseItem.GetLinkedChild in 10.x). An id that no longer exists must therefore |
| | | 566 | | // fall through to the path, or the entry is lost even though its file is still in the library. |
| | 0 | 567 | | if (TryGetGuidProperty(childElement, "ItemId", out var itemId) && allItemIds.Contains(itemId)) |
| | | 568 | | { |
| | 0 | 569 | | return itemId; |
| | | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | var path = GetStringProperty(childElement, "Path"); |
| | 0 | 573 | | if (!string.IsNullOrEmpty(path)) |
| | | 574 | | { |
| | 0 | 575 | | if (pathToIdMap.TryGetValue(path, out var idByPath)) |
| | | 576 | | { |
| | 0 | 577 | | return idByPath; |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | // 10.x resolved entries relative to the container that holds them. |
| | 0 | 581 | | if (!Path.IsPathRooted(path) && !string.IsNullOrEmpty(containingFolderPath)) |
| | | 582 | | { |
| | 0 | 583 | | string? absolutePath = null; |
| | | 584 | | try |
| | | 585 | | { |
| | 0 | 586 | | absolutePath = Path.GetFullPath(Path.Combine(containingFolderPath, path)); |
| | 0 | 587 | | } |
| | 0 | 588 | | catch (ArgumentException) |
| | | 589 | | { |
| | | 590 | | // Malformed path, nothing to resolve. |
| | 0 | 591 | | } |
| | | 592 | | |
| | 0 | 593 | | if (absolutePath is not null && pathToIdMap.TryGetValue(absolutePath, out var idByAbsolutePath)) |
| | | 594 | | { |
| | 0 | 595 | | return idByAbsolutePath; |
| | | 596 | | } |
| | | 597 | | } |
| | | 598 | | } |
| | | 599 | | |
| | 0 | 600 | | if (TryGetGuidProperty(childElement, "LibraryItemId", out var libraryItemId) && allItemIds.Contains(libraryItemI |
| | | 601 | | { |
| | 0 | 602 | | return libraryItemId; |
| | | 603 | | } |
| | | 604 | | |
| | 0 | 605 | | return null; |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | private static string? GetStringProperty(JsonElement element, string propertyName) |
| | 0 | 609 | | => element.TryGetProperty(propertyName, out var property) && property.ValueKind == JsonValueKind.String |
| | 0 | 610 | | ? property.GetString() |
| | 0 | 611 | | : null; |
| | | 612 | | |
| | | 613 | | private static bool TryGetGuidProperty(JsonElement element, string propertyName, out Guid value) |
| | | 614 | | { |
| | 0 | 615 | | value = Guid.Empty; |
| | 0 | 616 | | var raw = GetStringProperty(element, propertyName); |
| | | 617 | | |
| | 0 | 618 | | return !string.IsNullOrEmpty(raw) && Guid.TryParse(raw, out value) && !value.IsEmpty(); |
| | | 619 | | } |
| | | 620 | | |
| | | 621 | | private void ProcessVideoAlternateVersions( |
| | | 622 | | JsonElement root, |
| | | 623 | | Guid parentId, |
| | | 624 | | Dictionary<string, Guid> pathToIdMap, |
| | | 625 | | HashSet<Guid> allItemIds, |
| | | 626 | | List<LinkedChildEntity> linkedChildrenToAdd) |
| | | 627 | | { |
| | 0 | 628 | | int sortOrder = 0; |
| | | 629 | | |
| | 0 | 630 | | if (root.TryGetProperty("LocalAlternateVersions", out var localAlternateVersionsElement) |
| | 0 | 631 | | && localAlternateVersionsElement.ValueKind == JsonValueKind.Array) |
| | | 632 | | { |
| | 0 | 633 | | foreach (var pathElement in localAlternateVersionsElement.EnumerateArray()) |
| | | 634 | | { |
| | 0 | 635 | | if (pathElement.ValueKind != JsonValueKind.String) |
| | | 636 | | { |
| | | 637 | | continue; |
| | | 638 | | } |
| | | 639 | | |
| | 0 | 640 | | var path = pathElement.GetString(); |
| | 0 | 641 | | if (string.IsNullOrEmpty(path)) |
| | | 642 | | { |
| | | 643 | | continue; |
| | | 644 | | } |
| | | 645 | | |
| | | 646 | | // Try to resolve the path to an ItemId |
| | 0 | 647 | | if (pathToIdMap.TryGetValue(path, out var childId)) |
| | | 648 | | { |
| | 0 | 649 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 650 | | { |
| | 0 | 651 | | ParentId = parentId, |
| | 0 | 652 | | ChildId = childId, |
| | 0 | 653 | | ChildType = LinkedChildType.LocalAlternateVersion, |
| | 0 | 654 | | SortOrder = sortOrder++ |
| | 0 | 655 | | }); |
| | | 656 | | |
| | 0 | 657 | | _logger.LogDebug( |
| | 0 | 658 | | "Migrating LocalAlternateVersion: Parent={ParentId}, Child={ChildId}, Path={Path}", |
| | 0 | 659 | | parentId, |
| | 0 | 660 | | childId, |
| | 0 | 661 | | path); |
| | | 662 | | } |
| | | 663 | | else |
| | | 664 | | { |
| | 0 | 665 | | _logger.LogWarning( |
| | 0 | 666 | | "Could not resolve LocalAlternateVersion path to ItemId: {Path} for parent {ParentId}", |
| | 0 | 667 | | path, |
| | 0 | 668 | | parentId); |
| | | 669 | | } |
| | | 670 | | } |
| | | 671 | | } |
| | | 672 | | |
| | 0 | 673 | | if (root.TryGetProperty("LinkedAlternateVersions", out var linkedAlternateVersionsElement) |
| | 0 | 674 | | && linkedAlternateVersionsElement.ValueKind == JsonValueKind.Array) |
| | | 675 | | { |
| | 0 | 676 | | foreach (var linkedChildElement in linkedAlternateVersionsElement.EnumerateArray()) |
| | | 677 | | { |
| | 0 | 678 | | var childId = ResolveChildId(linkedChildElement, null, pathToIdMap, allItemIds); |
| | 0 | 679 | | if (!childId.HasValue) |
| | | 680 | | { |
| | 0 | 681 | | _logger.LogWarning("Could not resolve LinkedAlternateVersion child ID for parent {ParentId}", parent |
| | 0 | 682 | | continue; |
| | | 683 | | } |
| | | 684 | | |
| | 0 | 685 | | linkedChildrenToAdd.Add(new LinkedChildEntity |
| | 0 | 686 | | { |
| | 0 | 687 | | ParentId = parentId, |
| | 0 | 688 | | ChildId = childId.Value, |
| | 0 | 689 | | ChildType = LinkedChildType.LinkedAlternateVersion, |
| | 0 | 690 | | SortOrder = sortOrder++ |
| | 0 | 691 | | }); |
| | | 692 | | |
| | 0 | 693 | | _logger.LogDebug( |
| | 0 | 694 | | "Migrating LinkedAlternateVersion: Parent={ParentId}, Child={ChildId}", |
| | 0 | 695 | | parentId, |
| | 0 | 696 | | childId.Value); |
| | | 697 | | } |
| | | 698 | | } |
| | 0 | 699 | | } |
| | | 700 | | } |