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