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