RefreshInternalDateModified.cs 5.2 KB

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