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