| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Xml; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Database.Implementations.Entities; |
| | | 8 | | using MediaBrowser.Controller; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Restores playlist entries from playlist.xml for playlists that lost all of their children. |
| | | 16 | | /// </summary> |
| | | 17 | | [JellyfinMigration("2026-07-29T12:00:00", nameof(RestorePlaylistChildrenFromMetadata))] |
| | | 18 | | internal class RestorePlaylistChildrenFromMetadata : IDatabaseMigrationRoutine |
| | | 19 | | { |
| | | 20 | | private const string PlaylistTypeName = "MediaBrowser.Controller.Playlists.Playlist"; |
| | | 21 | | private const string PlaylistFileName = "playlist.xml"; |
| | | 22 | | |
| | | 23 | | private readonly ILogger<RestorePlaylistChildrenFromMetadata> _logger; |
| | | 24 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 25 | | private readonly IServerApplicationHost _appHost; |
| | | 26 | | |
| | | 27 | | public RestorePlaylistChildrenFromMetadata( |
| | | 28 | | ILoggerFactory loggerFactory, |
| | | 29 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 30 | | IServerApplicationHost appHost) |
| | | 31 | | { |
| | 0 | 32 | | _logger = loggerFactory.CreateLogger<RestorePlaylistChildrenFromMetadata>(); |
| | 0 | 33 | | _dbProvider = dbProvider; |
| | 0 | 34 | | _appHost = appHost; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public void Perform() |
| | | 39 | | { |
| | 0 | 40 | | using var context = _dbProvider.CreateDbContext(); |
| | | 41 | | |
| | 0 | 42 | | var playlists = context.BaseItems |
| | 0 | 43 | | .Where(b => b.Type == PlaylistTypeName && b.Path != null) |
| | 0 | 44 | | .Select(b => new { b.Id, b.Name, b.Path }) |
| | 0 | 45 | | .ToList(); |
| | | 46 | | |
| | 0 | 47 | | if (playlists.Count == 0) |
| | | 48 | | { |
| | 0 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | var childCountByPlaylist = context.LinkedChildren |
| | 0 | 53 | | .Where(lc => context.BaseItems.Any(b => b.Id.Equals(lc.ParentId) && b.Type == PlaylistTypeName)) |
| | 0 | 54 | | .GroupBy(lc => lc.ParentId) |
| | 0 | 55 | | .Select(g => new { ParentId = g.Key, Count = g.Count() }) |
| | 0 | 56 | | .ToDictionary(g => g.ParentId, g => g.Count); |
| | | 57 | | |
| | 0 | 58 | | var pathToIdMap = context.BaseItems |
| | 0 | 59 | | .Where(b => b.Path != null) |
| | 0 | 60 | | .Select(b => new { b.Id, b.Path }) |
| | 0 | 61 | | .GroupBy(b => b.Path!) |
| | 0 | 62 | | .ToDictionary(g => g.Key, g => g.First().Id); |
| | | 63 | | |
| | 0 | 64 | | var restoredPlaylists = 0; |
| | 0 | 65 | | var restoredEntries = 0; |
| | | 66 | | |
| | 0 | 67 | | foreach (var playlist in playlists) |
| | | 68 | | { |
| | | 69 | | // Only directory-based (Jellyfin-managed) playlists keep their entries in playlist.xml. |
| | | 70 | | // A playlist that is itself a file (.m3u and friends) is re-read by the library scan. |
| | 0 | 71 | | var playlistPath = _appHost.ExpandVirtualPath(playlist.Path!); |
| | 0 | 72 | | var metadataPath = Path.Combine(playlistPath, PlaylistFileName); |
| | 0 | 73 | | if (!Directory.Exists(playlistPath) || !File.Exists(metadataPath)) |
| | | 74 | | { |
| | | 75 | | continue; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | var storedPaths = ReadEntryPaths(metadataPath, playlist.Id); |
| | 0 | 79 | | if (storedPaths.Count == 0) |
| | | 80 | | { |
| | | 81 | | continue; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | var childCount = childCountByPlaylist.GetValueOrDefault(playlist.Id); |
| | 0 | 85 | | if (childCount > 0) |
| | | 86 | | { |
| | | 87 | | // Merging into a playlist that still has entries would resurrect anything the user |
| | | 88 | | // removed while the metadata file was not rewritten, and there is no way to tell the |
| | | 89 | | // two apart. Report the mismatch instead so it can be checked by hand. |
| | 0 | 90 | | if (storedPaths.Count > childCount) |
| | | 91 | | { |
| | 0 | 92 | | _logger.LogWarning( |
| | 0 | 93 | | "Playlist {PlaylistName} ({PlaylistId}) holds {ChildCount} entries but {MetadataPath} lists {Sto |
| | 0 | 94 | | playlist.Name, |
| | 0 | 95 | | playlist.Id, |
| | 0 | 96 | | childCount, |
| | 0 | 97 | | metadataPath, |
| | 0 | 98 | | storedPaths.Count); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | continue; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | var sortOrder = 0; |
| | 0 | 105 | | foreach (var storedPath in storedPaths) |
| | | 106 | | { |
| | 0 | 107 | | if (!pathToIdMap.TryGetValue(storedPath, out var childId)) |
| | | 108 | | { |
| | 0 | 109 | | _logger.LogWarning( |
| | 0 | 110 | | "Cannot restore entry {EntryPath} of playlist {PlaylistName}: no library item has that path.", |
| | 0 | 111 | | storedPath, |
| | 0 | 112 | | playlist.Name); |
| | 0 | 113 | | continue; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | context.LinkedChildren.Add(new LinkedChildEntity |
| | 0 | 117 | | { |
| | 0 | 118 | | ParentId = playlist.Id, |
| | 0 | 119 | | ChildId = childId, |
| | 0 | 120 | | ChildType = LinkedChildType.Manual, |
| | 0 | 121 | | SortOrder = sortOrder |
| | 0 | 122 | | }); |
| | | 123 | | |
| | 0 | 124 | | sortOrder++; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | if (sortOrder > 0) |
| | | 128 | | { |
| | 0 | 129 | | restoredPlaylists++; |
| | 0 | 130 | | restoredEntries += sortOrder; |
| | 0 | 131 | | _logger.LogInformation( |
| | 0 | 132 | | "Restored {Count} entries of empty playlist {PlaylistName} ({PlaylistId}) from {MetadataPath}.", |
| | 0 | 133 | | sortOrder, |
| | 0 | 134 | | playlist.Name, |
| | 0 | 135 | | playlist.Id, |
| | 0 | 136 | | metadataPath); |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | if (restoredEntries > 0) |
| | | 141 | | { |
| | 0 | 142 | | context.SaveChanges(); |
| | 0 | 143 | | _logger.LogInformation("Restored {EntryCount} entries across {PlaylistCount} playlists.", restoredEntries, r |
| | | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | private List<string> ReadEntryPaths(string metadataPath, Guid playlistId) |
| | | 148 | | { |
| | 0 | 149 | | var paths = new List<string>(); |
| | 0 | 150 | | var settings = new XmlReaderSettings |
| | 0 | 151 | | { |
| | 0 | 152 | | IgnoreComments = true, |
| | 0 | 153 | | IgnoreWhitespace = true, |
| | 0 | 154 | | IgnoreProcessingInstructions = true, |
| | 0 | 155 | | DtdProcessing = DtdProcessing.Prohibit |
| | 0 | 156 | | }; |
| | | 157 | | |
| | | 158 | | try |
| | | 159 | | { |
| | 0 | 160 | | using var reader = XmlReader.Create(metadataPath, settings); |
| | 0 | 161 | | var inEntry = false; |
| | 0 | 162 | | while (reader.Read()) |
| | | 163 | | { |
| | 0 | 164 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 165 | | { |
| | | 166 | | continue; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | if (string.Equals(reader.Name, "PlaylistItem", StringComparison.Ordinal)) |
| | | 170 | | { |
| | 0 | 171 | | inEntry = true; |
| | | 172 | | } |
| | 0 | 173 | | else if (inEntry && string.Equals(reader.Name, "Path", StringComparison.Ordinal)) |
| | | 174 | | { |
| | 0 | 175 | | inEntry = false; |
| | 0 | 176 | | var value = reader.ReadElementContentAsString(); |
| | 0 | 177 | | if (!string.IsNullOrWhiteSpace(value)) |
| | | 178 | | { |
| | 0 | 179 | | paths.Add(value.Trim()); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |
| | 0 | 183 | | } |
| | 0 | 184 | | catch (Exception ex) when (ex is XmlException or IOException or UnauthorizedAccessException) |
| | | 185 | | { |
| | 0 | 186 | | _logger.LogWarning(ex, "Could not read playlist metadata {MetadataPath} of playlist {PlaylistId}.", metadata |
| | 0 | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | return paths; |
| | | 190 | | } |
| | | 191 | | } |