| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Database.Implementations; |
| | | 9 | | using Jellyfin.Server.ServerSetupApp; |
| | | 10 | | using MediaBrowser.Common.Configuration; |
| | | 11 | | using MediaBrowser.Controller; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Removes on-disk external item data (attachments, subtitles, trickplay tiles, chapter images) for items that |
| | | 19 | | /// no longer exist in the <c>BaseItems</c> table. The database side is cleaned up synchronously by |
| | | 20 | | /// <c>IItemPersistenceService.DeleteItem</c>, so the leftover orphans live on the filesystem. |
| | | 21 | | /// </summary> |
| | | 22 | | [JellyfinMigration("2026-05-25T01:00:00", nameof(CleanupOrphanedExternalData))] |
| | | 23 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 24 | | public class CleanupOrphanedExternalData : IAsyncMigrationRoutine |
| | | 25 | | { |
| | | 26 | | private const int ProgressLogStep = 500; |
| | | 27 | | |
| | | 28 | | private readonly IStartupLogger<CleanupOrphanedExternalData> _logger; |
| | | 29 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 30 | | private readonly IApplicationPaths _appPaths; |
| | | 31 | | private readonly IServerApplicationPaths _serverPaths; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="CleanupOrphanedExternalData"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="logger">The startup logger.</param> |
| | | 37 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 38 | | /// <param name="appPaths">The application paths.</param> |
| | | 39 | | /// <param name="serverPaths">The server application paths.</param> |
| | | 40 | | public CleanupOrphanedExternalData( |
| | | 41 | | IStartupLogger<CleanupOrphanedExternalData> logger, |
| | | 42 | | IDbContextFactory<JellyfinDbContext> dbContextFactory, |
| | | 43 | | IApplicationPaths appPaths, |
| | | 44 | | IServerApplicationPaths serverPaths) |
| | | 45 | | { |
| | 0 | 46 | | _logger = logger; |
| | 0 | 47 | | _dbContextFactory = dbContextFactory; |
| | 0 | 48 | | _appPaths = appPaths; |
| | 0 | 49 | | _serverPaths = serverPaths; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc/> |
| | | 53 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 54 | | { |
| | 0 | 55 | | var knownIds = await LoadKnownItemIdsAsync(cancellationToken).ConfigureAwait(false); |
| | | 56 | | |
| | 0 | 57 | | CleanupGuidIndexedRoot( |
| | 0 | 58 | | "attachment", |
| | 0 | 59 | | Path.Combine(_appPaths.DataPath, "attachments"), |
| | 0 | 60 | | knownIds, |
| | 0 | 61 | | deleteSubPath: null, |
| | 0 | 62 | | cancellationToken); |
| | | 63 | | |
| | 0 | 64 | | CleanupGuidIndexedRoot( |
| | 0 | 65 | | "subtitle", |
| | 0 | 66 | | Path.Combine(_appPaths.DataPath, "subtitles"), |
| | 0 | 67 | | knownIds, |
| | 0 | 68 | | deleteSubPath: null, |
| | 0 | 69 | | cancellationToken); |
| | | 70 | | |
| | 0 | 71 | | CleanupGuidIndexedRoot( |
| | 0 | 72 | | "trickplay", |
| | 0 | 73 | | _appPaths.TrickplayPath, |
| | 0 | 74 | | knownIds, |
| | 0 | 75 | | deleteSubPath: null, |
| | 0 | 76 | | cancellationToken); |
| | | 77 | | |
| | 0 | 78 | | CleanupGuidIndexedRoot( |
| | 0 | 79 | | "chapter image", |
| | 0 | 80 | | Path.Combine(_serverPaths.InternalMetadataPath, "library"), |
| | 0 | 81 | | knownIds, |
| | 0 | 82 | | deleteSubPath: "chapters", |
| | 0 | 83 | | cancellationToken); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | private async Task<HashSet<Guid>> LoadKnownItemIdsAsync(CancellationToken cancellationToken) |
| | | 87 | | { |
| | 0 | 88 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 89 | | await using (context.ConfigureAwait(false)) |
| | | 90 | | { |
| | 0 | 91 | | var ids = await context.BaseItems |
| | 0 | 92 | | .AsNoTracking() |
| | 0 | 93 | | .Select(b => b.Id) |
| | 0 | 94 | | .ToListAsync(cancellationToken) |
| | 0 | 95 | | .ConfigureAwait(false); |
| | 0 | 96 | | return [.. ids]; |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | private void CleanupGuidIndexedRoot( |
| | | 101 | | string label, |
| | | 102 | | string root, |
| | | 103 | | HashSet<Guid> knownIds, |
| | | 104 | | string? deleteSubPath, |
| | | 105 | | CancellationToken cancellationToken) |
| | | 106 | | { |
| | 0 | 107 | | if (string.IsNullOrEmpty(root) || !Directory.Exists(root)) |
| | | 108 | | { |
| | 0 | 109 | | _logger.LogInformation("Skipping {Label} cleanup; root {Root} does not exist", label, root); |
| | 0 | 110 | | return; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | _logger.LogInformation("Scanning for orphaned {Label} data under {Root}", label, root); |
| | | 114 | | |
| | 0 | 115 | | var scanned = 0; |
| | 0 | 116 | | var removed = 0; |
| | 0 | 117 | | foreach (var prefixDir in Directory.EnumerateDirectories(root)) |
| | | 118 | | { |
| | 0 | 119 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 120 | | |
| | 0 | 121 | | var prefixName = Path.GetFileName(prefixDir); |
| | 0 | 122 | | if (prefixName.Length != 2) |
| | | 123 | | { |
| | | 124 | | continue; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | foreach (var guidDir in Directory.EnumerateDirectories(prefixDir)) |
| | | 128 | | { |
| | 0 | 129 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 130 | | |
| | 0 | 131 | | scanned++; |
| | 0 | 132 | | if (scanned % ProgressLogStep == 0) |
| | | 133 | | { |
| | 0 | 134 | | _logger.LogInformation("Scanning {Label}: {Scanned} directories examined, {Removed} orphans removed |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | var leafName = Path.GetFileName(guidDir); |
| | 0 | 138 | | if (!Guid.TryParse(leafName, CultureInfo.InvariantCulture, out var id)) |
| | | 139 | | { |
| | | 140 | | continue; |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | if (knownIds.Contains(id)) |
| | | 144 | | { |
| | | 145 | | continue; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | var target = deleteSubPath is null ? guidDir : Path.Combine(guidDir, deleteSubPath); |
| | 0 | 149 | | if (deleteSubPath is not null && !Directory.Exists(target)) |
| | | 150 | | { |
| | | 151 | | continue; |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | if (TryDelete(target)) |
| | | 155 | | { |
| | 0 | 156 | | removed++; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | _logger.LogInformation("Finished {Label} cleanup: scanned {Scanned} directories, removed {Removed} orphans", lab |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | private bool TryDelete(string dir) |
| | | 165 | | { |
| | | 166 | | try |
| | | 167 | | { |
| | 0 | 168 | | Directory.Delete(dir, recursive: true); |
| | 0 | 169 | | return true; |
| | | 170 | | } |
| | 0 | 171 | | catch (IOException ex) |
| | | 172 | | { |
| | 0 | 173 | | _logger.LogWarning(ex, "Failed to delete orphaned directory {Dir}", dir); |
| | 0 | 174 | | } |
| | 0 | 175 | | catch (UnauthorizedAccessException ex) |
| | | 176 | | { |
| | 0 | 177 | | _logger.LogWarning(ex, "Permission denied deleting orphaned directory {Dir}", dir); |
| | 0 | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | return false; |
| | 0 | 181 | | } |
| | | 182 | | } |