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