| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Text.Json; |
| | 9 | | using Jellyfin.Data.Enums; |
| | 10 | | using Jellyfin.Database.Implementations; |
| | 11 | | using Jellyfin.Database.Implementations.Entities; |
| | 12 | | using Jellyfin.Extensions.Json; |
| | 13 | | using MediaBrowser.Common.Configuration; |
| | 14 | | using MediaBrowser.Common.Extensions; |
| | 15 | | using MediaBrowser.Controller.Entities; |
| | 16 | | using MediaBrowser.Controller.Library; |
| | 17 | | using Microsoft.EntityFrameworkCore; |
| | 18 | | using Microsoft.Extensions.Logging; |
| | 19 | |
|
| | 20 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Migration to move extracted files to the new directories. |
| | 24 | | /// </summary> |
| | 25 | | public class MigrateKeyframeData : IDatabaseMigrationRoutine |
| | 26 | | { |
| | 27 | | private readonly ILibraryManager _libraryManager; |
| | 28 | | private readonly ILogger<MoveTrickplayFiles> _logger; |
| | 29 | | private readonly IApplicationPaths _appPaths; |
| | 30 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| 0 | 31 | | private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="MigrateKeyframeData"/> class. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 37 | | /// <param name="logger">The logger.</param> |
| | 38 | | /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param> |
| | 39 | | /// <param name="dbProvider">The EFCore db factory.</param> |
| | 40 | | public MigrateKeyframeData( |
| | 41 | | ILibraryManager libraryManager, |
| | 42 | | ILogger<MoveTrickplayFiles> logger, |
| | 43 | | IApplicationPaths appPaths, |
| | 44 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | 45 | | { |
| 0 | 46 | | _libraryManager = libraryManager; |
| 0 | 47 | | _logger = logger; |
| 0 | 48 | | _appPaths = appPaths; |
| 0 | 49 | | _dbProvider = dbProvider; |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | private string KeyframeCachePath => Path.Combine(_appPaths.DataPath, "keyframes"); |
| | 53 | |
|
| | 54 | | /// <inheritdoc /> |
| 0 | 55 | | public Guid Id => new("EA4bCAE1-09A4-428E-9B90-4B4FD2EA1B24"); |
| | 56 | |
|
| | 57 | | /// <inheritdoc /> |
| 0 | 58 | | public string Name => "MigrateKeyframeData"; |
| | 59 | |
|
| | 60 | | /// <inheritdoc /> |
| 0 | 61 | | public bool PerformOnNewInstall => false; |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public void Perform() |
| | 65 | | { |
| | 66 | | const int Limit = 100; |
| 0 | 67 | | int itemCount = 0, offset = 0, previousCount; |
| | 68 | |
|
| 0 | 69 | | var sw = Stopwatch.StartNew(); |
| 0 | 70 | | var itemsQuery = new InternalItemsQuery |
| 0 | 71 | | { |
| 0 | 72 | | MediaTypes = [MediaType.Video], |
| 0 | 73 | | SourceTypes = [SourceType.Library], |
| 0 | 74 | | IsVirtualItem = false, |
| 0 | 75 | | IsFolder = false |
| 0 | 76 | | }; |
| | 77 | |
|
| 0 | 78 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 79 | | context.KeyframeData.ExecuteDelete(); |
| 0 | 80 | | using var transaction = context.Database.BeginTransaction(); |
| 0 | 81 | | List<KeyframeData> keyframes = []; |
| | 82 | |
|
| | 83 | | do |
| | 84 | | { |
| 0 | 85 | | var result = _libraryManager.GetItemsResult(itemsQuery); |
| 0 | 86 | | _logger.LogInformation("Importing keyframes for {Count} items", result.TotalRecordCount); |
| | 87 | |
|
| 0 | 88 | | var items = result.Items; |
| 0 | 89 | | previousCount = items.Count; |
| 0 | 90 | | offset += Limit; |
| 0 | 91 | | foreach (var item in items) |
| | 92 | | { |
| 0 | 93 | | if (TryGetKeyframeData(item, out var data)) |
| | 94 | | { |
| 0 | 95 | | keyframes.Add(data); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (++itemCount % 10_000 == 0) |
| | 99 | | { |
| 0 | 100 | | context.KeyframeData.AddRange(keyframes); |
| 0 | 101 | | keyframes.Clear(); |
| 0 | 102 | | _logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed); |
| | 103 | | } |
| | 104 | | } |
| 0 | 105 | | } while (previousCount == Limit); |
| | 106 | |
|
| 0 | 107 | | context.KeyframeData.AddRange(keyframes); |
| 0 | 108 | | context.SaveChanges(); |
| 0 | 109 | | transaction.Commit(); |
| | 110 | |
|
| 0 | 111 | | _logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed); |
| | 112 | |
|
| 0 | 113 | | if (Directory.Exists(KeyframeCachePath)) |
| | 114 | | { |
| 0 | 115 | | Directory.Delete(KeyframeCachePath, true); |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | private bool TryGetKeyframeData(BaseItem item, [NotNullWhen(true)] out KeyframeData? data) |
| | 120 | | { |
| 0 | 121 | | data = null; |
| 0 | 122 | | var path = item.Path; |
| 0 | 123 | | if (!string.IsNullOrEmpty(path)) |
| | 124 | | { |
| 0 | 125 | | var cachePath = GetCachePath(KeyframeCachePath, path); |
| 0 | 126 | | if (TryReadFromCache(cachePath, out var keyframeData)) |
| | 127 | | { |
| 0 | 128 | | data = new() |
| 0 | 129 | | { |
| 0 | 130 | | ItemId = item.Id, |
| 0 | 131 | | KeyframeTicks = keyframeData.KeyframeTicks.ToList(), |
| 0 | 132 | | TotalDuration = keyframeData.TotalDuration |
| 0 | 133 | | }; |
| | 134 | |
|
| 0 | 135 | | return true; |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | return false; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | private string? GetCachePath(string keyframeCachePath, string filePath) |
| | 143 | | { |
| | 144 | | DateTime? lastWriteTimeUtc; |
| | 145 | | try |
| | 146 | | { |
| 0 | 147 | | lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath); |
| 0 | 148 | | } |
| 0 | 149 | | catch (IOException e) |
| | 150 | | { |
| 0 | 151 | | _logger.LogDebug("Skipping {Path}: {Exception}", filePath, e.Message); |
| | 152 | |
|
| 0 | 153 | | return null; |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Value.Ticks.ToString(CultureInfo.InvariantCultu |
| 0 | 157 | | var prefix = filename[..1]; |
| | 158 | |
|
| 0 | 159 | | return Path.Join(keyframeCachePath, prefix, filename); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | private static bool TryReadFromCache(string? cachePath, [NotNullWhen(true)] out MediaEncoding.Keyframes.KeyframeData |
| | 163 | | { |
| 0 | 164 | | if (File.Exists(cachePath)) |
| | 165 | | { |
| 0 | 166 | | var bytes = File.ReadAllBytes(cachePath); |
| 0 | 167 | | cachedResult = JsonSerializer.Deserialize<MediaEncoding.Keyframes.KeyframeData>(bytes, _jsonOptions); |
| | 168 | |
|
| 0 | 169 | | return cachedResult is not null; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | cachedResult = null; |
| | 173 | |
|
| 0 | 174 | | return false; |
| | 175 | | } |
| | 176 | | } |