| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Linq; |
| | 5 | | using Jellyfin.Database.Implementations; |
| | 6 | | using Jellyfin.Database.Implementations.Entities; |
| | 7 | | using Jellyfin.Extensions; |
| | 8 | | using MediaBrowser.Controller; |
| | 9 | | using MediaBrowser.Controller.Configuration; |
| | 10 | | using MediaBrowser.Controller.Entities; |
| | 11 | | using MediaBrowser.Controller.Entities.Audio; |
| | 12 | | using MediaBrowser.Controller.Library; |
| | 13 | | using MediaBrowser.Model.IO; |
| | 14 | | using Microsoft.EntityFrameworkCore; |
| | 15 | | using Microsoft.Extensions.Logging; |
| | 16 | |
|
| | 17 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Migration to re-read creation dates for library items with internal metadata paths. |
| | 21 | | /// </summary> |
| | 22 | | [JellyfinMigration("2025-04-20T23:00:00", nameof(RefreshInternalDateModified))] |
| | 23 | | public class RefreshInternalDateModified : IDatabaseMigrationRoutine |
| | 24 | | { |
| | 25 | | private readonly ILogger<RefreshInternalDateModified> _logger; |
| | 26 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 27 | | private readonly IFileSystem _fileSystem; |
| | 28 | | private readonly IServerApplicationHost _applicationHost; |
| | 29 | | private readonly bool _useFileCreationTimeForDateAdded; |
| | 30 | |
|
| 0 | 31 | | private IReadOnlyList<string> _internalTypes = [ |
| 0 | 32 | | typeof(Genre).FullName!, |
| 0 | 33 | | typeof(MusicGenre).FullName!, |
| 0 | 34 | | typeof(MusicArtist).FullName!, |
| 0 | 35 | | typeof(People).FullName!, |
| 0 | 36 | | typeof(Studio).FullName! |
| 0 | 37 | | ]; |
| | 38 | |
|
| | 39 | | private IReadOnlyList<string> _internalPaths; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Initializes a new instance of the <see cref="RefreshInternalDateModified"/> class. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="applicationHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param> |
| | 45 | | /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param> |
| | 46 | | /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param> |
| | 47 | | /// <param name="dbProvider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param> |
| | 48 | | /// <param name="logger">The logger.</param> |
| | 49 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | 50 | | public RefreshInternalDateModified( |
| | 51 | | IServerApplicationHost applicationHost, |
| | 52 | | IServerApplicationPaths applicationPaths, |
| | 53 | | IServerConfigurationManager configurationManager, |
| | 54 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | 55 | | ILogger<RefreshInternalDateModified> logger, |
| | 56 | | IFileSystem fileSystem) |
| | 57 | | { |
| 0 | 58 | | _dbProvider = dbProvider; |
| 0 | 59 | | _logger = logger; |
| 0 | 60 | | _fileSystem = fileSystem; |
| 0 | 61 | | _applicationHost = applicationHost; |
| 0 | 62 | | _internalPaths = [ |
| 0 | 63 | | applicationPaths.ArtistsPath, |
| 0 | 64 | | applicationPaths.GenrePath, |
| 0 | 65 | | applicationPaths.MusicGenrePath, |
| 0 | 66 | | applicationPaths.StudioPath, |
| 0 | 67 | | applicationPaths.PeoplePath |
| 0 | 68 | | ]; |
| 0 | 69 | | _useFileCreationTimeForDateAdded = configurationManager.GetMetadataConfiguration().UseFileCreationTimeForDateAdd |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <inheritdoc /> |
| | 73 | | public void Perform() |
| | 74 | | { |
| | 75 | | const int Limit = 5000; |
| 0 | 76 | | int itemCount = 0, offset = 0; |
| | 77 | |
|
| 0 | 78 | | var sw = Stopwatch.StartNew(); |
| | 79 | |
|
| 0 | 80 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 81 | | var records = context.BaseItems.Count(b => _internalTypes.Contains(b.Type)); |
| 0 | 82 | | _logger.LogInformation("Checking if {Count} potentially internal items require refreshed DateModified", records) |
| | 83 | |
|
| | 84 | | do |
| | 85 | | { |
| 0 | 86 | | var results = context.BaseItems |
| 0 | 87 | | .Where(b => _internalTypes.Contains(b.Type)) |
| 0 | 88 | | .OrderBy(e => e.Id) |
| 0 | 89 | | .Skip(offset) |
| 0 | 90 | | .Take(Limit) |
| 0 | 91 | | .ToList(); |
| | 92 | |
|
| 0 | 93 | | foreach (var item in results) |
| | 94 | | { |
| 0 | 95 | | var itemPath = item.Path; |
| 0 | 96 | | if (itemPath is not null) |
| | 97 | | { |
| 0 | 98 | | var realPath = _applicationHost.ExpandVirtualPath(item.Path); |
| 0 | 99 | | if (_internalPaths.Any(path => realPath.StartsWith(path, StringComparison.Ordinal))) |
| | 100 | | { |
| 0 | 101 | | var writeTime = _fileSystem.GetLastWriteTimeUtc(realPath); |
| 0 | 102 | | var itemModificationTime = item.DateModified; |
| 0 | 103 | | if (writeTime != itemModificationTime) |
| | 104 | | { |
| 0 | 105 | | _logger.LogDebug("Reset file modification date: Old: {Old} - New: {New} - Path: {Path}", ite |
| 0 | 106 | | item.DateModified = writeTime; |
| 0 | 107 | | if (_useFileCreationTimeForDateAdded) |
| | 108 | | { |
| 0 | 109 | | item.DateCreated = _fileSystem.GetCreationTimeUtc(realPath); |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | itemCount++; |
| | 113 | | } |
| | 114 | | } |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | offset += Limit; |
| 0 | 119 | | if (offset > records) |
| | 120 | | { |
| 0 | 121 | | offset = records; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | _logger.LogInformation("Checked: {Count} - Refreshed: {Items} - Time: {Time}", offset, itemCount, sw.Elapsed |
| 0 | 125 | | } while (offset < records); |
| | 126 | |
|
| 0 | 127 | | context.SaveChanges(); |
| | 128 | |
|
| 0 | 129 | | _logger.LogInformation("Refreshed DateModified for {Count} items in {Time}", itemCount, sw.Elapsed); |
| 0 | 130 | | } |
| | 131 | | } |