Skip to content

The Free Software Media System - Server Backend & API

Decouple demotion detection from deletion

Shadowghost457c53da6f48
diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs
index cc322d0ddd..ed41bdb6ba 100644
--- a/MediaBrowser.Controller/Entities/Folder.cs
+++ b/MediaBrowser.Controller/Entities/Folder.cs
@@ -513,19 +513,6 @@ namespace MediaBrowser.Controller.Entities
// Check if path is in LocalAlternateVersions of any valid child
if (!string.IsNullOrEmpty(item.Path) && alternateVersionPaths.Contains(item.Path))
{
- // If this was a primary (no PrimaryVersionId, no OwnerId), it needs demotion
- if (video.OwnerId.IsEmpty())
- {
- var newPrimary = newItems
- .OfType<Video>()
- .FirstOrDefault(v => (v.LocalAlternateVersions ?? [])
- .Any(p => string.Equals(p, item.Path, StringComparison.OrdinalIgnoreCase)));
- if (newPrimary is not null)
- {
- oldPrimariesToDemote.Add((video, newPrimary));
- }
- }
-
Logger.LogDebug("Item path matches an alternate version, skipping deletion: {Path}", item.Path);
continue;
}
@@ -540,6 +527,27 @@ namespace MediaBrowser.Controller.Entities
LibraryManager.DeleteItem(item, new DeleteOptions { DeleteFileLocation = false }, this, false);
}
}
+
+ // Detect items that need demotion AFTER all deletions have run.
+ // DeleteItem may promote an alternate to primary (clearing its OwnerId),
+ // so we must check OwnerId after the deletion loop to see the updated state.
+ foreach (var item in itemsRemoved.Except(actuallyRemoved))
+ {
+ if (item is Video video
+ && video.OwnerId.IsEmpty()
+ && !string.IsNullOrEmpty(item.Path)
+ && alternateVersionPaths.Contains(item.Path))
+ {
+ var newPrimary = newItems
+ .OfType<Video>()
+ .FirstOrDefault(v => (v.LocalAlternateVersions ?? [])
+ .Any(p => string.Equals(p, item.Path, StringComparison.OrdinalIgnoreCase)));
+ if (newPrimary is not null)
+ {
+ oldPrimariesToDemote.Add((video, newPrimary));
+ }
+ }
+ }
}
if (newItems.Count > 0)