MoveTrickplayFiles.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Trickplay;
  7. using MediaBrowser.Model.IO;
  8. namespace Jellyfin.Server.Migrations.Routines;
  9. /// <summary>
  10. /// Migration to move trickplay files to the new directory.
  11. /// </summary>
  12. public class MoveTrickplayFiles : IMigrationRoutine
  13. {
  14. private readonly ITrickplayManager _trickplayManager;
  15. private readonly IFileSystem _fileSystem;
  16. private readonly ILibraryManager _libraryManager;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="MoveTrickplayFiles"/> class.
  19. /// </summary>
  20. /// <param name="trickplayManager">Instance of the <see cref="ITrickplayManager"/> interface.</param>
  21. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  22. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  23. public MoveTrickplayFiles(ITrickplayManager trickplayManager, IFileSystem fileSystem, ILibraryManager libraryManager)
  24. {
  25. _trickplayManager = trickplayManager;
  26. _fileSystem = fileSystem;
  27. _libraryManager = libraryManager;
  28. }
  29. /// <inheritdoc />
  30. public Guid Id => new("4EF123D5-8EFF-4B0B-869D-3AED07A60E1B");
  31. /// <inheritdoc />
  32. public string Name => "MoveTrickplayFiles";
  33. /// <inheritdoc />
  34. public bool PerformOnNewInstall => true;
  35. /// <inheritdoc />
  36. public void Perform()
  37. {
  38. var trickplayItems = _trickplayManager.GetTrickplayItemsAsync().GetAwaiter().GetResult();
  39. foreach (var itemId in trickplayItems)
  40. {
  41. var resolutions = _trickplayManager.GetTrickplayResolutions(itemId).GetAwaiter().GetResult();
  42. var item = _libraryManager.GetItemById(itemId);
  43. if (item is null)
  44. {
  45. continue;
  46. }
  47. foreach (var resolution in resolutions)
  48. {
  49. var oldPath = GetOldTrickplayDirectory(item, resolution.Key);
  50. var newPath = _trickplayManager.GetTrickplayDirectory(item, resolution.Value.TileWidth, resolution.Value.TileHeight, resolution.Value.Width, false);
  51. if (_fileSystem.DirectoryExists(oldPath))
  52. {
  53. _fileSystem.MoveDirectory(oldPath, newPath);
  54. }
  55. }
  56. }
  57. }
  58. private string GetOldTrickplayDirectory(BaseItem item, int? width = null)
  59. {
  60. var path = Path.Combine(item.GetInternalMetadataPath(), "trickplay");
  61. return width.HasValue ? Path.Combine(path, width.Value.ToString(CultureInfo.InvariantCulture)) : path;
  62. }
  63. }