| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Database.Implementations; |
| | | 10 | | using MediaBrowser.Controller.Entities; |
| | | 11 | | using MediaBrowser.Controller.IO; |
| | | 12 | | using MediaBrowser.Controller.Library; |
| | | 13 | | using MediaBrowser.Controller.Playlists; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | |
| | | 17 | | namespace Emby.Server.Implementations.Data; |
| | | 18 | | |
| | | 19 | | public class CleanDatabaseScheduledTask : ILibraryPostScanTask |
| | | 20 | | { |
| | | 21 | | private readonly ILibraryManager _libraryManager; |
| | | 22 | | private readonly ILogger<CleanDatabaseScheduledTask> _logger; |
| | | 23 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 24 | | private readonly IPathManager _pathManager; |
| | | 25 | | |
| | | 26 | | public CleanDatabaseScheduledTask( |
| | | 27 | | ILibraryManager libraryManager, |
| | | 28 | | ILogger<CleanDatabaseScheduledTask> logger, |
| | | 29 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 30 | | IPathManager pathManager) |
| | | 31 | | { |
| | 21 | 32 | | _libraryManager = libraryManager; |
| | 21 | 33 | | _logger = logger; |
| | 21 | 34 | | _dbProvider = dbProvider; |
| | 21 | 35 | | _pathManager = pathManager; |
| | 21 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 39 | | { |
| | 16 | 40 | | var deadItemsProgress = new Progress<double>(val => progress.Report(val * 0.8)); |
| | 16 | 41 | | await CleanDeadItems(cancellationToken, deadItemsProgress).ConfigureAwait(false); |
| | | 42 | | |
| | 14 | 43 | | var playlistProgress = new Progress<double>(val => progress.Report(80 + (val * 0.2))); |
| | 14 | 44 | | await CleanOrphanedFilePlaylistsAsync(cancellationToken, playlistProgress).ConfigureAwait(false); |
| | 14 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress) |
| | | 48 | | { |
| | 16 | 49 | | var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 16 | 50 | | { |
| | 16 | 51 | | HasDeadParentId = true |
| | 16 | 52 | | }); |
| | | 53 | | |
| | 16 | 54 | | var numComplete = 0; |
| | 16 | 55 | | var numItems = itemIds.Count + 1; |
| | | 56 | | |
| | 16 | 57 | | _logger.LogDebug("Cleaning {Number} items with dead parents", numItems); |
| | | 58 | | |
| | 16 | 59 | | IProgress<double> subProgress = new Progress<double>((val) => progress.Report(val / 2)); |
| | | 60 | | |
| | 32 | 61 | | foreach (var itemId in itemIds) |
| | | 62 | | { |
| | 0 | 63 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 64 | | |
| | 0 | 65 | | var item = _libraryManager.GetItemById(itemId); |
| | 0 | 66 | | if (item is not null) |
| | | 67 | | { |
| | 0 | 68 | | _logger.LogInformation("Cleaning item {Item} type: {Type} path: {Path}", item.Name, item.GetType().Name, |
| | | 69 | | |
| | 0 | 70 | | foreach (var mediaSource in item.GetMediaSources(false)) |
| | | 71 | | { |
| | | 72 | | // Delete extracted data |
| | 0 | 73 | | var mediaSourceItem = _libraryManager.GetItemById(mediaSource.Id); |
| | 0 | 74 | | if (mediaSourceItem is null) |
| | | 75 | | { |
| | | 76 | | continue; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | var extractedDataFolders = _pathManager.GetExtractedDataPaths(mediaSourceItem); |
| | 0 | 80 | | foreach (var folder in extractedDataFolders) |
| | | 81 | | { |
| | 0 | 82 | | if (Directory.Exists(folder)) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | Directory.Delete(folder, true); |
| | 0 | 87 | | } |
| | 0 | 88 | | catch (Exception e) |
| | | 89 | | { |
| | 0 | 90 | | _logger.LogWarning("Failed to remove {Folder}: {Exception}", folder, e.Message); |
| | 0 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // Delete item |
| | 0 | 97 | | _libraryManager.DeleteItem(item, new DeleteOptions |
| | 0 | 98 | | { |
| | 0 | 99 | | DeleteFileLocation = false |
| | 0 | 100 | | }); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | numComplete++; |
| | 0 | 104 | | double percent = numComplete; |
| | 0 | 105 | | percent /= numItems; |
| | 0 | 106 | | subProgress.Report(percent * 100); |
| | | 107 | | } |
| | | 108 | | |
| | 16 | 109 | | subProgress = new Progress<double>((val) => progress.Report((val / 2) + 50)); |
| | 16 | 110 | | var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 16 | 111 | | await using (context.ConfigureAwait(false)) |
| | | 112 | | { |
| | 16 | 113 | | var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false); |
| | 15 | 114 | | await using (transaction.ConfigureAwait(false)) |
| | | 115 | | { |
| | 15 | 116 | | await context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDeleteAsync(cancellationToken).Co |
| | 14 | 117 | | subProgress.Report(50); |
| | 14 | 118 | | await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); |
| | 14 | 119 | | subProgress.Report(100); |
| | | 120 | | } |
| | 14 | 121 | | } |
| | | 122 | | |
| | 14 | 123 | | progress.Report(100); |
| | 14 | 124 | | } |
| | | 125 | | |
| | | 126 | | private async Task CleanOrphanedFilePlaylistsAsync(CancellationToken cancellationToken, IProgress<double> progress) |
| | | 127 | | { |
| | 14 | 128 | | var playlists = _libraryManager.GetItemList(new InternalItemsQuery |
| | 14 | 129 | | { |
| | 14 | 130 | | IncludeItemTypes = [BaseItemKind.Playlist], |
| | 14 | 131 | | Recursive = true |
| | 14 | 132 | | }).OfType<Playlist>().ToList(); |
| | | 133 | | |
| | 14 | 134 | | var numComplete = 0; |
| | 14 | 135 | | var numItems = Math.Max(playlists.Count, 1); |
| | | 136 | | |
| | 28 | 137 | | foreach (var playlist in playlists) |
| | | 138 | | { |
| | 0 | 139 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 140 | | |
| | 0 | 141 | | if (playlist.IsFile && !File.Exists(playlist.Path)) |
| | | 142 | | { |
| | 0 | 143 | | _logger.LogInformation("Removing file-based playlist {Name} because source file {Path} no longer exists" |
| | 0 | 144 | | _libraryManager.DeleteItem(playlist, new DeleteOptions { DeleteFileLocation = false }); |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | numComplete++; |
| | 0 | 148 | | progress.Report((double)numComplete / numItems * 100); |
| | | 149 | | } |
| | | 150 | | |
| | 14 | 151 | | progress.Report(100); |
| | 14 | 152 | | } |
| | | 153 | | } |