MoveTrickplayFiles.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Server.ServerSetupApp;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Trickplay;
  11. using MediaBrowser.Model.IO;
  12. using Microsoft.Extensions.Logging;
  13. namespace Jellyfin.Server.Migrations.Routines;
  14. /// <summary>
  15. /// Migration to move trickplay files to the new directory.
  16. /// </summary>
  17. #pragma warning disable CS0618 // Type or member is obsolete
  18. [JellyfinMigration("2025-04-20T23:00:00", nameof(MoveTrickplayFiles), RunMigrationOnSetup = true)]
  19. public class MoveTrickplayFiles : IMigrationRoutine
  20. #pragma warning restore CS0618 // Type or member is obsolete
  21. {
  22. private readonly ITrickplayManager _trickplayManager;
  23. private readonly IFileSystem _fileSystem;
  24. private readonly ILibraryManager _libraryManager;
  25. private readonly IStartupLogger _logger;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="MoveTrickplayFiles"/> class.
  28. /// </summary>
  29. /// <param name="trickplayManager">Instance of the <see cref="ITrickplayManager"/> interface.</param>
  30. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  31. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  32. /// <param name="logger">The logger.</param>
  33. public MoveTrickplayFiles(
  34. ITrickplayManager trickplayManager,
  35. IFileSystem fileSystem,
  36. ILibraryManager libraryManager,
  37. IStartupLogger<MoveTrickplayFiles> logger)
  38. {
  39. _trickplayManager = trickplayManager;
  40. _fileSystem = fileSystem;
  41. _libraryManager = libraryManager;
  42. _logger = logger;
  43. }
  44. /// <inheritdoc />
  45. public void Perform()
  46. {
  47. const int Limit = 5000;
  48. int itemCount = 0, offset = 0, previousCount;
  49. var sw = Stopwatch.StartNew();
  50. var trickplayQuery = new InternalItemsQuery
  51. {
  52. MediaTypes = [MediaType.Video],
  53. SourceTypes = [SourceType.Library],
  54. IsVirtualItem = false,
  55. IsFolder = false
  56. };
  57. do
  58. {
  59. var trickplayInfos = _trickplayManager.GetTrickplayItemsAsync(Limit, offset).GetAwaiter().GetResult();
  60. trickplayQuery.ItemIds = trickplayInfos.Select(i => i.ItemId).Distinct().ToArray();
  61. var items = _libraryManager.GetItemList(trickplayQuery);
  62. foreach (var trickplayInfo in trickplayInfos)
  63. {
  64. var item = items.OfType<Video>().FirstOrDefault(i => i.Id.Equals(trickplayInfo.ItemId));
  65. if (item is null)
  66. {
  67. continue;
  68. }
  69. var moved = false;
  70. var oldPath = GetOldTrickplayDirectory(item, trickplayInfo.Width);
  71. var newPath = _trickplayManager.GetTrickplayDirectory(item, trickplayInfo.TileWidth, trickplayInfo.TileHeight, trickplayInfo.Width, false);
  72. if (_fileSystem.DirectoryExists(oldPath))
  73. {
  74. _fileSystem.MoveDirectory(oldPath, newPath);
  75. moved = true;
  76. }
  77. oldPath = GetNewOldTrickplayDirectory(item, trickplayInfo.TileWidth, trickplayInfo.TileHeight, trickplayInfo.Width, false);
  78. if (_fileSystem.DirectoryExists(oldPath))
  79. {
  80. _fileSystem.MoveDirectory(oldPath, newPath);
  81. moved = true;
  82. }
  83. if (moved)
  84. {
  85. itemCount++;
  86. }
  87. }
  88. offset += Limit;
  89. previousCount = trickplayInfos.Count;
  90. _logger.LogInformation("Checked: {Checked} - Moved: {Count} - Time: {Time}", offset, itemCount, sw.Elapsed);
  91. } while (previousCount == Limit);
  92. _logger.LogInformation("Moved {Count} items in {Time}", itemCount, sw.Elapsed);
  93. }
  94. private string GetOldTrickplayDirectory(BaseItem item, int? width = null)
  95. {
  96. var path = Path.Combine(item.GetInternalMetadataPath(), "trickplay");
  97. return width.HasValue ? Path.Combine(path, width.Value.ToString(CultureInfo.InvariantCulture)) : path;
  98. }
  99. private string GetNewOldTrickplayDirectory(BaseItem item, int tileWidth, int tileHeight, int width, bool saveWithMedia = false)
  100. {
  101. var path = saveWithMedia
  102. ? Path.Combine(item.ContainingFolderPath, Path.ChangeExtension(item.Path, ".trickplay"))
  103. : Path.Combine(item.GetInternalMetadataPath(), "trickplay");
  104. var subdirectory = string.Format(
  105. CultureInfo.InvariantCulture,
  106. "{0} - {1}x{2}",
  107. width.ToString(CultureInfo.InvariantCulture),
  108. tileWidth.ToString(CultureInfo.InvariantCulture),
  109. tileHeight.ToString(CultureInfo.InvariantCulture));
  110. return Path.Combine(path, subdirectory);
  111. }
  112. }