MoveTrickplayFiles.cs 4.9 KB

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